From eb8f59aa277f82f87232aeec4eff837ed6578d05 Mon Sep 17 00:00:00 2001 From: eordano Date: Wed, 18 Feb 2015 13:19:34 -0300 Subject: [PATCH] Drop duplicated code --- test/hdkeys.js | 36 ++++++++++++++++++++++++++++++++++++ test/hdprivatekey.js | 26 -------------------------- test/hdpublickey.js | 29 ----------------------------- 3 files changed, 36 insertions(+), 55 deletions(-) diff --git a/test/hdkeys.js b/test/hdkeys.js index d941eda..36900a0 100644 --- a/test/hdkeys.js +++ b/test/hdkeys.js @@ -10,12 +10,48 @@ /* jshint maxstatements: 100 */ /* jshint unused: false */ +var _ = require('lodash'); var should = require('chai').should(); +var expect = require('chai').expect; var bitcore = require('..'); var Networks = bitcore.Networks; var HDPrivateKey = bitcore.HDPrivateKey; var HDPublicKey = bitcore.HDPublicKey; +describe('HDKeys building with static methods', function() { + var classes = [HDPublicKey, HDPrivateKey]; + var clazz, index; + + _.each(classes, function(clazz) { + var expectStaticMethodFail = function(staticMethod, argument, message) { + expect(clazz[staticMethod].bind(null, argument)).to.throw(message); + }; + it(clazz.name + ' fromJSON checks that a valid JSON is provided', function() { + var errorMessage = 'No valid JSON string was provided'; + var method = 'fromJSON'; + expectStaticMethodFail(method, undefined, errorMessage); + expectStaticMethodFail(method, null, errorMessage); + expectStaticMethodFail(method, 'invalid JSON', errorMessage); + expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage); + expectStaticMethodFail(method, {}, errorMessage); + }); + it(clazz.name + ' fromString checks that a string is provided', function() { + var errorMessage = 'No valid string was provided'; + var method = 'fromString'; + expectStaticMethodFail(method, undefined, errorMessage); + expectStaticMethodFail(method, null, errorMessage); + expectStaticMethodFail(method, {}, errorMessage); + }); + it(clazz.name + ' fromObject checks that an object is provided', function() { + var errorMessage = 'No valid argument was provided'; + var method = 'fromObject'; + expectStaticMethodFail(method, undefined, errorMessage); + expectStaticMethodFail(method, null, errorMessage); + expectStaticMethodFail(method, '', errorMessage); + }); + }); +}); + describe('BIP32 compliance', function() { it('should initialize test vector 1 from the extended public key', function() { diff --git a/test/hdprivatekey.js b/test/hdprivatekey.js index eabad67..b41ab9f 100644 --- a/test/hdprivatekey.js +++ b/test/hdprivatekey.js @@ -85,32 +85,6 @@ describe('HDPrivate key interface', function() { it('allows no-new calling', function() { HDPrivateKey(xprivkey).toString().should.equal(xprivkey); }); - var expectStaticMethodFail = function(staticMethod, argument, message) { - expect(HDPrivateKey[staticMethod].bind(null, argument)).to.throw(message); - }; - it('fromJSON checks that a valid JSON is provided', function() { - var errorMessage = 'No valid JSON string was provided'; - var method = 'fromJSON'; - expectStaticMethodFail(method, undefined, errorMessage); - expectStaticMethodFail(method, null, errorMessage); - expectStaticMethodFail(method, 'invalid JSON', errorMessage); - expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage); - expectStaticMethodFail(method, {}, errorMessage); - }); - it('fromString checks that a string is provided', function() { - var errorMessage = 'No valid string was provided'; - var method = 'fromString'; - expectStaticMethodFail(method, undefined, errorMessage); - expectStaticMethodFail(method, null, errorMessage); - expectStaticMethodFail(method, {}, errorMessage); - }); - it('fromObject checks that an object is provided', function() { - var errorMessage = 'No valid argument was provided'; - var method = 'fromObject'; - expectStaticMethodFail(method, undefined, errorMessage); - expectStaticMethodFail(method, null, errorMessage); - expectStaticMethodFail(method, '', errorMessage); - }); }); it('inspect() displays correctly', function() { diff --git a/test/hdpublickey.js b/test/hdpublickey.js index 54816b0..c0e4938 100644 --- a/test/hdpublickey.js +++ b/test/hdpublickey.js @@ -124,35 +124,6 @@ describe('HDPublicKey interface', function() { }); }); - describe('building with static methods', function() { - var expectStaticMethodFail = function(staticMethod, argument, message) { - expect(HDPublicKey[staticMethod].bind(null, argument)).to.throw(message); - }; - it('fromJSON checks that a valid JSON is provided', function() { - var errorMessage = 'No valid JSON string was provided'; - var method = 'fromJSON'; - expectStaticMethodFail(method, undefined, errorMessage); - expectStaticMethodFail(method, null, errorMessage); - expectStaticMethodFail(method, 'invalid JSON', errorMessage); - expectStaticMethodFail(method, '{\'singlequotes\': true}', errorMessage); - expectStaticMethodFail(method, {}, errorMessage); - }); - it('fromString checks that a string is provided', function() { - var errorMessage = 'No valid string was provided'; - var method = 'fromString'; - expectStaticMethodFail(method, undefined, errorMessage); - expectStaticMethodFail(method, null, errorMessage); - expectStaticMethodFail(method, {}, errorMessage); - }); - it('fromObject checks that an object is provided', function() { - var errorMessage = 'No valid argument was provided'; - var method = 'fromObject'; - expectStaticMethodFail(method, undefined, errorMessage); - expectStaticMethodFail(method, null, errorMessage); - expectStaticMethodFail(method, '', errorMessage); - }); - }); - describe('error checking on serialization', function() { var compareType = function(a, b) { expect(a instanceof b).to.equal(true);