Skip to content

Commit 4c1224e

Browse files
wechmanwechman
authored andcommitted
Generate ASTJSONTest variants only if a file with expected result exists
1 parent 371a531 commit 4c1224e

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

test/libsolidity/ASTJSON/assembly/nested_functions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
}
9090
]
9191
},
92-
"evmVersion": "london",
92+
"evmVersion": %EVMVERSION%,
9393
"externalReferences":
9494
[
9595
{

test/libsolidity/ASTJSON/assembly/nested_functions_parseOnly.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
}
7777
]
7878
},
79-
"evmVersion": "london",
79+
"evmVersion": %EVMVERSION%,
8080
"externalReferences": [],
8181
"id": 5,
8282
"nodeType": "InlineAssembly",

test/libsolidity/ASTJSON/event_with_variables_of_internal_types.json

Whitespace-only changes.

test/libsolidity/ASTJSONTest.cpp

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,29 @@ namespace
5050

5151
string const sourceDelimiter("==== Source: ");
5252

53-
const map<string, CompilerStack::State> compilerStateMap = {
54-
{"Empty", CompilerStack::State::Empty},
55-
{"SourcesSet", CompilerStack::State::SourcesSet},
56-
{"Parsed", CompilerStack::State::Parsed},
57-
{"ParsedAndImported", CompilerStack::State::ParsedAndImported},
58-
{"AnalysisPerformed", CompilerStack::State::AnalysisPerformed},
59-
{"CompilationSuccessful", CompilerStack::State::CompilationSuccessful}
60-
};
53+
string compilerStateToString(CompilerStack::State _state)
54+
{
55+
switch (_state)
56+
{
57+
case CompilerStack::State::Empty: return "Empty";
58+
case CompilerStack::State::SourcesSet: return "SourcesSet";
59+
case CompilerStack::State::Parsed: return "Parsed";
60+
case CompilerStack::State::ParsedAndImported: return "ParsedAndImported";
61+
case CompilerStack::State::AnalysisPerformed: return "AnalysisPerformed";
62+
case CompilerStack::State::CompilationSuccessful: return "CompilationSuccessful";
63+
}
64+
soltestAssert(false, "Unexpected value of state parameter");
65+
}
66+
67+
CompilerStack::State stringToCompilerState(const string& _state)
68+
{
69+
for (unsigned int i = CompilerStack::State::Empty; i <= CompilerStack::State::CompilationSuccessful; ++i)
70+
{
71+
if (_state == compilerStateToString(CompilerStack::State(i)))
72+
return CompilerStack::State(i);
73+
}
74+
BOOST_THROW_EXCEPTION(runtime_error("Unsupported compiler state (" + _state + ") in test contract file"));
75+
}
6176

6277
void replaceVersionWithTag(string& _input)
6378
{
@@ -88,11 +103,25 @@ ASTJSONTest::ASTJSONTest(string const& _filename)
88103
string_view baseName = _filename;
89104
baseName.remove_suffix(4);
90105

91-
m_variants = {
92-
TestVariant(baseName, CompilerStack::State::Parsed),
93-
TestVariant(baseName, CompilerStack::State::AnalysisPerformed),
106+
const std::vector<CompilerStack::State> variantCompileStates = {
107+
CompilerStack::State::Parsed,
108+
CompilerStack::State::AnalysisPerformed
94109
};
95110

111+
for (const auto state: variantCompileStates)
112+
{
113+
auto variant = TestVariant(baseName, state);
114+
if (boost::filesystem::exists(variant.astFilename()))
115+
{
116+
variant.expectation = readFileAsString(variant.astFilename());
117+
boost::replace_all(variant.expectation, "\r\n", "\n");
118+
m_variants.push_back(variant);
119+
}
120+
}
121+
122+
if (m_variants.empty())
123+
BOOST_THROW_EXCEPTION(runtime_error("Missing file with expected result for: \"" + _filename + "\"."));
124+
96125
ifstream file(_filename);
97126
if (!file)
98127
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test contract: \"" + _filename + "\"."));
@@ -120,24 +149,16 @@ ASTJSONTest::ASTJSONTest(string const& _filename)
120149
{
121150
string state = line.substr(failMarker.size());
122151
boost::algorithm::trim(state);
123-
if (compilerStateMap.find(state) == compilerStateMap.end())
124-
BOOST_THROW_EXCEPTION(runtime_error("Unsupported compiler state (" + state + ") in test contract file"));
125152
if (m_expectedFailAfter.has_value())
126153
BOOST_THROW_EXCEPTION(runtime_error("Duplicated \"failAfter\" directive"));
127-
m_expectedFailAfter = compilerStateMap.at(state);
154+
m_expectedFailAfter = stringToCompilerState(state);
128155
}
129156
else if (!line.empty() && !boost::algorithm::starts_with(line, delimiter))
130157
source += line + "\n";
131158
}
132159

133160
m_sources.emplace_back(sourceName.empty() ? "a" : sourceName, source);
134161
file.close();
135-
136-
for (TestVariant& variant: m_variants)
137-
{
138-
variant.expectation = readFileAsString(variant.astFilename());
139-
boost::replace_all(variant.expectation, "\r\n", "\n");
140-
}
141162
}
142163

143164
TestCase::TestResult ASTJSONTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)

0 commit comments

Comments
 (0)