Drop duplicated code

This commit is contained in:
eordano 2015-02-18 13:19:34 -03:00
parent 9c3170cb3a
commit eb8f59aa27
3 changed files with 36 additions and 55 deletions

View File

@ -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() {

View File

@ -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() {

View File

@ -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);