Merge pull request #1041 from eordano/unspent/fixZeroUndefined

UnspentOutput: Add tests to check for zero amount of satoshis
This commit is contained in:
Manuel Aráoz 2015-02-05 13:53:17 -03:00
commit f882657c2e
2 changed files with 29 additions and 3 deletions

View File

@ -41,10 +41,12 @@ function UnspentOutput(data) {
if (!_.isNumber(outputIndex)) {
throw new Error('Invalid outputIndex, received ' + outputIndex);
}
$.checkArgument(!_.isUndefined(data.scriptPubKey || data.script), 'Must provide the scriptPubKey for that output!');
$.checkArgument(!_.isUndefined(data.scriptPubKey) || !_.isUndefined(data.script),
'Must provide the scriptPubKey for that output!');
var script = new Script(data.scriptPubKey || data.script);
$.checkArgument(!_.isUndefined(data.amount || data.satoshis), 'Must provide an amount for the output');
var amount = data.amount ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis;
$.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis),
'Must provide an amount for the output');
var amount = !_.isUndefined(data.amount) ? new Unit.fromBTC(data.amount).toSatoshis() : data.satoshis;
$.checkArgument(_.isNumber(amount), 'Amount must be a number');
JSUtil.defineImmutable(this, {
address: address,

View File

@ -53,6 +53,30 @@ describe('UnspentOutput', function() {
expect(new UnspentOutput(sampleData1).inspect()).to.equal(expected);
});
describe('checking the constructor parameters', function() {
var notDefined = {
'txId': 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
'outputIndex': 0,
'script': 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG',
};
var zero = {
'txId': 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458',
'outputIndex': 0,
'script': 'OP_DUP OP_HASH160 20 0x88d9931ea73d60eaf7e5671efc0552b912911f2a OP_EQUALVERIFY OP_CHECKSIG',
'amount': 0
};
it('fails when no amount is defined', function() {
expect(function() {
return new UnspentOutput(notDefined);
}).to.throw('Must provide an amount for the output');
});
it('does not fail when amount is zero', function() {
expect(function() {
return new UnspentOutput(zero);
}).to.not.throw();
});
});
it('toString returns txid:vout', function() {
var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0';
expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);