UNPKG

5.51 kBJavaScriptView Raw
1var Ajv = require("ajv");
2var abiSchema = require("../spec/abi.spec.json");
3var assert = require("assert");
4
5
6describe("ABI Schema", function() {
7 var validate;
8
9 beforeEach("reset validator", function() {
10 var ajv = new Ajv({ useDefaults: true });
11 validate = ajv.compile(abiSchema);
12 });
13
14 describe("event definition", function() {
15 it("validates with all fields valid", function() {
16 var abi = [{
17 "type": "event",
18 "name": "ButtonPressed",
19 "inputs": [{
20 "name": "button",
21 "type": "uint256",
22 "indexed": true
23 }],
24 "anonymous": false
25 }];
26
27 assert(validate(abi));
28 });
29
30 it("cannot omit type", function() {
31 var abi = [{
32 "name": "ButtonPressed",
33 "inputs": [{
34 "name": "button",
35 "type": "uint256",
36 "indexed": true
37 }],
38 "anonymous": false
39 }];
40
41 assert(!validate(abi));
42 });
43
44 it("cannot omit name", function() {
45 var abi = [{
46 "type": "event",
47 "inputs": [{
48 "name": "button",
49 "type": "uint256",
50 "indexed": false
51 }],
52 "anonymous": false
53 }];
54
55 assert(!validate(abi));
56 });
57
58 it("cannot omit inputs", function() {
59 var abi = [{
60 "type": "event",
61 "name": "ButtonPressed",
62 "anonymous": false
63 }];
64
65 assert(!validate(abi));
66 });
67
68 it("cannot omit anonymous", function() {
69 var abi = [{
70 "type": "event",
71 "name": "ButtonPressed",
72 "inputs": [{
73 "name": "button",
74 "type": "uint256",
75 "indexed": true
76 }]
77 }];
78
79 assert(!validate(abi));
80 });
81 });
82 describe("normal function definition", function() {
83 it("can omit type, outputs, constant, and payable", function() {
84 var abi = [{
85 "name": "press",
86 "inputs": [{
87 "name": "button",
88 "type": "uint256"
89 }],
90 "stateMutability": "nonpayable"
91 }];
92
93 assert(validate(abi));
94 assert.equal(abi[0].type, "function");
95 assert.equal(abi[0].stateMutability, "nonpayable");
96 assert.deepEqual(abi[0].outputs, []);
97 });
98
99 it("cannot omit name", function() {
100 var abi = [{
101 "type": "function",
102 "outputs": [],
103 "inputs": [],
104 "stateMutability": "nonpayable"
105 }];
106
107 assert(!validate(abi));
108 });
109
110 it("cannot omit inputs", function() {
111 var abi = [{
112 "name": "pressButton",
113 "type": "function",
114 "outputs": [],
115 "stateMutability": "nonpayable"
116 }];
117
118 assert(!validate(abi));
119 });
120 });
121
122 describe("constructor function definition", function() {
123 it("can omit constant, and payable", function() {
124 var abi = [{
125 "type": "constructor",
126 "inputs": [{
127 "name": "button",
128 "type": "uint256"
129 }],
130 "stateMutability": "nonpayable"
131 }];
132
133 assert(validate(abi));
134 assert.equal(abi[0].type, "constructor");
135 assert.equal(abi[0].stateMutability, "nonpayable");
136 assert.deepEqual(abi[0].outputs, []);
137 });
138
139 it("cannot include name", function() {
140 var abi = [{
141 "name": "ButtonPresser",
142 "type": "constructor",
143 "inputs": [{
144 "name": "button",
145 "type": "uint256"
146 }],
147 "stateMutability": "nonpayable"
148 }];
149
150 assert(!validate(abi));
151 });
152
153 it("cannot include outputs", function() {
154 var abi;
155
156 abi = [{
157 "type": "constructor",
158 "inputs": [{
159 "name": "button",
160 "type": "uint256"
161 }],
162 "outputs": [{
163 "name": "amount",
164 "type": "uint256"
165 }],
166 "stateMutability": "nonpayable"
167 }];
168 assert(!validate(abi));
169
170 abi = [{
171 "type": "constructor",
172 "inputs": [{
173 "name": "button",
174 "type": "uint256"
175 }],
176 "outputs": [],
177 "stateMutability": "nonpayable"
178 }];
179 assert(!validate(abi));
180 });
181
182 it("cannot omit inputs", function() {
183 var abi = [{
184 "type": "constructor",
185 "stateMutability": "nonpayable"
186 }];
187
188 assert(!validate(abi));
189 });
190 });
191
192 describe("fallback function definition", function() {
193 it("can omit constant and payable", function() {
194 var abi = [{
195 "type": "fallback",
196 "stateMutability": "nonpayable"
197 }];
198
199 var valid = validate(abi);
200 assert(valid);
201 assert.equal(abi[0].stateMutability, "nonpayable");
202 });
203
204 it("cannot include name", function() {
205 var abi = [{
206 "type": "fallback",
207 "name": "default",
208 "stateMutability": "nonpayable"
209 }];
210
211 assert(!validate(abi));
212 });
213
214 it("cannot include outputs", function() {
215 var abi;
216
217 abi = [{
218 "type": "fallback",
219 "stateMutability": "nonpayable",
220 "outputs": [{
221 "name": "amount",
222 "type": "uint256"
223 }]
224 }];
225 assert(!validate(abi));
226
227 abi = [{
228 "type": "fallback",
229 "stateMutability": "nonpayable",
230 "outputs": []
231 }];
232 assert(!validate(abi));
233 });
234
235 it("cannot include inputs", function() {
236 var abi = [{
237 "type": "fallback",
238 "stateMutability": "payable",
239 "inputs": [{
240 "name": "arg",
241 "type": "uint256"
242 }]
243 }];
244
245 assert(!validate(abi));
246 });
247 });
248});