-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathcreate-parser.test.js
More file actions
153 lines (140 loc) · 4.96 KB
/
create-parser.test.js
File metadata and controls
153 lines (140 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { LanguageFacts } from '@nomicfoundation/slang/utils';
import { createParser } from '../../../src/slang-utils/create-parser.js';
describe('inferLanguage', function () {
const latestSupportedVersion = LanguageFacts.latestVersion();
const options = { filepath: 'test.sol' };
const fixtures = [
{
description: 'Caret range',
source: `pragma solidity ^0.7.0;`,
version: '0.7.0'
},
{
description: 'Pinned version',
source: `pragma solidity 0.8.1;`,
version: '0.8.1'
},
{
description: 'Caret range and pinned version',
source: `pragma solidity ^0.8.0; pragma solidity 0.8.2;`,
version: '0.8.2'
},
{
description: 'pragma is broken by new lines, whitespace and comments',
source: `pragma solidity 0.
// comment 1
7.
/* comment 2*/
3;`,
version: '0.7.3'
},
{
description: 'With multiline comment before the range',
source: `pragma solidity /* comment */ 0.8.2;`,
version: '0.8.2'
},
{
description: 'With natspec comment before the range',
source: `pragma solidity /** comment */ 0.8.2;`,
version: '0.8.2'
},
{
description: 'With multiline comment between the ranges',
source: `pragma solidity ^0.8.0 /* comment */ 0.8.2;`,
version: '0.8.2'
},
{
description: 'With natspec comment between the ranges',
source: `pragma solidity ^0.8.0 /** comment */ 0.8.2;`,
version: '0.8.2'
},
{
description: 'With multiline comment after the range',
source: `pragma solidity 0.8.2 /* comment */;`,
version: '0.8.2'
},
{
description: 'With natspec comment after the range',
source: `pragma solidity 0.8.2 /** comment */;`,
version: '0.8.2'
},
{
description: 'With tracing line comment',
source: `pragma solidity 0.8.2; // line comment`,
version: '0.8.2'
},
{
description: 'With line comment between "solidity" and the version',
source: `pragma solidity
// line comment
0.8.2;`,
version: '0.8.2'
},
{
description:
'should use the latest version if the range is outside the supported versions',
source: `pragma solidity ^10.0.0;`,
version: latestSupportedVersion
}
];
for (const { description, source, version, skip } of fixtures) {
(skip ? test.skip : test)(description, function () {
const { parser } = createParser(source, options);
expect(parser.languageVersion).toEqual(version);
});
}
test('should use the latest successful version if the source has no pragmas', function () {
let { parser } = createParser(`contract Foo {}`, options);
expect(parser.languageVersion).toEqual(latestSupportedVersion);
// ({ parser } = createParser(`contract Foo {byte bar;}`, options));
// expect(parser.languageVersion).toEqual('0.7.6');
});
test('should use compiler option if given', function () {
let { parser } = createParser(`pragma solidity ^0.8.0;`, {
compiler: '0.8.20'
});
expect(parser.languageVersion).toEqual('0.8.20');
({ parser } = createParser(`pragma solidity ^0.8.0;`, {
compiler: '0.8.2'
}));
expect(parser.languageVersion).toEqual('0.8.2');
({ parser } = createParser(`pragma solidity ^0.8.0;`, {}));
expect(parser.languageVersion).toEqual('0.8.0');
});
test('should throw if compiler option does not match the syntax', function () {
expect(() =>
createParser(`contract Foo {byte bar;}`, { compiler: '0.8.0' })
).toThrow(
'Based on the compiler option provided, we inferred your code to be using Solidity version'
);
});
test('should throw if pragma is outside the supported version and the syntax does not match with the latest supported version', function () {
expect(() =>
createParser(`pragma solidity 10.0.0;contract Foo {byte bar;}`, options)
).toThrow(
"We couldn't infer a Solidity version based on the pragma statements"
);
});
test('should throw if there is no pragma and the syntax does not match with the latest supported version', function () {
expect(() => createParser(`contract Foo {byte bar;}`, options)).toThrow(
"We couldn't infer a Solidity version based on the pragma statements"
);
});
test('should throw an error if there are incompatible ranges and the syntax does not match with the latest supported version', function () {
expect(() =>
createParser(
`pragma solidity ^0.8.0; pragma solidity 0.7.6;contract Foo {byte bar;}`,
options
)
).toThrow(
"We couldn't infer a Solidity version based on the pragma statements"
);
});
test('should throw an error if the pragma statement is and the syntax do not match', function () {
expect(() =>
createParser(`pragma solidity ^0.8.0;contract Foo {byte bar;}`, options)
).toThrow(
'Based on the pragma statements, we inferred your code to be using Solidity version'
);
});
});