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