Skip to content

Commit 1169877

Browse files
committed
Support running compiler unit tests on older (read: <0.4.9) compilers
1 parent 640e98d commit 1169877

File tree

1 file changed

+71
-50
lines changed

1 file changed

+71
-50
lines changed

test/package.js

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@ const tape = require('tape');
22
const semver = require('semver');
33
const solc = require('../index.js');
44

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+
533
tape('Version and license', function (t) {
634
t.test('check version', function (st) {
735
st.equal(typeof solc.version(), 'string');
@@ -21,9 +49,9 @@ tape('Compilation', function (t) {
2149
t.test('single files can be compiled', function (st) {
2250
var output = solc.compile('contract x { function g() {} }');
2351
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);
2755
st.end();
2856
});
2957
t.test('invalid source code fails properly', function (st) {
@@ -33,7 +61,8 @@ tape('Compilation', function (t) {
3361
// Check if the ParserError exists, but allow others too
3462
st.ok(output.errors.length >= 1);
3563
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) {
3766
st.ok(true);
3867
}
3968
}
@@ -52,13 +81,12 @@ tape('Compilation', function (t) {
5281
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
5382
};
5483
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);
6290
st.end();
6391
});
6492

@@ -80,13 +108,12 @@ tape('Compilation', function (t) {
80108
}
81109
}
82110
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);
90117
st.end();
91118
});
92119

@@ -111,7 +138,8 @@ tape('Compilation', function (t) {
111138
for (var error in output.errors) {
112139
// Error should be something like:
113140
// 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) {
115143
st.ok(true);
116144
}
117145
}
@@ -155,7 +183,8 @@ tape('Compilation', function (t) {
155183
for (var error in output.errors) {
156184
// Error should be something like:
157185
// 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) {
159188
st.ok(true);
160189
}
161190
}
@@ -299,17 +328,13 @@ tape('Compilation', function (t) {
299328
}
300329
};
301330

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-
310331
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);
313338
st.end();
314339
});
315340
});
@@ -334,11 +359,10 @@ tape('Linking', function (t) {
334359
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
335360
};
336361
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' });
342366
st.ok(bytecode.indexOf('_') < 0);
343367
st.end();
344368
});
@@ -349,11 +373,10 @@ tape('Linking', function (t) {
349373
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
350374
};
351375
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, { });
357380
st.ok(bytecode.indexOf('_') >= 0);
358381
st.end();
359382
});
@@ -364,12 +387,11 @@ tape('Linking', function (t) {
364387
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
365388
};
366389
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);
371393
st.throws(function () {
372-
solc.linkBytecode(output.contracts['cont.sol:x'].bytecode, { 'lib.sol:L': '' });
394+
solc.linkBytecode(bytecode, { 'lib.sol:L': '' });
373395
});
374396
st.end();
375397
});
@@ -380,11 +402,10 @@ tape('Linking', function (t) {
380402
'cont.sol': 'import "lib.sol"; contract x { function g() { L1234567890123456789012345678901234567890.f(); } }'
381403
};
382404
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' });
388409
st.ok(bytecode.indexOf('_') < 0);
389410
st.end();
390411
});

0 commit comments

Comments
 (0)