@@ -2,6 +2,34 @@ const tape = require('tape');
2
2
const semver = require ( 'semver' ) ;
3
3
const solc = require ( '../index.js' ) ;
4
4
5
+ function getBytecode ( output , fileName , contractName ) {
6
+ try {
7
+ var outputContract ;
8
+ if ( semver . lt ( solc . semver ( ) , '0.4.9' ) ) {
9
+ outputContract = output . contracts [ contractName ] ;
10
+ } else {
11
+ outputContract = output . contracts [ fileName + ':' + contractName ] ;
12
+ }
13
+ return outputContract [ 'bytecode' ] ;
14
+ } catch ( e ) {
15
+ return '' ;
16
+ }
17
+ }
18
+
19
+ function getBytecodeStandard ( output , fileName , contractName ) {
20
+ try {
21
+ var outputFile ;
22
+ if ( semver . lt ( solc . semver ( ) , '0.4.9' ) ) {
23
+ outputFile = output . contracts [ '' ] ;
24
+ } else {
25
+ outputFile = output . contracts [ fileName ] ;
26
+ }
27
+ return outputFile [ contractName ] [ 'evm' ] [ 'bytecode' ] [ 'object' ] ;
28
+ } catch ( e ) {
29
+ return '' ;
30
+ }
31
+ }
32
+
5
33
tape ( 'Version and license' , function ( t ) {
6
34
t . test ( 'check version' , function ( st ) {
7
35
st . equal ( typeof solc . version ( ) , 'string' ) ;
@@ -21,19 +49,32 @@ tape('Compilation', function (t) {
21
49
t . test ( 'single files can be compiled' , function ( st ) {
22
50
var output = solc . compile ( 'contract x { function g() {} }' ) ;
23
51
st . ok ( 'contracts' in output ) ;
24
- st . ok ( ':x' in output . contracts ) ;
25
- st . ok ( ' bytecode' in output . contracts [ ':x' ] ) ;
26
- st . ok ( output . contracts [ ':x' ] . bytecode . length > 0 ) ;
52
+ var bytecode = getBytecode ( output , '' , 'x' ) ;
53
+ st . ok ( bytecode ) ;
54
+ st . ok ( bytecode . length > 0 ) ;
27
55
st . end ( ) ;
28
56
} ) ;
29
57
t . test ( 'invalid source code fails properly' , function ( st ) {
30
58
var output = solc . compile ( 'contract x { this is an invalid contract }' ) ;
59
+ if ( semver . lt ( solc . semver ( ) , '0.1.4' ) ) {
60
+ st . ok ( output . error . indexOf ( 'Parser error: Expected identifier' ) !== - 1 ) ;
61
+ st . end ( ) ;
62
+ return ;
63
+ }
31
64
st . plan ( 3 ) ;
32
65
st . ok ( 'errors' in output ) ;
33
66
// Check if the ParserError exists, but allow others too
34
67
st . ok ( output . errors . length >= 1 ) ;
35
68
for ( var error in output . errors ) {
36
- if ( output . errors [ error ] . indexOf ( 'ParserError' ) !== - 1 ) {
69
+ // Error should be something like:
70
+ // ParserError
71
+ // Error: Expected identifier
72
+ // Parser error: Expected identifier
73
+ if (
74
+ output . errors [ error ] . indexOf ( 'ParserError' ) !== - 1 ||
75
+ output . errors [ error ] . indexOf ( 'Error: Expected identifier' ) !== - 1 ||
76
+ output . errors [ error ] . indexOf ( 'Parser error: Expected identifier' ) !== - 1
77
+ ) {
37
78
st . ok ( true ) ;
38
79
}
39
80
}
@@ -52,13 +93,12 @@ tape('Compilation', function (t) {
52
93
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
53
94
} ;
54
95
var output = solc . compile ( { sources : input } ) ;
55
- st . ok ( 'contracts' in output ) ;
56
- st . ok ( 'cont.sol:x' in output . contracts ) ;
57
- st . ok ( 'lib.sol:L' in output . contracts ) ;
58
- st . ok ( 'bytecode' in output . contracts [ 'cont.sol:x' ] ) ;
59
- st . ok ( 'bytecode' in output . contracts [ 'lib.sol:L' ] ) ;
60
- st . ok ( output . contracts [ 'cont.sol:x' ] . bytecode . length > 0 ) ;
61
- st . ok ( output . contracts [ 'lib.sol:L' ] . bytecode . length > 0 ) ;
96
+ var x = getBytecode ( output , 'cont.sol' , 'x' ) ;
97
+ st . ok ( x ) ;
98
+ st . ok ( x . length > 0 ) ;
99
+ var L = getBytecode ( output , 'lib.sol' , 'L' ) ;
100
+ st . ok ( L ) ;
101
+ st . ok ( L . length > 0 ) ;
62
102
st . end ( ) ;
63
103
} ) ;
64
104
@@ -80,13 +120,12 @@ tape('Compilation', function (t) {
80
120
}
81
121
}
82
122
var output = solc . compile ( { sources : input } , 0 , findImports ) ;
83
- st . ok ( 'contracts' in output ) ;
84
- st . ok ( 'cont.sol:x' in output . contracts ) ;
85
- st . ok ( 'lib.sol:L' in output . contracts ) ;
86
- st . ok ( 'bytecode' in output . contracts [ 'cont.sol:x' ] ) ;
87
- st . ok ( 'bytecode' in output . contracts [ 'lib.sol:L' ] ) ;
88
- st . ok ( output . contracts [ 'cont.sol:x' ] . bytecode . length > 0 ) ;
89
- st . ok ( output . contracts [ 'lib.sol:L' ] . bytecode . length > 0 ) ;
123
+ var x = getBytecode ( output , 'cont.sol' , 'x' ) ;
124
+ var L = getBytecode ( output , 'lib.sol' , 'L' ) ;
125
+ st . ok ( x ) ;
126
+ st . ok ( x . length > 0 ) ;
127
+ st . ok ( L ) ;
128
+ st . ok ( L . length > 0 ) ;
90
129
st . end ( ) ;
91
130
} ) ;
92
131
@@ -111,7 +150,8 @@ tape('Compilation', function (t) {
111
150
for ( var error in output . errors ) {
112
151
// Error should be something like:
113
152
// cont.sol:1:1: ParserError: Source "lib.sol" not found: File not found
114
- if ( output . errors [ error ] . indexOf ( 'ParserError' ) !== - 1 && output . errors [ error ] . indexOf ( 'File not found' ) !== - 1 ) {
153
+ // cont.sol:1:1: Error: Source "lib.sol" not found: File not found
154
+ if ( output . errors [ error ] . indexOf ( 'Error' ) !== - 1 && output . errors [ error ] . indexOf ( 'File not found' ) !== - 1 ) {
115
155
st . ok ( true ) ;
116
156
}
117
157
}
@@ -155,7 +195,8 @@ tape('Compilation', function (t) {
155
195
for ( var error in output . errors ) {
156
196
// Error should be something like:
157
197
// cont.sol:1:1: ParserError: Source "lib.sol" not found: File not supplied initially.
158
- if ( output . errors [ error ] . indexOf ( 'ParserError' ) !== - 1 && output . errors [ error ] . indexOf ( 'File not supplied initially.' ) !== - 1 ) {
198
+ // cont.sol:1:1: Error: Source "lib.sol" not found: File not supplied initially.
199
+ if ( output . errors [ error ] . indexOf ( 'Error' ) !== - 1 && output . errors [ error ] . indexOf ( 'File not supplied initially.' ) !== - 1 ) {
159
200
st . ok ( true ) ;
160
201
}
161
202
}
@@ -280,6 +321,14 @@ tape('Compilation', function (t) {
280
321
st . end ( ) ;
281
322
} ) ;
282
323
t . test ( 'compiling standard JSON (using wrapper)' , function ( st ) {
324
+ // Example needs support for compileJSONMulti
325
+ // FIXME: add test for wrapper without multiple files
326
+ if ( semver . lt ( solc . semver ( ) , '0.1.6' ) ) {
327
+ st . skip ( 'Not supported by solc <0.1.6' ) ;
328
+ st . end ( ) ;
329
+ return ;
330
+ }
331
+
283
332
var input = {
284
333
'language' : 'Solidity' ,
285
334
'settings' : {
@@ -299,17 +348,13 @@ tape('Compilation', function (t) {
299
348
}
300
349
} ;
301
350
302
- function bytecodeExists ( output , fileName , contractName ) {
303
- try {
304
- return output . contracts [ fileName ] [ contractName ] [ 'evm' ] [ 'bytecode' ] [ 'object' ] . length > 0 ;
305
- } catch ( e ) {
306
- return false ;
307
- }
308
- }
309
-
310
351
var output = JSON . parse ( solc . compileStandardWrapper ( JSON . stringify ( input ) ) ) ;
311
- st . ok ( bytecodeExists ( output , 'cont.sol' , 'x' ) ) ;
312
- st . ok ( bytecodeExists ( output , 'lib.sol' , 'L' ) ) ;
352
+ var x = getBytecodeStandard ( output , 'cont.sol' , 'x' ) ;
353
+ st . ok ( x ) ;
354
+ st . ok ( x . length > 0 ) ;
355
+ var L = getBytecodeStandard ( output , 'lib.sol' , 'L' ) ;
356
+ st . ok ( L ) ;
357
+ st . ok ( L . length > 0 ) ;
313
358
st . end ( ) ;
314
359
} ) ;
315
360
} ) ;
@@ -328,17 +373,24 @@ tape('Loading Legacy Versions', function (t) {
328
373
} ) ;
329
374
330
375
tape ( 'Linking' , function ( t ) {
376
+ // FIXME: all the linking tests require compileJSONMulti support,
377
+ // create test cases which have all files in a single source and could run with 0.1.3
378
+ if ( semver . lt ( solc . semver ( ) , '0.1.6' ) ) {
379
+ t . skip ( 'Not supported by solc <0.1.6' ) ;
380
+ t . end ( ) ;
381
+ return ;
382
+ }
383
+
331
384
t . test ( 'link properly' , function ( st ) {
332
385
var input = {
333
386
'lib.sol' : 'library L { function f() returns (uint) { return 7; } }' ,
334
387
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
335
388
} ;
336
389
var output = solc . compile ( { sources : input } ) ;
337
- st . ok ( 'contracts' in output ) ;
338
- st . ok ( 'cont.sol:x' in output . contracts ) ;
339
- st . ok ( 'bytecode' in output . contracts [ 'cont.sol:x' ] ) ;
340
- st . ok ( output . contracts [ 'cont.sol:x' ] . bytecode . length > 0 ) ;
341
- var bytecode = solc . linkBytecode ( output . contracts [ 'cont.sol:x' ] . bytecode , { 'lib.sol:L' : '0x123456' } ) ;
390
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
391
+ st . ok ( bytecode ) ;
392
+ st . ok ( bytecode . length > 0 ) ;
393
+ bytecode = solc . linkBytecode ( bytecode , { 'lib.sol:L' : '0x123456' } ) ;
342
394
st . ok ( bytecode . indexOf ( '_' ) < 0 ) ;
343
395
st . end ( ) ;
344
396
} ) ;
@@ -349,11 +401,10 @@ tape('Linking', function (t) {
349
401
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
350
402
} ;
351
403
var output = solc . compile ( { sources : input } ) ;
352
- st . ok ( 'contracts' in output ) ;
353
- st . ok ( 'cont.sol:x' in output . contracts ) ;
354
- st . ok ( 'bytecode' in output . contracts [ 'cont.sol:x' ] ) ;
355
- st . ok ( output . contracts [ 'cont.sol:x' ] . bytecode . length > 0 ) ;
356
- var bytecode = solc . linkBytecode ( output . contracts [ 'cont.sol:x' ] . bytecode , { } ) ;
404
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
405
+ st . ok ( bytecode ) ;
406
+ st . ok ( bytecode . length > 0 ) ;
407
+ bytecode = solc . linkBytecode ( bytecode , { } ) ;
357
408
st . ok ( bytecode . indexOf ( '_' ) >= 0 ) ;
358
409
st . end ( ) ;
359
410
} ) ;
@@ -364,12 +415,11 @@ tape('Linking', function (t) {
364
415
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
365
416
} ;
366
417
var output = solc . compile ( { sources : input } ) ;
367
- st . ok ( 'contracts' in output ) ;
368
- st . ok ( 'cont.sol:x' in output . contracts ) ;
369
- st . ok ( 'bytecode' in output . contracts [ 'cont.sol:x' ] ) ;
370
- st . ok ( output . contracts [ 'cont.sol:x' ] . bytecode . length > 0 ) ;
418
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
419
+ st . ok ( bytecode ) ;
420
+ st . ok ( bytecode . length > 0 ) ;
371
421
st . throws ( function ( ) {
372
- solc . linkBytecode ( output . contracts [ 'cont.sol:x' ] . bytecode , { 'lib.sol:L' : '' } ) ;
422
+ solc . linkBytecode ( bytecode , { 'lib.sol:L' : '' } ) ;
373
423
} ) ;
374
424
st . end ( ) ;
375
425
} ) ;
@@ -380,11 +430,10 @@ tape('Linking', function (t) {
380
430
'cont.sol' : 'import "lib.sol"; contract x { function g() { L1234567890123456789012345678901234567890.f(); } }'
381
431
} ;
382
432
var output = solc . compile ( { sources : input } ) ;
383
- st . ok ( 'contracts' in output ) ;
384
- st . ok ( 'cont.sol:x' in output . contracts ) ;
385
- st . ok ( 'bytecode' in output . contracts [ 'cont.sol:x' ] ) ;
386
- st . ok ( output . contracts [ 'cont.sol:x' ] . bytecode . length > 0 ) ;
387
- var bytecode = solc . linkBytecode ( output . contracts [ 'cont.sol:x' ] . bytecode , { 'lib.sol:L1234567890123456789012345678901234567890' : '0x123456' } ) ;
433
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
434
+ st . ok ( bytecode ) ;
435
+ st . ok ( bytecode . length > 0 ) ;
436
+ bytecode = solc . linkBytecode ( bytecode , { 'lib.sol:L1234567890123456789012345678901234567890' : '0x123456' } ) ;
388
437
st . ok ( bytecode . indexOf ( '_' ) < 0 ) ;
389
438
st . end ( ) ;
390
439
} ) ;
0 commit comments