21
21
*/
22
22
23
23
#include < test/libsolidity/AnalysisFramework.h>
24
+ #include < test/libsolidity/util/SoltestErrors.h>
24
25
25
26
#include < test/Common.h>
26
27
@@ -41,13 +42,15 @@ BOOST_FIXTURE_TEST_SUITE(SolidityNameAndTypeResolution, AnalysisFramework)
41
42
42
43
BOOST_AUTO_TEST_CASE (function_no_implementation)
43
44
{
44
- SourceUnit const * sourceUnit = nullptr ;
45
45
char const * text = R"(
46
46
abstract contract test {
47
47
function functionName(bytes32 input) public virtual returns (bytes32 out);
48
48
}
49
49
)" ;
50
- sourceUnit = parseAndAnalyse (text);
50
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
51
+ soltestAssert (sourceUnit);
52
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
53
+
51
54
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes ();
52
55
ContractDefinition* contract = dynamic_cast <ContractDefinition*>(nodes[1 ].get ());
53
56
BOOST_REQUIRE (contract);
@@ -57,12 +60,14 @@ BOOST_AUTO_TEST_CASE(function_no_implementation)
57
60
58
61
BOOST_AUTO_TEST_CASE (abstract_contract)
59
62
{
60
- SourceUnit const * sourceUnit = nullptr ;
61
63
char const * text = R"(
62
64
abstract contract base { function foo() public virtual; }
63
65
contract derived is base { function foo() public override {} }
64
66
)" ;
65
- sourceUnit = parseAndAnalyse (text);
67
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
68
+ soltestAssert (sourceUnit);
69
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
70
+
66
71
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes ();
67
72
ContractDefinition* base = dynamic_cast <ContractDefinition*>(nodes[1 ].get ());
68
73
ContractDefinition* derived = dynamic_cast <ContractDefinition*>(nodes[2 ].get ());
@@ -76,12 +81,14 @@ BOOST_AUTO_TEST_CASE(abstract_contract)
76
81
77
82
BOOST_AUTO_TEST_CASE (abstract_contract_with_overload)
78
83
{
79
- SourceUnit const * sourceUnit = nullptr ;
80
84
char const * text = R"(
81
85
abstract contract base { function foo(bool) public virtual; }
82
86
abstract contract derived is base { function foo(uint) public {} }
83
87
)" ;
84
- sourceUnit = parseAndAnalyse (text);
88
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
89
+ soltestAssert (sourceUnit);
90
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
91
+
85
92
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes ();
86
93
ContractDefinition* base = dynamic_cast <ContractDefinition*>(nodes[1 ].get ());
87
94
ContractDefinition* derived = dynamic_cast <ContractDefinition*>(nodes[2 ].get ());
@@ -93,12 +100,14 @@ BOOST_AUTO_TEST_CASE(abstract_contract_with_overload)
93
100
94
101
BOOST_AUTO_TEST_CASE (implement_abstract_via_constructor)
95
102
{
96
- SourceUnit const * sourceUnit = nullptr ;
97
103
char const * text = R"(
98
104
abstract contract base { function foo() public virtual; }
99
105
abstract contract foo is base { constructor() {} }
100
106
)" ;
101
- sourceUnit = parseAndAnalyse (text);
107
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
108
+ soltestAssert (sourceUnit);
109
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
110
+
102
111
std::vector<ASTPointer<ASTNode>> nodes = sourceUnit->nodes ();
103
112
BOOST_CHECK_EQUAL (nodes.size (), 3 );
104
113
ContractDefinition* derived = dynamic_cast <ContractDefinition*>(nodes[2 ].get ());
@@ -108,15 +117,17 @@ BOOST_AUTO_TEST_CASE(implement_abstract_via_constructor)
108
117
109
118
BOOST_AUTO_TEST_CASE (function_canonical_signature)
110
119
{
111
- SourceUnit const * sourceUnit = nullptr ;
112
120
char const * text = R"(
113
121
contract Test {
114
122
function foo(uint256 arg1, uint64 arg2, bool arg3) public returns (uint256 ret) {
115
123
ret = arg1 + arg2;
116
124
}
117
125
}
118
126
)" ;
119
- sourceUnit = parseAndAnalyse (text);
127
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
128
+ soltestAssert (sourceUnit);
129
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
130
+
120
131
for (ASTPointer<ASTNode> const & node: sourceUnit->nodes ())
121
132
if (ContractDefinition* contract = dynamic_cast <ContractDefinition*>(node.get ()))
122
133
{
@@ -127,15 +138,17 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature)
127
138
128
139
BOOST_AUTO_TEST_CASE (function_canonical_signature_type_aliases)
129
140
{
130
- SourceUnit const * sourceUnit = nullptr ;
131
141
char const * text = R"(
132
142
contract Test {
133
143
function boo(uint, bytes32, address) public returns (uint ret) {
134
144
ret = 5;
135
145
}
136
146
}
137
147
)" ;
138
- sourceUnit = parseAndAnalyse (text);
148
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
149
+ soltestAssert (sourceUnit);
150
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
151
+
139
152
for (ASTPointer<ASTNode> const & node: sourceUnit->nodes ())
140
153
if (ContractDefinition* contract = dynamic_cast <ContractDefinition*>(node.get ()))
141
154
{
@@ -148,7 +161,6 @@ BOOST_AUTO_TEST_CASE(function_canonical_signature_type_aliases)
148
161
149
162
BOOST_AUTO_TEST_CASE (function_external_types)
150
163
{
151
- SourceUnit const * sourceUnit = nullptr ;
152
164
char const * text = R"(
153
165
contract C {
154
166
uint a;
@@ -159,7 +171,10 @@ BOOST_AUTO_TEST_CASE(function_external_types)
159
171
}
160
172
}
161
173
)" ;
162
- sourceUnit = parseAndAnalyse (text);
174
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
175
+ soltestAssert (sourceUnit);
176
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
177
+
163
178
for (ASTPointer<ASTNode> const & node: sourceUnit->nodes ())
164
179
if (ContractDefinition* contract = dynamic_cast <ContractDefinition*>(node.get ()))
165
180
{
@@ -172,7 +187,6 @@ BOOST_AUTO_TEST_CASE(function_external_types)
172
187
173
188
BOOST_AUTO_TEST_CASE (enum_external_type)
174
189
{
175
- SourceUnit const * sourceUnit = nullptr ;
176
190
char const * text = R"(
177
191
// test for bug #1801
178
192
contract Test {
@@ -182,7 +196,10 @@ BOOST_AUTO_TEST_CASE(enum_external_type)
182
196
}
183
197
}
184
198
)" ;
185
- sourceUnit = parseAndAnalyse (text);
199
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
200
+ soltestAssert (sourceUnit);
201
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
202
+
186
203
for (ASTPointer<ASTNode> const & node: sourceUnit->nodes ())
187
204
if (ContractDefinition* contract = dynamic_cast <ContractDefinition*>(node.get ()))
188
205
{
@@ -264,7 +281,10 @@ BOOST_AUTO_TEST_CASE(struct_with_mapping_in_library)
264
281
function f(X storage x) external {}
265
282
}
266
283
)" ;
267
- SourceUnit const * sourceUnit = parseAndAnalyse (text);
284
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
285
+ soltestAssert (sourceUnit);
286
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
287
+
268
288
for (ASTPointer<ASTNode> const & node: sourceUnit->nodes ())
269
289
if (ContractDefinition* contract = dynamic_cast <ContractDefinition*>(node.get ()))
270
290
{
@@ -287,10 +307,12 @@ BOOST_AUTO_TEST_CASE(state_variable_accessors)
287
307
}
288
308
)" ;
289
309
290
- SourceUnit const * source;
310
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
311
+ soltestAssert (sourceUnit);
312
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
313
+
291
314
ContractDefinition const * contract;
292
- source = parseAndAnalyse (text);
293
- BOOST_REQUIRE ((contract = retrieveContractByName (*source, " test" )) != nullptr );
315
+ BOOST_REQUIRE ((contract = retrieveContractByName (*sourceUnit, " test" )) != nullptr );
294
316
FunctionTypePointer function = retrieveFunctionBySignature (*contract, " foo()" );
295
317
BOOST_REQUIRE (function && function->hasDeclaration ());
296
318
auto returnParams = function->returnParameterTypes ();
@@ -327,9 +349,12 @@ BOOST_AUTO_TEST_CASE(private_state_variable)
327
349
}
328
350
)" ;
329
351
352
+ auto [sourceUnit, errors] = parseAnalyseAndReturnError (text);
353
+ soltestAssert (sourceUnit);
354
+ soltestAssert (errors.empty (), " Unexpected error: " + formatErrors (errors));
355
+
330
356
ContractDefinition const * contract;
331
- SourceUnit const * source = parseAndAnalyse (text);
332
- BOOST_CHECK ((contract = retrieveContractByName (*source, " test" )) != nullptr );
357
+ BOOST_CHECK ((contract = retrieveContractByName (*sourceUnit, " test" )) != nullptr );
333
358
FunctionTypePointer function;
334
359
function = retrieveFunctionBySignature (*contract, " foo()" );
335
360
BOOST_CHECK_MESSAGE (function == nullptr , " Accessor function of a private variable should not exist" );
@@ -345,7 +370,7 @@ BOOST_AUTO_TEST_CASE(string)
345
370
function f(string calldata x) external { s = x; }
346
371
}
347
372
)" ;
348
- BOOST_CHECK_NO_THROW ( parseAndAnalyse ( sourceCode) );
373
+ CHECK_SUCCESS ( sourceCode);
349
374
}
350
375
351
376
BOOST_AUTO_TEST_CASE (dynamic_return_types_not_possible)
0 commit comments