Skip to content

Commit 0e99ace

Browse files
committed
Add comprehensive tests for the file import callback
1 parent 23b7472 commit 0e99ace

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

test/package.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,61 @@ tape('Compilation', function (t) {
7272
st.ok(output.contracts['lib.sol:L'].bytecode.length > 0);
7373
st.end();
7474
});
75+
76+
t.test('lazy-loading callback works (with file not found)', function (st) {
77+
var input = {
78+
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
79+
};
80+
function findImports (path) {
81+
return { error: 'File not found' };
82+
}
83+
var output = solc.compile({sources: input}, 0, findImports);
84+
st.plan(3);
85+
st.ok('errors' in output);
86+
// Check if the ParserError exists, but allow others too
87+
st.ok(output.errors.length >= 1);
88+
for (var error in output.errors) {
89+
// Error should be something like:
90+
// cont.sol:1:1: ParserError: Source "lib.sol" not found: File not found
91+
if (output.errors[error].indexOf('ParserError') !== -1 && output.errors[error].indexOf('File not found') !== -1) {
92+
st.ok(true);
93+
}
94+
}
95+
st.end();
96+
});
97+
98+
t.test('lazy-loading callback works (with exception)', function (st) {
99+
var input = {
100+
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
101+
};
102+
function findImports (path) {
103+
throw new Error('Could not implement this interface properly...');
104+
}
105+
st.throws(function () {
106+
solc.compile({sources: input}, 0, findImports);
107+
}, /^Error: Could not implement this interface properly.../);
108+
st.end();
109+
});
110+
111+
t.test('file import without lazy-loading callback fails properly', function (st) {
112+
var input = {
113+
'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
114+
};
115+
var output = solc.compile({sources: input}, 0);
116+
st.plan(3);
117+
st.ok('errors' in output);
118+
// Check if the ParserError exists, but allow others too
119+
st.ok(output.errors.length >= 1);
120+
for (var error in output.errors) {
121+
// Error should be something like:
122+
// cont.sol:1:1: ParserError: Source "lib.sol" not found: File not supplied initially.
123+
if (output.errors[error].indexOf('ParserError') !== -1 && output.errors[error].indexOf('File not supplied initially.') !== -1) {
124+
st.ok(true);
125+
}
126+
}
127+
st.end();
128+
});
129+
75130
t.test('compiling standard JSON', function (st) {
76131
if (!solc.supportsStandard) {
77132
st.skip('Not supported by solc');

0 commit comments

Comments
 (0)