2018-08-03 17:30:18 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2022-06-16 16:05:51 +00:00
|
|
|
#include <libsolidity/ast/ASTJsonExporter.h>
|
2021-10-27 11:22:02 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
|
|
|
#include <libsolutil/CommonIO.h>
|
2021-10-22 02:05:49 +00:00
|
|
|
#include <libsolutil/JSON.h>
|
2021-10-27 11:22:02 +00:00
|
|
|
|
|
|
|
#include <test/Common.h>
|
|
|
|
#include <test/libsolidity/ASTJSONTest.h>
|
|
|
|
|
2018-08-03 17:30:18 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2021-10-27 11:22:02 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2022-02-22 11:29:29 +00:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2021-10-27 11:22:02 +00:00
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
|
2018-08-03 17:30:18 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
using namespace solidity;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::langutil;
|
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::frontend::test;
|
|
|
|
using namespace solidity::util::formatting;
|
|
|
|
using namespace solidity::util;
|
2018-08-03 17:30:18 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using namespace boost::unit_test;
|
2023-08-30 00:08:06 +00:00
|
|
|
using namespace std::string_literals;
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2020-01-14 11:46:47 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string const sourceDelimiter("==== Source: ");
|
2020-09-03 09:39:56 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string compilerStateToString(CompilerStack::State _state)
|
2022-02-23 13:57:57 +00:00
|
|
|
{
|
|
|
|
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";
|
2023-06-12 10:03:34 +00:00
|
|
|
case CompilerStack::State::AnalysisSuccessful: return "AnalysisSuccessful";
|
2022-02-23 13:57:57 +00:00
|
|
|
case CompilerStack::State::CompilationSuccessful: return "CompilationSuccessful";
|
|
|
|
}
|
|
|
|
soltestAssert(false, "Unexpected value of state parameter");
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
CompilerStack::State stringToCompilerState(const std::string& _state)
|
2022-02-23 13:57:57 +00:00
|
|
|
{
|
|
|
|
for (unsigned int i = CompilerStack::State::Empty; i <= CompilerStack::State::CompilationSuccessful; ++i)
|
|
|
|
{
|
|
|
|
if (_state == compilerStateToString(CompilerStack::State(i)))
|
|
|
|
return CompilerStack::State(i);
|
|
|
|
}
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Unsupported compiler state (" + _state + ") in test contract file"));
|
2022-02-23 13:57:57 +00:00
|
|
|
}
|
2022-02-22 11:29:29 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void replaceVersionWithTag(std::string& _input)
|
2020-01-14 11:46:47 +00:00
|
|
|
{
|
|
|
|
boost::algorithm::replace_all(
|
|
|
|
_input,
|
2020-01-14 16:48:17 +00:00
|
|
|
"\"" + solidity::test::CommonOptions::get().evmVersion().name() + "\"",
|
2020-01-14 11:46:47 +00:00
|
|
|
"%EVMVERSION%"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void replaceTagWithVersion(std::string& _input)
|
2020-01-14 11:46:47 +00:00
|
|
|
{
|
|
|
|
boost::algorithm::replace_all(
|
|
|
|
_input,
|
|
|
|
"%EVMVERSION%",
|
2020-01-14 16:48:17 +00:00
|
|
|
"\"" + solidity::test::CommonOptions::get().evmVersion().name() + "\""
|
2020-01-14 11:46:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void ASTJSONTest::generateTestVariants(std::string const& _filename)
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string_view baseName = _filename;
|
2021-10-27 11:22:02 +00:00
|
|
|
baseName.remove_suffix(4);
|
|
|
|
|
2022-02-23 13:57:57 +00:00
|
|
|
const std::vector<CompilerStack::State> variantCompileStates = {
|
|
|
|
CompilerStack::State::Parsed,
|
2023-06-12 10:03:34 +00:00
|
|
|
CompilerStack::State::AnalysisSuccessful,
|
2021-10-27 11:22:02 +00:00
|
|
|
};
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2022-02-23 13:57:57 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2022-03-07 13:21:27 +00:00
|
|
|
}
|
2022-02-23 13:57:57 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void ASTJSONTest::fillSources(std::string const& _filename)
|
2022-03-07 13:21:27 +00:00
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::ifstream file(_filename);
|
2018-08-03 17:30:18 +00:00
|
|
|
if (!file)
|
2023-08-30 00:08:06 +00:00
|
|
|
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:");
|
2018-08-03 17:30:18 +00:00
|
|
|
while (getline(file, line))
|
|
|
|
{
|
|
|
|
if (boost::algorithm::starts_with(line, sourceDelimiter))
|
|
|
|
{
|
|
|
|
if (!sourceName.empty())
|
|
|
|
m_sources.emplace_back(sourceName, source);
|
|
|
|
|
2020-09-03 09:39:56 +00:00
|
|
|
sourceName = line.substr(
|
|
|
|
sourceDelimiter.size(),
|
|
|
|
line.size() - " ===="s.size() - sourceDelimiter.size()
|
|
|
|
);
|
2023-08-30 00:08:06 +00:00
|
|
|
source = std::string();
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
2022-02-22 11:29:29 +00:00
|
|
|
else if (boost::algorithm::starts_with(line, failMarker))
|
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string state = line.substr(failMarker.size());
|
2022-02-22 11:29:29 +00:00
|
|
|
boost::algorithm::trim(state);
|
|
|
|
if (m_expectedFailAfter.has_value())
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Duplicated \"failAfter\" directive"));
|
2022-02-23 13:57:57 +00:00
|
|
|
m_expectedFailAfter = stringToCompilerState(state);
|
2022-03-07 13:21:27 +00:00
|
|
|
|
2022-02-22 11:29:29 +00:00
|
|
|
}
|
2018-08-03 17:30:18 +00:00
|
|
|
else if (!line.empty() && !boost::algorithm::starts_with(line, delimiter))
|
|
|
|
source += line + "\n";
|
|
|
|
}
|
|
|
|
m_sources.emplace_back(sourceName.empty() ? "a" : sourceName, source);
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2022-03-07 13:21:27 +00:00
|
|
|
void ASTJSONTest::validateTestConfiguration() const
|
|
|
|
{
|
|
|
|
if (m_variants.empty())
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("No file with expected result found."));
|
2022-03-07 13:21:27 +00:00
|
|
|
|
|
|
|
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(
|
2023-08-30 00:08:06 +00:00
|
|
|
std::runtime_error(
|
|
|
|
std::string("Unexpected JSON file: ") + unexpectedTestVariant->astFilename() +
|
2022-03-07 13:21:27 +00:00
|
|
|
" in \"failAfter: " +
|
|
|
|
compilerStateToString(m_expectedFailAfter.value()) + "\" scenario."
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
ASTJSONTest::ASTJSONTest(std::string const& _filename):
|
2023-05-17 16:07:58 +00:00
|
|
|
EVMVersionRestrictedTestCase(_filename)
|
2022-03-07 13:21:27 +00:00
|
|
|
{
|
|
|
|
if (!boost::algorithm::ends_with(_filename, ".sol"))
|
2023-08-30 00:08:06 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Invalid test contract file name: \"" + _filename + "\"."));
|
2022-03-07 13:21:27 +00:00
|
|
|
|
|
|
|
generateTestVariants(_filename);
|
|
|
|
fillSources(_filename);
|
|
|
|
validateTestConfiguration();
|
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
TestCase::TestResult ASTJSONTest::run(std::ostream& _stream, std::string const& _linePrefix, bool const _formatted)
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
|
|
|
CompilerStack c;
|
|
|
|
|
2019-03-20 18:07:12 +00:00
|
|
|
StringMap sources;
|
2023-08-30 00:08:06 +00:00
|
|
|
std::map<std::string, unsigned> sourceIndices;
|
2018-08-03 17:30:18 +00:00
|
|
|
for (size_t i = 0; i < m_sources.size(); i++)
|
|
|
|
{
|
2019-03-20 18:07:12 +00:00
|
|
|
sources[m_sources[i].first] = m_sources[i].second;
|
2019-12-12 23:39:29 +00:00
|
|
|
sourceIndices[m_sources[i].first] = static_cast<unsigned>(i + 1);
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
2020-08-26 17:01:25 +00:00
|
|
|
|
2021-10-27 11:22:02 +00:00
|
|
|
bool resultsMatch = true;
|
2020-07-08 20:08:50 +00:00
|
|
|
|
2021-10-27 11:22:02 +00:00
|
|
|
for (TestVariant& variant: m_variants)
|
2020-07-08 20:08:50 +00:00
|
|
|
{
|
2021-10-27 11:22:02 +00:00
|
|
|
c.reset();
|
|
|
|
c.setSources(sources);
|
|
|
|
c.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
2020-07-08 20:08:50 +00:00
|
|
|
|
2021-10-27 11:22:02 +00:00
|
|
|
if (!c.parseAndAnalyze(variant.stopAfter))
|
|
|
|
{
|
2022-02-22 11:29:29 +00:00
|
|
|
if (!m_expectedFailAfter.has_value() || m_expectedFailAfter.value() + 1 != c.state())
|
2022-02-18 08:13:33 +00:00
|
|
|
{
|
|
|
|
SourceReferenceFormatter formatter(_stream, c, _formatted, false);
|
|
|
|
formatter.printErrorInformation(c.errors());
|
|
|
|
return TestResult::FatalError;
|
|
|
|
}
|
2021-10-27 11:22:02 +00:00
|
|
|
}
|
2020-07-08 20:08:50 +00:00
|
|
|
|
2021-10-27 11:22:02 +00:00
|
|
|
resultsMatch = resultsMatch && runTest(
|
|
|
|
variant,
|
|
|
|
sourceIndices,
|
|
|
|
c,
|
|
|
|
_stream,
|
|
|
|
_linePrefix,
|
|
|
|
_formatted
|
|
|
|
);
|
2019-05-07 13:33:22 +00:00
|
|
|
}
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2020-08-26 17:01:25 +00:00
|
|
|
return resultsMatch ? TestResult::Success : TestResult::Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTJSONTest::runTest(
|
2021-10-27 11:22:02 +00:00
|
|
|
TestVariant& _variant,
|
2023-08-30 00:08:06 +00:00
|
|
|
std::map<std::string, unsigned> const& _sourceIndices,
|
2020-08-26 17:01:25 +00:00
|
|
|
CompilerStack& _compiler,
|
2023-08-30 00:08:06 +00:00
|
|
|
std::ostream& _stream,
|
|
|
|
std::string const& _linePrefix,
|
2020-08-26 17:01:25 +00:00
|
|
|
bool const _formatted
|
|
|
|
)
|
|
|
|
{
|
2020-05-19 12:26:08 +00:00
|
|
|
if (m_sources.size() > 1)
|
2021-10-27 11:22:02 +00:00
|
|
|
_variant.result += "[\n";
|
2020-05-19 12:26:08 +00:00
|
|
|
|
2018-08-03 17:30:18 +00:00
|
|
|
for (size_t i = 0; i < m_sources.size(); i++)
|
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::ostringstream result;
|
2022-06-16 16:05:51 +00:00
|
|
|
ASTJsonExporter(_compiler.state(), _sourceIndices).print(result, _compiler.ast(m_sources[i].first), JsonFormat{ JsonFormat::Pretty });
|
2021-10-27 11:22:02 +00:00
|
|
|
_variant.result += result.str();
|
2018-08-03 17:30:18 +00:00
|
|
|
if (i != m_sources.size() - 1)
|
2021-10-27 11:22:02 +00:00
|
|
|
_variant.result += ",";
|
|
|
|
_variant.result += "\n";
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:26:08 +00:00
|
|
|
if (m_sources.size() > 1)
|
2021-10-27 11:22:02 +00:00
|
|
|
_variant.result += "]\n";
|
2020-05-19 12:26:08 +00:00
|
|
|
|
2021-10-27 11:22:02 +00:00
|
|
|
replaceTagWithVersion(_variant.expectation);
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2021-10-27 11:22:02 +00:00
|
|
|
if (_variant.expectation != _variant.result)
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string nextIndentLevel = _linePrefix + " ";
|
2020-08-26 17:01:25 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) <<
|
|
|
|
_linePrefix <<
|
|
|
|
"Expected result" <<
|
2021-10-27 11:22:02 +00:00
|
|
|
(!_variant.name().empty() ? " (" + _variant.name() + "):" : ":") <<
|
2023-08-30 00:08:06 +00:00
|
|
|
std::endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::istringstream stream(_variant.expectation);
|
|
|
|
std::string line;
|
2018-08-03 17:30:18 +00:00
|
|
|
while (getline(stream, line))
|
2023-08-30 00:08:06 +00:00
|
|
|
_stream << nextIndentLevel << line << std::endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
2023-08-30 00:08:06 +00:00
|
|
|
_stream << std::endl;
|
2020-01-14 11:46:47 +00:00
|
|
|
|
2020-08-26 17:01:25 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) <<
|
|
|
|
_linePrefix <<
|
|
|
|
"Obtained result" <<
|
2021-10-27 11:22:02 +00:00
|
|
|
(!_variant.name().empty() ? " (" + _variant.name() + "):" : ":") <<
|
2023-08-30 00:08:06 +00:00
|
|
|
std::endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
std::istringstream stream(_variant.result);
|
|
|
|
std::string line;
|
2018-08-03 17:30:18 +00:00
|
|
|
while (getline(stream, line))
|
2023-08-30 00:08:06 +00:00
|
|
|
_stream << nextIndentLevel << line << std::endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
2023-08-30 00:08:06 +00:00
|
|
|
_stream << std::endl;
|
2020-08-26 17:01:25 +00:00
|
|
|
return false;
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 20:08:50 +00:00
|
|
|
return true;
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void ASTJSONTest::printSource(std::ostream& _stream, std::string const& _linePrefix, bool const) const
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
|
|
|
for (auto const& source: m_sources)
|
|
|
|
{
|
|
|
|
if (m_sources.size() > 1 || source.first != "a")
|
2023-08-30 00:08:06 +00:00
|
|
|
_stream << _linePrefix << sourceDelimiter << source.first << " ====" << std::endl << std::endl;
|
|
|
|
std::stringstream stream(source.second);
|
|
|
|
std::string line;
|
2018-08-03 17:30:18 +00:00
|
|
|
while (getline(stream, line))
|
2023-08-30 00:08:06 +00:00
|
|
|
_stream << _linePrefix << line << std::endl;
|
|
|
|
_stream << std::endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTJSONTest::printUpdatedExpectations(std::ostream&, std::string const&) const
|
|
|
|
{
|
2021-10-27 11:22:02 +00:00
|
|
|
for (TestVariant const& variant: m_variants)
|
|
|
|
updateExpectation(
|
|
|
|
variant.astFilename(),
|
|
|
|
variant.result,
|
|
|
|
variant.name().empty() ? "" : variant.name() + " "
|
|
|
|
);
|
2020-08-26 17:01:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
void ASTJSONTest::updateExpectation(std::string const& _filename, std::string const& _expectation, std::string const& _variant) const
|
2020-08-26 17:01:25 +00:00
|
|
|
{
|
2023-08-30 00:08:06 +00:00
|
|
|
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);
|
2020-01-14 11:46:47 +00:00
|
|
|
|
2023-08-30 00:08:06 +00:00
|
|
|
std::string replacedResult = _expectation;
|
2020-01-14 11:46:47 +00:00
|
|
|
replaceVersionWithTag(replacedResult);
|
|
|
|
|
|
|
|
file << replacedResult;
|
2018-08-03 17:30:18 +00:00
|
|
|
file.flush();
|
|
|
|
file.close();
|
|
|
|
}
|