Added `checkState` for positive integer in Output satoshis

This commit is contained in:
Braydon Fuller 2015-04-10 18:30:05 -04:00
parent 3005e19cbf
commit bcd4efb724
2 changed files with 51 additions and 0 deletions

View File

@ -7,6 +7,7 @@ var bufferUtil = require('../util/buffer');
var JSUtil = require('../util/js');
var BufferWriter = require('../encoding/bufferwriter');
var Script = require('../script');
var $ = require('../util/preconditions');
function Output(params) {
if (!(this instanceof Output)) {
@ -50,6 +51,10 @@ Object.defineProperty(Output.prototype, 'satoshis', {
this._satoshisBN = BN.fromNumber(num);
this._satoshis = num;
}
$.checkState(
JSUtil.isPositiveInteger(this._satoshis),
'Output satoshis is not a positive integer'
);
}
});

View File

@ -30,6 +30,52 @@ describe('Output', function() {
newOutput.satoshis.should.equal(100);
});
it('can be assigned a satoshi amount with a string', function() {
var newOutput = new Output({
satoshis: '100',
script: Script.empty()
});
newOutput.satoshis.should.equal(100);
});
describe('will error if output is not a positive integer', function() {
it('-100', function() {
(function() {
var newOutput = new Output({
satoshis: -100,
script: Script.empty()
});
}).should.throw('Output satoshis is not a positive integer');
});
it('1.1', function() {
(function() {
var newOutput = new Output({
satoshis: 1.1,
script: Script.empty()
});
}).should.throw('Output satoshis is not a positive integer');
});
it('NaN', function() {
(function() {
var newOutput = new Output({
satoshis: NaN,
script: Script.empty()
});
}).should.throw('Output satoshis is not a positive integer');
});
it('Infinity', function() {
(function() {
var newOutput = new Output({
satoshis: Infinity,
script: Script.empty()
});
}).should.throw('Output satoshis is not a positive integer');
});
});
var expectEqualOutputs = function(a, b) {
a.satoshis.should.equal(b.satoshis);
a.script.toString().should.equal(b.script.toString());