From f431c6f0584dfd84f18d245b909acacd95c73689 Mon Sep 17 00:00:00 2001 From: wechman Date: Tue, 22 Feb 2022 12:29:29 +0100 Subject: [PATCH] Support compilation fail scenarios in ASTJSON tests --- .../ASTJSON/not_existing_import.sol | 1 + test/libsolidity/ASTJSONTest.cpp | 24 +++++++++++++++++-- test/libsolidity/ASTJSONTest.h | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/test/libsolidity/ASTJSON/not_existing_import.sol b/test/libsolidity/ASTJSON/not_existing_import.sol index 6353c7527..7251a02b8 100644 --- a/test/libsolidity/ASTJSON/not_existing_import.sol +++ b/test/libsolidity/ASTJSON/not_existing_import.sol @@ -6,3 +6,4 @@ contract C is NotExisting.X } // ---- +// failAfter: Parsed diff --git a/test/libsolidity/ASTJSONTest.cpp b/test/libsolidity/ASTJSONTest.cpp index 975c66be2..0dbf48448 100644 --- a/test/libsolidity/ASTJSONTest.cpp +++ b/test/libsolidity/ASTJSONTest.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -49,6 +50,15 @@ namespace string const sourceDelimiter("==== Source: "); +const map compilerStateMap = { + {"Empty", CompilerStack::State::Empty}, + {"SourcesSet", CompilerStack::State::SourcesSet}, + {"Parsed", CompilerStack::State::Parsed}, + {"ParsedAndImported", CompilerStack::State::ParsedAndImported}, + {"AnalysisPerformed", CompilerStack::State::AnalysisPerformed}, + {"CompilationSuccessful", CompilerStack::State::CompilationSuccessful} +}; + void replaceVersionWithTag(string& _input) { boost::algorithm::replace_all( @@ -92,6 +102,7 @@ ASTJSONTest::ASTJSONTest(string const& _filename) string source; string line; string const delimiter("// ----"); + string const failMarker("// failAfter:"); while (getline(file, line)) { if (boost::algorithm::starts_with(line, sourceDelimiter)) @@ -105,6 +116,16 @@ ASTJSONTest::ASTJSONTest(string const& _filename) ); source = string(); } + else if (boost::algorithm::starts_with(line, failMarker)) + { + string state = line.substr(failMarker.size()); + boost::algorithm::trim(state); + if (compilerStateMap.find(state) == compilerStateMap.end()) + BOOST_THROW_EXCEPTION(runtime_error("Unsupported compiler state (" + state + ") in test contract file")); + if (m_expectedFailAfter.has_value()) + BOOST_THROW_EXCEPTION(runtime_error("Duplicated \"failAfter\" directive")); + m_expectedFailAfter = compilerStateMap.at(state); + } else if (!line.empty() && !boost::algorithm::starts_with(line, delimiter)) source += line + "\n"; } @@ -141,8 +162,7 @@ TestCase::TestResult ASTJSONTest::run(ostream& _stream, string const& _linePrefi if (!c.parseAndAnalyze(variant.stopAfter)) { - // We just want to export so raise fatal analysis errors only - if (c.state() < CompilerStack::State::ParsedAndImported) + if (!m_expectedFailAfter.has_value() || m_expectedFailAfter.value() + 1 != c.state()) { SourceReferenceFormatter formatter(_stream, c, _formatted, false); formatter.printErrorInformation(c.errors()); diff --git a/test/libsolidity/ASTJSONTest.h b/test/libsolidity/ASTJSONTest.h index 3365de589..97cea184b 100644 --- a/test/libsolidity/ASTJSONTest.h +++ b/test/libsolidity/ASTJSONTest.h @@ -90,6 +90,7 @@ private: ) const; std::vector m_variants; + std::optional m_expectedFailAfter; std::vector> m_sources; };