add preconditions to opcode
This commit is contained in:
parent
3775d3c08a
commit
81f1469f9a
@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var _ = require('lodash');
|
||||
var $ = require('./util/preconditions');
|
||||
|
||||
function Opcode(num) {
|
||||
if (!(this instanceof Opcode)) {
|
||||
@ -19,11 +20,13 @@ function Opcode(num) {
|
||||
}
|
||||
|
||||
Opcode.prototype.set = function(obj) {
|
||||
$.checkArgument(_.isObject(obj));
|
||||
this.num = typeof obj.num !== 'undefined' ? obj.num : this.num;
|
||||
return this;
|
||||
};
|
||||
|
||||
Opcode.prototype.fromNumber = function(num) {
|
||||
$.checkArgument(_.isNumber(num));
|
||||
this.num = num;
|
||||
return this;
|
||||
};
|
||||
@ -33,9 +36,11 @@ Opcode.prototype.toNumber = function() {
|
||||
};
|
||||
|
||||
Opcode.prototype.fromString = function(str) {
|
||||
$.checkArgument(_.isString(str));
|
||||
var num = Opcode.map[str];
|
||||
if (typeof num === 'undefined')
|
||||
if (typeof num === 'undefined') {
|
||||
throw new Error('Invalid opcodestr');
|
||||
}
|
||||
this.num = num;
|
||||
return this;
|
||||
};
|
||||
@ -49,9 +54,7 @@ Opcode.prototype.toString = function() {
|
||||
};
|
||||
|
||||
Opcode.smallInt = function(n) {
|
||||
if (!(n >= 0 && n <= 16)) {
|
||||
throw new Error('Invalid Argument: n must be between 0 and 16');
|
||||
}
|
||||
$.checkArgument(n >= 0 && n <= 16, 'Invalid Argument: n must be between 0 and 16');
|
||||
if (n === 0) {
|
||||
return Opcode('OP_0');
|
||||
}
|
||||
|
||||
@ -25,52 +25,61 @@ describe('Opcode', function() {
|
||||
});
|
||||
|
||||
describe('#fromNumber', function() {
|
||||
|
||||
it('should work for 0', function() {
|
||||
Opcode().fromNumber(0).num.should.equal(0);
|
||||
});
|
||||
it('should fail for non-number', function() {
|
||||
Opcode().fromNumber.bind(null, 'a string').should.throw('Invalid Argument');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#set', function() {
|
||||
it('should work for object', function() {
|
||||
Opcode().set({
|
||||
num: 42
|
||||
}).num.should.equal(42);
|
||||
});
|
||||
it('should fail for non-object', function() {
|
||||
Opcode().set.bind(null, 'non-object').should.throw('Invalid Argument');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#toNumber', function() {
|
||||
|
||||
it('should work for 0', function() {
|
||||
Opcode().fromNumber(0).toNumber().should.equal(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#fromString', function() {
|
||||
|
||||
it('should work for OP_0', function() {
|
||||
Opcode().fromString('OP_0').num.should.equal(0);
|
||||
});
|
||||
|
||||
it('should fail for invalid string', function() {
|
||||
Opcode().fromString.bind(null, 'OP_SATOSHI').should.throw('Invalid opcodestr');
|
||||
Opcode().fromString.bind(null, 'BANANA').should.throw('Invalid opcodestr');
|
||||
});
|
||||
it('should fail for non-string', function() {
|
||||
Opcode().fromString.bind(null, 123).should.throw('Invalid Argument');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#toString', function() {
|
||||
|
||||
it('should work for OP_0', function() {
|
||||
Opcode().fromString('OP_0').toString().should.equal('OP_0');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('@map', function() {
|
||||
|
||||
it('should have a map containing 116 elements', function() {
|
||||
_.size(Opcode.map).should.equal(116);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('@reverseMap', function() {
|
||||
|
||||
it('should exist and have op 185', function() {
|
||||
should.exist(Opcode.reverseMap);
|
||||
Opcode.reverseMap[185].should.equal('OP_NOP10');
|
||||
});
|
||||
|
||||
});
|
||||
var smallints = [
|
||||
Opcode('OP_0'),
|
||||
|
||||
Loading…
Reference in New Issue
Block a user