UNPKG

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