Merge pull request #1041 from eordano/unspent/fixZeroUndefined
UnspentOutput: Add tests to check for zero amount of satoshis
This commit is contained in:
commit
f882657c2e
|
@ -41,10 +41,12 @@ function UnspentOutput(data) {
|
||||||
if (!_.isNumber(outputIndex)) {
|
if (!_.isNumber(outputIndex)) {
|
||||||
throw new Error('Invalid outputIndex, received ' + 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);
|
var script = new Script(data.scriptPubKey || data.script);
|
||||||
$.checkArgument(!_.isUndefined(data.amount || data.satoshis), 'Must provide an amount for the output');
|
$.checkArgument(!_.isUndefined(data.amount) || !_.isUndefined(data.satoshis),
|
||||||
var amount = data.amount ? new Unit.fromBTC(data.amount).toSatoshis() : 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');
|
$.checkArgument(_.isNumber(amount), 'Amount must be a number');
|
||||||
JSUtil.defineImmutable(this, {
|
JSUtil.defineImmutable(this, {
|
||||||
address: address,
|
address: address,
|
||||||
|
|
|
@ -53,6 +53,30 @@ describe('UnspentOutput', function() {
|
||||||
expect(new UnspentOutput(sampleData1).inspect()).to.equal(expected);
|
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() {
|
it('toString returns txid:vout', function() {
|
||||||
var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0';
|
var expected = 'a477af6b2667c29670467e4e0728b685ee07b240235771862318e29ddbe58458:0';
|
||||||
expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);
|
expect(new UnspentOutput(sampleData1).toString()).to.equal(expected);
|
||||||
|
|
Loading…
Reference in New Issue