Skip to content

Commit 31fa64f

Browse files
author
Leonardo
authored
Merge pull request #419 from ethereum/moar-tests
Increase code coverage for older compiler versions
2 parents df26987 + 7077564 commit 31fa64f

File tree

3 files changed

+110
-7
lines changed

3 files changed

+110
-7
lines changed

linker.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
var assert = require('assert');
12
var keccak256 = require('js-sha3').keccak256;
23

34
function libraryHashPlaceholder (input) {
45
return '$' + keccak256(input).slice(0, 34) + '$';
56
}
67

78
var linkBytecode = function (bytecode, libraries) {
9+
assert(typeof bytecode === 'string');
10+
assert(typeof libraries === 'object');
811
// NOTE: for backwards compatibility support old compiler which didn't use file names
912
var librariesComplete = {};
1013
for (var libraryName in libraries) {
@@ -52,6 +55,7 @@ var linkBytecode = function (bytecode, libraries) {
5255
};
5356

5457
var findLinkReferences = function (bytecode) {
58+
assert(typeof bytecode === 'string');
5559
// find 40 bytes in the pattern of __...<36 digits>...__
5660
// e.g. __Lib.sol:L_____________________________
5761
var linkReferences = {};

test/compiler.js

+102-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ function runTests (solc, versionText) {
5555
error = output.errors[error];
5656
if (error.type === errorType) {
5757
if (message) {
58-
return error.message.match(message) !== null;
58+
if (error.message.match(message) !== null) {
59+
return true;
60+
}
61+
} else {
62+
return true;
5963
}
60-
return true;
6164
}
6265
}
6366
}
@@ -421,7 +424,40 @@ function runTests (solc, versionText) {
421424
st.end();
422425
});
423426

424-
t.test('compiling standard JSON', function (st) {
427+
t.test('compiling standard JSON (single file)', function (st) {
428+
var input = {
429+
'language': 'Solidity',
430+
'settings': {
431+
'outputSelection': {
432+
'*': {
433+
'*': [ 'evm.bytecode', 'evm.gasEstimates' ]
434+
}
435+
}
436+
},
437+
'sources': {
438+
'c.sol': {
439+
'content': 'contract C { function g() public { } function h() internal {} }'
440+
}
441+
}
442+
};
443+
444+
var output = JSON.parse(solc.compile(JSON.stringify(input)));
445+
st.ok(expectNoError(output));
446+
var C = getBytecodeStandard(output, 'c.sol', 'C');
447+
st.ok(typeof C === 'string');
448+
st.ok(C.length > 0);
449+
var CGas = getGasEstimate(output, 'c.sol', 'C');
450+
st.ok(typeof CGas === 'object');
451+
st.ok(typeof CGas['creation'] === 'object');
452+
st.ok(typeof CGas['creation']['codeDepositCost'] === 'string');
453+
st.ok(typeof CGas['external'] === 'object');
454+
st.ok(typeof CGas['external']['g()'] === 'string');
455+
st.ok(typeof CGas['internal'] === 'object');
456+
st.ok(typeof CGas['internal']['h()'] === 'string');
457+
st.end();
458+
});
459+
460+
t.test('compiling standard JSON (multiple files)', function (st) {
425461
// <0.1.6 doesn't have this
426462
if (!solc.features.multipleInputs) {
427463
st.skip('Not supported by solc');
@@ -468,6 +504,40 @@ function runTests (solc, versionText) {
468504
st.end();
469505
});
470506

507+
t.test('compiling standard JSON (abstract contract)', function (st) {
508+
// <0.1.6 doesn't have this
509+
if (!solc.features.multipleInputs) {
510+
st.skip('Not supported by solc');
511+
st.end();
512+
return;
513+
}
514+
515+
var isVersion6 = semver.gt(solc.semver(), '0.5.99');
516+
517+
var input = {
518+
'language': 'Solidity',
519+
'settings': {
520+
'outputSelection': {
521+
'*': {
522+
'*': [ 'evm.bytecode', 'evm.gasEstimates' ]
523+
}
524+
}
525+
},
526+
'sources': {
527+
'c.sol': {
528+
'content': (isVersion6 ? 'abstract ' : '') + 'contract C { function f() public; }'
529+
}
530+
}
531+
};
532+
533+
var output = JSON.parse(solc.compile(JSON.stringify(input)));
534+
st.ok(expectNoError(output));
535+
var C = getBytecodeStandard(output, 'c.sol', 'C');
536+
st.ok(typeof C === 'string');
537+
st.ok(C.length === 0);
538+
st.end();
539+
});
540+
471541
t.test('compiling standard JSON (with imports)', function (st) {
472542
// <0.2.1 doesn't have this
473543
if (!solc.features.importCallback) {
@@ -606,6 +676,35 @@ function runTests (solc, versionText) {
606676
st.end();
607677
});
608678

679+
t.test('compiling standard JSON (with warning >=0.4.0)', function (st) {
680+
// In 0.4.0 "pragma solidity" was added. Not including it is a warning.
681+
if (semver.lt(solc.semver(), '0.4.0')) {
682+
st.skip('Not supported by solc');
683+
st.end();
684+
return;
685+
}
686+
687+
var input = {
688+
'language': 'Solidity',
689+
'settings': {
690+
'outputSelection': {
691+
'*': {
692+
'*': [ 'evm.bytecode' ]
693+
}
694+
}
695+
},
696+
'sources': {
697+
'c.sol': {
698+
'content': 'contract C { function f() public { } }'
699+
}
700+
}
701+
};
702+
703+
var output = JSON.parse(solc.compile(JSON.stringify(input)));
704+
st.ok(expectError(output, 'Warning', 'Source file does not specify required compiler version!'));
705+
st.end();
706+
});
707+
609708
t.test('compiling standard JSON (using libraries) (using lowlevel API)', function (st) {
610709
// 0.4.0 has a bug with libraries
611710
if (semver.eq(solc.semver(), '0.4.0')) {

translate.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ function translateJsonCompilerOutput (output, libraries) {
110110
'evm': {
111111
'legacyAssembly': contractInput['assembly'],
112112
'bytecode': {
113-
'object': linker.linkBytecode(contractInput['bytecode'], libraries),
113+
'object': contractInput['bytecode'] && linker.linkBytecode(contractInput['bytecode'], libraries || {}),
114114
'opcodes': contractInput['opcodes'],
115115
'sourceMap': contractInput['srcmap'],
116-
'linkReferences': linker.findLinkReferences(contractInput['bytecode'])
116+
'linkReferences': contractInput['bytecode'] && linker.findLinkReferences(contractInput['bytecode'])
117117
},
118118
'deployedBytecode': {
119-
'object': linker.linkBytecode(contractInput['runtimeBytecode'], libraries),
119+
'object': contractInput['runtimeBytecode'] && linker.linkBytecode(contractInput['runtimeBytecode'], libraries || {}),
120120
'sourceMap': contractInput['srcmapRuntime'],
121-
'linkReferences': linker.findLinkReferences(contractInput['runtimeBytecode'])
121+
'linkReferences': contractInput['runtimeBytecode'] && linker.findLinkReferences(contractInput['runtimeBytecode'])
122122
},
123123
'methodIdentifiers': contractInput['functionHashes'],
124124
'gasEstimates': translatedGasEstimates

0 commit comments

Comments
 (0)