forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTJSONTest.cpp
322 lines (276 loc) · 9.24 KB
/
ASTJSONTest.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <liblangutil/SourceReferenceFormatter.h>
#include <libsolidity/ast/ASTJsonExporter.h>
#include <libsolutil/AnsiColorized.h>
#include <libsolutil/CommonIO.h>
#include <libsolutil/JSON.h>
#include <test/Common.h>
#include <test/libsolidity/ASTJSONTest.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp>
#include <fstream>
#include <memory>
#include <stdexcept>
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::util::formatting;
using namespace solidity::util;
namespace fs = boost::filesystem;
using namespace boost::unit_test;
using namespace std::string_literals;
namespace
{
std::string const sourceDelimiter("==== Source: ");
std::string compilerStateToString(CompilerStack::State _state)
{
switch (_state)
{
case CompilerStack::State::Empty: return "Empty";
case CompilerStack::State::SourcesSet: return "SourcesSet";
case CompilerStack::State::Parsed: return "Parsed";
case CompilerStack::State::ParsedAndImported: return "ParsedAndImported";
case CompilerStack::State::AnalysisSuccessful: return "AnalysisSuccessful";
case CompilerStack::State::CompilationSuccessful: return "CompilationSuccessful";
}
soltestAssert(false, "Unexpected value of state parameter");
}
CompilerStack::State stringToCompilerState(const std::string& _state)
{
for (unsigned int i = CompilerStack::State::Empty; i <= CompilerStack::State::CompilationSuccessful; ++i)
{
if (_state == compilerStateToString(CompilerStack::State(i)))
return CompilerStack::State(i);
}
BOOST_THROW_EXCEPTION(std::runtime_error("Unsupported compiler state (" + _state + ") in test contract file"));
}
void replaceVersionWithTag(std::string& _input)
{
boost::algorithm::replace_all(
_input,
"\"" + solidity::test::CommonOptions::get().evmVersion().name() + "\"",
"%EVMVERSION%"
);
}
void replaceTagWithVersion(std::string& _input)
{
boost::algorithm::replace_all(
_input,
"%EVMVERSION%",
"\"" + solidity::test::CommonOptions::get().evmVersion().name() + "\""
);
}
}
void ASTJSONTest::generateTestVariants(std::string const& _filename)
{
std::string_view baseName = _filename;
baseName.remove_suffix(4);
const std::vector<CompilerStack::State> variantCompileStates = {
CompilerStack::State::Parsed,
CompilerStack::State::AnalysisSuccessful,
};
for (const auto state: variantCompileStates)
{
auto variant = TestVariant(baseName, state);
if (boost::filesystem::exists(variant.astFilename()))
{
variant.expectation = readFileAsString(variant.astFilename());
boost::replace_all(variant.expectation, "\r\n", "\n");
m_variants.push_back(variant);
}
}
}
void ASTJSONTest::fillSources(std::string const& _filename)
{
std::ifstream file(_filename);
if (!file)
BOOST_THROW_EXCEPTION(std::runtime_error("Cannot open test contract: \"" + _filename + "\"."));
file.exceptions(std::ios::badbit);
std::string sourceName;
std::string source;
std::string line;
std::string const delimiter("// ----");
std::string const failMarker("// failAfter:");
while (getline(file, line))
{
if (boost::algorithm::starts_with(line, sourceDelimiter))
{
if (!sourceName.empty())
m_sources.emplace_back(sourceName, source);
sourceName = line.substr(
sourceDelimiter.size(),
line.size() - " ===="s.size() - sourceDelimiter.size()
);
source = std::string();
}
else if (boost::algorithm::starts_with(line, failMarker))
{
std::string state = line.substr(failMarker.size());
boost::algorithm::trim(state);
if (m_expectedFailAfter.has_value())
BOOST_THROW_EXCEPTION(std::runtime_error("Duplicated \"failAfter\" directive"));
m_expectedFailAfter = stringToCompilerState(state);
}
else if (!line.empty() && !boost::algorithm::starts_with(line, delimiter))
source += line + "\n";
}
m_sources.emplace_back(sourceName.empty() ? "a" : sourceName, source);
file.close();
}
void ASTJSONTest::validateTestConfiguration() const
{
if (m_variants.empty())
BOOST_THROW_EXCEPTION(std::runtime_error("No file with expected result found."));
if (m_expectedFailAfter.has_value())
{
auto unexpectedTestVariant = std::find_if(
m_variants.begin(), m_variants.end(),
[failAfter = m_expectedFailAfter](TestVariant v) { return v.stopAfter > failAfter; }
);
if (unexpectedTestVariant != m_variants.end())
BOOST_THROW_EXCEPTION(
std::runtime_error(
std::string("Unexpected JSON file: ") + unexpectedTestVariant->astFilename() +
" in \"failAfter: " +
compilerStateToString(m_expectedFailAfter.value()) + "\" scenario."
)
);
}
}
ASTJSONTest::ASTJSONTest(std::string const& _filename):
EVMVersionRestrictedTestCase(_filename)
{
if (!boost::algorithm::ends_with(_filename, ".sol"))
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid test contract file name: \"" + _filename + "\"."));
generateTestVariants(_filename);
fillSources(_filename);
validateTestConfiguration();
}
TestCase::TestResult ASTJSONTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted)
{
CompilerStack c;
StringMap sources;
std::map<std::string, unsigned> sourceIndices;
for (size_t i = 0; i < m_sources.size(); i++)
{
sources[m_sources[i].first] = m_sources[i].second;
sourceIndices[m_sources[i].first] = static_cast<unsigned>(i + 1);
}
bool resultsMatch = true;
for (TestVariant& variant: m_variants)
{
c.reset();
c.setSources(sources);
c.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
if (!c.parseAndAnalyze(variant.stopAfter))
{
if (!m_expectedFailAfter.has_value() || m_expectedFailAfter.value() + 1 != c.state())
{
SourceReferenceFormatter formatter(_stream, c, _formatted, false);
formatter.printErrorInformation(c.errors());
return TestResult::FatalError;
}
}
resultsMatch = resultsMatch && runTest(
variant,
sourceIndices,
c,
_stream,
_linePrefix,
_formatted
);
}
return resultsMatch ? TestResult::Success : TestResult::Failure;
}
bool ASTJSONTest::runTest(
TestVariant& _variant,
std::map<std::string, unsigned> const& _sourceIndices,
CompilerStack& _compiler,
std::ostream& _stream,
std::string const& _linePrefix,
bool const _formatted
)
{
if (m_sources.size() > 1)
_variant.result += "[\n";
for (size_t i = 0; i < m_sources.size(); i++)
{
std::ostringstream result;
ASTJsonExporter(_compiler.state(), _sourceIndices).print(result, _compiler.ast(m_sources[i].first), JsonFormat{ JsonFormat::Pretty });
_variant.result += result.str();
if (i != m_sources.size() - 1)
_variant.result += ",";
_variant.result += "\n";
}
if (m_sources.size() > 1)
_variant.result += "]\n";
replaceTagWithVersion(_variant.expectation);
if (_variant.expectation != _variant.result)
{
std::string nextIndentLevel = _linePrefix + " ";
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) <<
_linePrefix <<
"Expected result" <<
(!_variant.name().empty() ? " (" + _variant.name() + "):" : ":") <<
std::endl;
printPrefixed(_stream, _variant.expectation, nextIndentLevel);
_stream << std::endl;
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) <<
_linePrefix <<
"Obtained result" <<
(!_variant.name().empty() ? " (" + _variant.name() + "):" : ":") <<
std::endl;
printPrefixed(_stream, _variant.result, nextIndentLevel);
_stream << std::endl;
return false;
}
return true;
}
void ASTJSONTest::printSource(std::ostream& _stream, std::string const& _linePrefix, bool const) const
{
for (auto const& source: m_sources)
{
if (m_sources.size() > 1 || source.first != "a")
printPrefixed(_stream, sourceDelimiter + source.first + " ====\n", _linePrefix);
printPrefixed(_stream, source.second, _linePrefix);
_stream << std::endl;
}
}
void ASTJSONTest::printUpdatedExpectations(std::ostream&, std::string const&) const
{
for (TestVariant const& variant: m_variants)
updateExpectation(
variant.astFilename(),
variant.result,
variant.name().empty() ? "" : variant.name() + " "
);
}
void ASTJSONTest::updateExpectation(std::string const& _filename, std::string const& _expectation, std::string const& _variant) const
{
std::ofstream file(_filename.c_str());
if (!file) BOOST_THROW_EXCEPTION(std::runtime_error("Cannot write " + _variant + "AST expectation to \"" + _filename + "\"."));
file.exceptions(std::ios::badbit);
std::string replacedResult = _expectation;
replaceVersionWithTag(replacedResult);
file << replacedResult;
file.flush();
file.close();
}