Skip to content

Commit 9af32ce

Browse files
committed
testing the errors
1 parent 308eeb9 commit 9af32ce

2 files changed

Lines changed: 56 additions & 15 deletions

File tree

src/slang-utils/create-parser.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ function parserAndOutput(
2121
};
2222
}
2323

24+
function createError(
25+
{ parseOutput }: { parseOutput: ParseOutput },
26+
reason: string
27+
): Error {
28+
return new Error(
29+
`We encountered the following syntax error:\n\n\t${parseOutput.errors()[0].message}\n\n${reason}`
30+
);
31+
}
32+
2433
export function createParser(
2534
text: string,
2635
options: ParserOptions<AstNode>
@@ -30,10 +39,9 @@ export function createParser(
3039
const result = parserAndOutput(text, compiler);
3140

3241
if (!result.parseOutput.isValid())
33-
throw new Error(
34-
`We encountered the following syntax error:\n\n\t${
35-
result.parseOutput.errors()[0].message
36-
}\n\nBased on the compiler option provided, we inferred your code to be using Solidity version ${
42+
throw createError(
43+
result,
44+
`Based on the compiler option provided, we inferred your code to be using Solidity version ${
3745
result.parser.languageVersion
3846
}. If you would like to change that, specify a different version in your \`.prettierrc\` file.`
3947
);
@@ -51,10 +59,9 @@ export function createParser(
5159
);
5260

5361
if (!result.parseOutput.isValid())
54-
throw new Error(
55-
`We encountered the following syntax error:\n\n\t${
56-
result.parseOutput.errors()[0].message
57-
}\n\nWe couldn't infer a Solidity version base on the pragma statements in your code so we defaulted to ${
62+
throw createError(
63+
result,
64+
`We couldn't infer a Solidity version based on the pragma statements in your code so we defaulted to ${
5865
result.parser.languageVersion
5966
}. You might be attempting to use a syntax not yet supported by Slang or you might want to specify a version in your \`.prettierrc\` file.`
6067
);
@@ -64,10 +71,9 @@ export function createParser(
6471
const result = parserAndOutput(text, inferredRanges[0]);
6572

6673
if (!result.parseOutput.isValid())
67-
throw new Error(
68-
`We encountered the following syntax error:\n\n\t${
69-
result.parseOutput.errors()[0].message
70-
}\n\nBased on the pragma statements, we inferred your code to be using Solidity version ${
74+
throw createError(
75+
result,
76+
`Based on the pragma statements, we inferred your code to be using Solidity version ${
7177
result.parser.languageVersion
7278
}. If you would like to change that, update the pragmas in your source file, or specify a version in your \`.prettierrc\` file.`
7379
);

tests/unit/slang-utils/create-parser.test.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,44 @@ describe('inferLanguage', function () {
110110
expect(parser.languageVersion).toEqual('0.8.0');
111111
});
112112

113-
test.skip('should throw an error if there are incompatible ranges', function () {
113+
test('should throw if compiler option does not match the syntax', function () {
114114
expect(() =>
115-
createParser(`pragma solidity ^0.8.0; pragma solidity 0.7.6;`, options)
116-
).toThrow();
115+
createParser(`contract Foo {byte bar;}`, { compiler: '0.8.0' })
116+
).toThrow(
117+
'Based on the compiler option provided, we inferred your code to be using Solidity version'
118+
);
119+
});
120+
121+
test('should throw if pragma is outside the supported version and the syntax does not match with the latest supported version', function () {
122+
expect(() =>
123+
createParser(`pragma solidity 10.0.0;contract Foo {byte bar;}`, options)
124+
).toThrow(
125+
"We couldn't infer a Solidity version based on the pragma statements"
126+
);
127+
});
128+
129+
test('should throw if there is no pragma and the syntax does not match with the latest supported version', function () {
130+
expect(() => createParser(`contract Foo {byte bar;}`, options)).toThrow(
131+
"We couldn't infer a Solidity version based on the pragma statements"
132+
);
133+
});
134+
135+
test('should throw an error if there are incompatible ranges and the syntax does not match with the latest supported version', function () {
136+
expect(() =>
137+
createParser(
138+
`pragma solidity ^0.8.0; pragma solidity 0.7.6;contract Foo {byte bar;}`,
139+
options
140+
)
141+
).toThrow(
142+
"We couldn't infer a Solidity version based on the pragma statements"
143+
);
144+
});
145+
146+
test('should throw an error if the pragma statement is and the syntax do not match', function () {
147+
expect(() =>
148+
createParser(`pragma solidity ^0.8.0;contract Foo {byte bar;}`, options)
149+
).toThrow(
150+
'Based on the pragma statements, we inferred your code to be using Solidity version'
151+
);
117152
});
118153
});

0 commit comments

Comments
 (0)