UNPKG

4.83 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('lodash');
4var chai = require('chai');
5var should = chai.should();
6var expect = chai.expect;
7var bitcore = require('..');
8var Opcode = bitcore.Opcode;
9
10describe('Opcode', function() {
11
12 it('should create a new Opcode', function() {
13 var opcode = new Opcode(5);
14 should.exist(opcode);
15 });
16
17 it('should convert to a string with this handy syntax', function() {
18 Opcode(0).toString().should.equal('OP_0');
19 Opcode(96).toString().should.equal('OP_16');
20 Opcode(97).toString().should.equal('OP_NOP');
21 });
22
23 it('should convert to a number with this handy syntax', function() {
24 Opcode('OP_0').toNumber().should.equal(0);
25 Opcode('OP_16').toNumber().should.equal(96);
26 Opcode('OP_NOP').toNumber().should.equal(97);
27 });
28
29 describe('#fromNumber', function() {
30 it('should work for 0', function() {
31 Opcode.fromNumber(0).num.should.equal(0);
32 });
33 it('should fail for non-number', function() {
34 Opcode.fromNumber.bind(null, 'a string').should.throw('Invalid Argument');
35 });
36 });
37
38 describe('#set', function() {
39 it('should work for object', function() {
40 Opcode(42).num.should.equal(42);
41 });
42 it('should fail for empty-object', function() {
43 expect(function() {
44 Opcode();
45 }).to.throw(TypeError);
46 });
47 });
48
49 describe('#toNumber', function() {
50 it('should work for 0', function() {
51 Opcode.fromNumber(0).toNumber().should.equal(0);
52 });
53 });
54
55 describe('#buffer', function() {
56 it('should correctly input/output a buffer', function() {
57 var buf = Buffer.from('a6', 'hex');
58 Opcode.fromBuffer(buf).toBuffer().should.deep.equal(buf);
59 });
60 });
61
62 describe('#fromString', function() {
63 it('should work for OP_0', function() {
64 Opcode.fromString('OP_0').num.should.equal(0);
65 });
66 it('should fail for invalid string', function() {
67 Opcode.fromString.bind(null, 'OP_SATOSHI').should.throw('Invalid opcodestr');
68 Opcode.fromString.bind(null, 'BANANA').should.throw('Invalid opcodestr');
69 });
70 it('should fail for non-string', function() {
71 Opcode.fromString.bind(null, 123).should.throw('Invalid Argument');
72 });
73 });
74
75 describe('#toString', function() {
76 it('should work for OP_0', function() {
77 Opcode.fromString('OP_0').toString().should.equal('OP_0');
78 });
79
80 it('should not work for non-opcode', function() {
81 expect(function(){
82 Opcode('OP_NOTACODE').toString();
83 }).to.throw('Opcode does not have a string representation');
84 });
85 });
86
87 describe('@map', function() {
88 it('should have a map containing 118 elements', function() {
89 _.size(Opcode.map).should.equal(118);
90 });
91 });
92
93 describe('@reverseMap', function() {
94 it('should exist and have op 185', function() {
95 should.exist(Opcode.reverseMap);
96 Opcode.reverseMap[185].should.equal('OP_NOP10');
97 });
98 });
99 var smallints = [
100 Opcode('OP_0'),
101 Opcode('OP_1'),
102 Opcode('OP_2'),
103 Opcode('OP_3'),
104 Opcode('OP_4'),
105 Opcode('OP_5'),
106 Opcode('OP_6'),
107 Opcode('OP_7'),
108 Opcode('OP_8'),
109 Opcode('OP_9'),
110 Opcode('OP_10'),
111 Opcode('OP_11'),
112 Opcode('OP_12'),
113 Opcode('OP_13'),
114 Opcode('OP_14'),
115 Opcode('OP_15'),
116 Opcode('OP_16')
117 ];
118
119 describe('@smallInt', function() {
120 var testSmallInt = function(n, op) {
121 Opcode.smallInt(n).toString().should.equal(op.toString());
122 };
123
124 for (var i = 0; i < smallints.length; i++) {
125 var op = smallints[i];
126 it('should work for small int ' + op, testSmallInt.bind(null, i, op));
127 }
128
129 it('with not number', function () {
130 Opcode.smallInt.bind(null, '2').should.throw('Invalid Argument');
131 });
132
133 it('with n equal -1', function () {
134 Opcode.smallInt.bind(null, -1).should.throw('Invalid Argument');
135 });
136
137 it('with n equal 17', function () {
138 Opcode.smallInt.bind(null, 17).should.throw('Invalid Argument');
139 });
140 });
141 describe('@isSmallIntOp', function() {
142 var testIsSmallInt = function(op) {
143 Opcode.isSmallIntOp(op).should.equal(true);
144 };
145 for (var i = 0; i < smallints.length; i++) {
146 var op = smallints[i];
147 it('should work for small int ' + op, testIsSmallInt.bind(null, op));
148 }
149
150 it('should work for non-small ints', function() {
151 Opcode.isSmallIntOp(Opcode('OP_RETURN')).should.equal(false);
152 Opcode.isSmallIntOp(Opcode('OP_CHECKSIG')).should.equal(false);
153 Opcode.isSmallIntOp(Opcode('OP_IF')).should.equal(false);
154 Opcode.isSmallIntOp(Opcode('OP_NOP')).should.equal(false);
155 });
156
157 });
158
159 describe('#inspect', function() {
160 it('should output opcode by name, hex, and decimal', function() {
161 Opcode.fromString('OP_NOP').inspect().should.equal('<Opcode: OP_NOP, hex: 61, decimal: 97>');
162 });
163 });
164
165});