Browse Source
100% code coverage for the preconditions module. Usage: ``` $.checkState(something === anotherthing, 'Expected something to be anotherthing'); $.checkArgument(something < 100, 'something', 'must be less than 100'); $.checkArgumentType(something, PrivateKey, 'something'); // The third argument is a helper to mention the name of the argument $.checkArgumentType(something, PrivateKey); // but it's optional (will show up as "(unknown argument)") ```patch-2
Esteban Ordano
10 years ago
8 changed files with 212 additions and 132 deletions
@ -1,85 +1,80 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
function format(arg) { |
||||
|
return '\'' + arg |
||||
|
.replace('{0}', '\' + arguments[0] + \'') |
||||
|
.replace('{1}', '\' + arguments[1] + \'') |
||||
|
.replace('{2}', '\' + arguments[2] + \'') + '\''; |
||||
|
} |
||||
|
|
||||
module.exports = [{ |
module.exports = [{ |
||||
name: 'HDPrivateKey', |
name: 'InvalidB58Char', |
||||
message: 'Internal Error on HDPrivateKey {0}', |
message: format('Invalid Base58 character: {0} in {1}') |
||||
errors: [ |
}, { |
||||
{ |
name: 'InvalidB58Checksum', |
||||
name: 'InvalidArgument', |
message: format('Invalid Base58 checksum for {0}') |
||||
message: 'Invalid Argument {0}, expected {1} but got {2}', |
}, { |
||||
|
name: 'InvalidNetwork', |
||||
|
message: format('Invalid version for network: got {0}') |
||||
|
}, { |
||||
|
name: 'InvalidState', |
||||
|
message: format('Invalid state: {0}') |
||||
|
}, { |
||||
|
name: 'InvalidNetworkArgument', |
||||
|
message: format('Invalid network: must be "livenet" or "testnet", got {0}') |
||||
|
}, { |
||||
|
name: 'InvalidArgument', |
||||
|
message: format('Invalid Argument {0}, {1}'), |
||||
|
}, { |
||||
|
name: 'InvalidArgumentType', |
||||
|
message: format('Invalid Argument for {2}, expected {1} but got ') + '+ typeof arguments[0]', |
||||
|
}, { |
||||
|
name: 'HDPrivateKey', |
||||
|
message: format('Internal Error on HDPrivateKey {0}'), |
||||
|
errors: [{ |
||||
|
name: 'InvalidDerivationArgument', |
||||
|
message: format('Invalid derivation argument {0}, expected string, or number and boolean') |
||||
|
}, { |
||||
|
name: 'InvalidEntropyArgument', |
||||
|
message: format('Invalid entropy: must be an hexa string or binary buffer, got {0}'), |
||||
errors: [{ |
errors: [{ |
||||
name: 'InvalidB58Char', |
name: 'TooMuchEntropy', |
||||
message: 'Invalid Base58 character: {0} in {1}' |
message: format('Invalid entropy: more than 512 bits is non standard, got "{0}"') |
||||
}, { |
}, { |
||||
name: 'InvalidB58Checksum', |
name: 'NotEnoughEntropy', |
||||
message: 'Invalid Base58 checksum for {0}' |
message: format('Invalid entropy: at least 128 bits needed, got "{0}"') |
||||
}, { |
|
||||
name: 'InvalidDerivationArgument', |
|
||||
message: 'Invalid derivation argument {0}, expected string, or number and boolean' |
|
||||
}, { |
|
||||
name: 'InvalidEntropyArgument', |
|
||||
message: 'Invalid entropy: must be an hexa string or binary buffer, got {0}', |
|
||||
errors: [{ |
|
||||
name: 'TooMuchEntropy', |
|
||||
message: 'Invalid entropy: more than 512 bits is non standard, got "{0}"' |
|
||||
}, { |
|
||||
name: 'NotEnoughEntropy', |
|
||||
message: 'Invalid entropy: at least 128 bits needed, got "{0}"' |
|
||||
}] |
|
||||
}, { |
|
||||
name: 'InvalidLength', |
|
||||
message: 'Invalid length for xprivkey string in {0}' |
|
||||
}, { |
|
||||
name: 'InvalidNetwork', |
|
||||
message: 'Invalid version for network: got {0}' |
|
||||
}, { |
|
||||
name: 'InvalidNetworkArgument', |
|
||||
message: 'Invalid network: must be "livenet" or "testnet", got {0}' |
|
||||
}, { |
|
||||
name: 'InvalidPath', |
|
||||
message: 'Invalid derivation path: {0}' |
|
||||
}, { |
|
||||
name: 'UnrecognizedArgument', |
|
||||
message: 'Invalid argument: creating a HDPrivateKey requires a string, buffer, json or object, got "{0}"' |
|
||||
}] |
|
||||
} |
|
||||
] |
|
||||
}, { |
|
||||
name: 'HDPublicKey', |
|
||||
message: 'Internal Error on HDPublicKey {0}', |
|
||||
errors: [ |
|
||||
{ |
|
||||
name: 'InvalidArgument', |
|
||||
message: 'Invalid Argument {0}, expected {1} but got {2}', |
|
||||
errors: [{ |
|
||||
name: 'ArgumentIsPrivateExtended', |
|
||||
message: 'Argument is an extended private key: {0}' |
|
||||
}, { |
|
||||
name: 'InvalidB58Char', |
|
||||
message: 'Invalid Base58 character: {0} in {1}' |
|
||||
}, { |
|
||||
name: 'InvalidB58Checksum', |
|
||||
message: 'Invalid Base58 checksum for {0}' |
|
||||
}, { |
|
||||
name: 'InvalidDerivationArgument', |
|
||||
message: 'Invalid derivation argument: got {0}' |
|
||||
}, { |
|
||||
name: 'InvalidLength', |
|
||||
message: 'Invalid length for xpubkey: got "{0}"' |
|
||||
}, { |
|
||||
name: 'InvalidNetwork', |
|
||||
message: 'Invalid network, expected a different version: got "{0}"' |
|
||||
}, { |
|
||||
name: 'InvalidNetworkArgument', |
|
||||
message: 'Expected network to be "livenet" or "testnet", got "{0}"' |
|
||||
}, { |
|
||||
name: 'InvalidPath', |
|
||||
message: 'Invalid derivation path, it should look like: "m/1/100", got "{0}"' |
|
||||
}, { |
|
||||
name: 'MustSupplyArgument', |
|
||||
message: 'Must supply an argument to create a HDPublicKey' |
|
||||
}, { |
|
||||
name: 'UnrecognizedArgument', |
|
||||
message: 'Invalid argument for creation, must be string, json, buffer, or object' |
|
||||
}] |
}] |
||||
} |
}, { |
||||
] |
name: 'InvalidLength', |
||||
}]; |
message: format('Invalid length for xprivkey string in {0}') |
||||
|
}, { |
||||
|
name: 'InvalidPath', |
||||
|
message: format('Invalid derivation path: {0}') |
||||
|
}, { |
||||
|
name: 'UnrecognizedArgument', |
||||
|
message: format('Invalid argument: creating a HDPrivateKey requires a string, buffer, json or object, got "{0}"') |
||||
|
}] |
||||
|
}, { |
||||
|
name: 'HDPublicKey', |
||||
|
message: format('Internal Error on HDPublicKey {0}'), |
||||
|
errors: [{ |
||||
|
name: 'ArgumentIsPrivateExtended', |
||||
|
message: format('Argument is an extended private key: {0}') |
||||
|
}, { |
||||
|
name: 'InvalidDerivationArgument', |
||||
|
message: format('Invalid derivation argument: got {0}') |
||||
|
}, { |
||||
|
name: 'InvalidLength', |
||||
|
message: format('Invalid length for xpubkey: got "{0}"') |
||||
|
}, { |
||||
|
name: 'InvalidPath', |
||||
|
message: format('Invalid derivation path, it should look like: "m/1/100", got "{0}"') |
||||
|
}, { |
||||
|
name: 'MustSupplyArgument', |
||||
|
message: format('Must supply an argument to create a HDPublicKey') |
||||
|
}, { |
||||
|
name: 'UnrecognizedArgument', |
||||
|
message: format('Invalid argument for creation, must be string, json, buffer, or object') |
||||
|
}] |
||||
|
} |
||||
|
]; |
||||
|
@ -0,0 +1,29 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var errors = require('../errors'); |
||||
|
var _ = require('lodash'); |
||||
|
|
||||
|
module.exports = { |
||||
|
checkState: function(condition, message) { |
||||
|
if (!condition) { |
||||
|
throw new errors.InvalidState(message); |
||||
|
} |
||||
|
}, |
||||
|
checkArgument: function(condition, argumentName, message) { |
||||
|
if (!condition) { |
||||
|
throw new errors.InvalidArgument(argumentName, message); |
||||
|
} |
||||
|
}, |
||||
|
checkArgumentType: function(argument, type, argumentName) { |
||||
|
argumentName = argumentName || '(unknown name)'; |
||||
|
if (_.isString(type)) { |
||||
|
if (typeof argument !== type) { |
||||
|
throw new errors.InvalidArgumentType(argument, type, argumentName); |
||||
|
} |
||||
|
} else { |
||||
|
if (!(argument instanceof type)) { |
||||
|
throw new errors.InvalidArgumentType(argument, type.name, argumentName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
}; |
@ -0,0 +1,61 @@ |
|||||
|
'use strict'; |
||||
|
|
||||
|
var errors = require('../../lib/errors'); |
||||
|
var $ = require('../../lib/util/preconditions'); |
||||
|
var PrivateKey = require('../../lib/privatekey'); |
||||
|
|
||||
|
describe('preconditions', function() { |
||||
|
|
||||
|
it('can be used to assert state', function() { |
||||
|
(function() { |
||||
|
$.checkState(false, 'testing'); |
||||
|
}).should.throw(errors.InvalidState); |
||||
|
}); |
||||
|
it('throws no false negative', function() { |
||||
|
(function() { |
||||
|
$.checkState(true, 'testing'); |
||||
|
}).should.not.throw(); |
||||
|
}); |
||||
|
|
||||
|
it('can be used to check an argument', function() { |
||||
|
(function() { |
||||
|
$.checkArgument(false, 'testing'); |
||||
|
}).should.throw(errors.InvalidArgument); |
||||
|
|
||||
|
(function() { |
||||
|
$.checkArgument(true, 'testing'); |
||||
|
}).should.not.throw(errors.InvalidArgument); |
||||
|
}); |
||||
|
|
||||
|
it('can be used to check an argument type', function() { |
||||
|
var error; |
||||
|
try { |
||||
|
$.checkArgumentType(1, 'string', 'argumentName'); |
||||
|
} catch (e) { |
||||
|
error = e; |
||||
|
e.message.should.equal('Invalid Argument for argumentName, expected string but got number'); |
||||
|
} |
||||
|
error.should.exist(); |
||||
|
}); |
||||
|
it('has no false negatives when used to check an argument type', function() { |
||||
|
(function() { |
||||
|
$.checkArgumentType('a String', 'string', 'argumentName'); |
||||
|
}).should.not.throw(); |
||||
|
}); |
||||
|
|
||||
|
it('can be used to check an argument type for a class', function() { |
||||
|
var error; |
||||
|
try { |
||||
|
$.checkArgumentType(1, PrivateKey); |
||||
|
} catch (e) { |
||||
|
error = e; |
||||
|
e.message.should.equal('Invalid Argument for (unknown name), expected PrivateKey but got number'); |
||||
|
} |
||||
|
error.should.exist(); |
||||
|
}); |
||||
|
it('has no false negatives when checking a type for a class', function() { |
||||
|
(function() { |
||||
|
$.checkArgumentType(new PrivateKey(), PrivateKey); |
||||
|
}).should.not.throw(); |
||||
|
}); |
||||
|
}); |
Loading…
Reference in new issue