@@ -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,9 +49,9 @@ 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 ) {
@@ -33,7 +61,8 @@ tape('Compilation', function (t) {
33
61
// Check if the ParserError exists, but allow others too
34
62
st . ok ( output . errors . length >= 1 ) ;
35
63
for ( var error in output . errors ) {
36
- if ( output . errors [ error ] . indexOf ( 'ParserError' ) !== - 1 ) {
64
+ // In early versions it was only displaying "Error: Expected identifier" as opposed to "ParserError"
65
+ if ( output . errors [ error ] . indexOf ( 'ParserError' ) !== - 1 || output . errors [ error ] . indexOf ( 'Error: Expected identifier' ) !== - 1 ) {
37
66
st . ok ( true ) ;
38
67
}
39
68
}
@@ -52,13 +81,12 @@ tape('Compilation', function (t) {
52
81
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
53
82
} ;
54
83
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 ) ;
84
+ var x = getBytecode ( output , 'cont.sol' , 'x' ) ;
85
+ st . ok ( x ) ;
86
+ st . ok ( x . length > 0 ) ;
87
+ var L = getBytecode ( output , 'lib.sol' , 'L' ) ;
88
+ st . ok ( L ) ;
89
+ st . ok ( L . length > 0 ) ;
62
90
st . end ( ) ;
63
91
} ) ;
64
92
@@ -80,13 +108,12 @@ tape('Compilation', function (t) {
80
108
}
81
109
}
82
110
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 ) ;
111
+ var x = getBytecode ( output , 'cont.sol' , 'x' ) ;
112
+ var L = getBytecode ( output , 'lib.sol' , 'L' ) ;
113
+ st . ok ( x ) ;
114
+ st . ok ( x . length > 0 ) ;
115
+ st . ok ( L ) ;
116
+ st . ok ( L . length > 0 ) ;
90
117
st . end ( ) ;
91
118
} ) ;
92
119
@@ -111,7 +138,8 @@ tape('Compilation', function (t) {
111
138
for ( var error in output . errors ) {
112
139
// Error should be something like:
113
140
// 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 ) {
141
+ // cont.sol:1:1: Error: Source "lib.sol" not found: File not found
142
+ if ( output . errors [ error ] . indexOf ( 'Error' ) !== - 1 && output . errors [ error ] . indexOf ( 'File not found' ) !== - 1 ) {
115
143
st . ok ( true ) ;
116
144
}
117
145
}
@@ -155,7 +183,8 @@ tape('Compilation', function (t) {
155
183
for ( var error in output . errors ) {
156
184
// Error should be something like:
157
185
// 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 ) {
186
+ // cont.sol:1:1: Error: Source "lib.sol" not found: File not supplied initially.
187
+ if ( output . errors [ error ] . indexOf ( 'Error' ) !== - 1 && output . errors [ error ] . indexOf ( 'File not supplied initially.' ) !== - 1 ) {
159
188
st . ok ( true ) ;
160
189
}
161
190
}
@@ -299,17 +328,13 @@ tape('Compilation', function (t) {
299
328
}
300
329
} ;
301
330
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
331
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' ) ) ;
332
+ var x = getBytecodeStandard ( output , 'cont.sol' , 'x' ) ;
333
+ st . ok ( x ) ;
334
+ st . ok ( x . length > 0 ) ;
335
+ var L = getBytecodeStandard ( output , 'lib.sol' , 'L' ) ;
336
+ st . ok ( L ) ;
337
+ st . ok ( L . length > 0 ) ;
313
338
st . end ( ) ;
314
339
} ) ;
315
340
} ) ;
@@ -334,11 +359,10 @@ tape('Linking', function (t) {
334
359
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
335
360
} ;
336
361
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' } ) ;
362
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
363
+ st . ok ( bytecode ) ;
364
+ st . ok ( bytecode . length > 0 ) ;
365
+ bytecode = solc . linkBytecode ( bytecode , { 'lib.sol:L' : '0x123456' } ) ;
342
366
st . ok ( bytecode . indexOf ( '_' ) < 0 ) ;
343
367
st . end ( ) ;
344
368
} ) ;
@@ -349,11 +373,10 @@ tape('Linking', function (t) {
349
373
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
350
374
} ;
351
375
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 , { } ) ;
376
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
377
+ st . ok ( bytecode ) ;
378
+ st . ok ( bytecode . length > 0 ) ;
379
+ bytecode = solc . linkBytecode ( bytecode , { } ) ;
357
380
st . ok ( bytecode . indexOf ( '_' ) >= 0 ) ;
358
381
st . end ( ) ;
359
382
} ) ;
@@ -364,12 +387,11 @@ tape('Linking', function (t) {
364
387
'cont.sol' : 'import "lib.sol"; contract x { function g() { L.f(); } }'
365
388
} ;
366
389
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 ) ;
390
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
391
+ st . ok ( bytecode ) ;
392
+ st . ok ( bytecode . length > 0 ) ;
371
393
st . throws ( function ( ) {
372
- solc . linkBytecode ( output . contracts [ 'cont.sol:x' ] . bytecode , { 'lib.sol:L' : '' } ) ;
394
+ solc . linkBytecode ( bytecode , { 'lib.sol:L' : '' } ) ;
373
395
} ) ;
374
396
st . end ( ) ;
375
397
} ) ;
@@ -380,11 +402,10 @@ tape('Linking', function (t) {
380
402
'cont.sol' : 'import "lib.sol"; contract x { function g() { L1234567890123456789012345678901234567890.f(); } }'
381
403
} ;
382
404
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' } ) ;
405
+ var bytecode = getBytecode ( output , 'cont.sol' , 'x' ) ;
406
+ st . ok ( bytecode ) ;
407
+ st . ok ( bytecode . length > 0 ) ;
408
+ bytecode = solc . linkBytecode ( bytecode , { 'lib.sol:L1234567890123456789012345678901234567890' : '0x123456' } ) ;
388
409
st . ok ( bytecode . indexOf ( '_' ) < 0 ) ;
389
410
st . end ( ) ;
390
411
} ) ;
0 commit comments