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-01-14 11:46:47 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2018-08-03 17:30:18 +00:00
|
|
|
#include <test/libsolidity/ASTJSONTest.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/AnsiColorized.h>
|
2020-12-01 13:22:15 +00:00
|
|
|
#include <liblangutil/SourceReferenceFormatter.h>
|
2018-08-03 17:30:18 +00:00
|
|
|
#include <libsolidity/ast/ASTJsonConverter.h>
|
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/throw_exception.hpp>
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2018-08-03 17:30:18 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
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;
|
|
|
|
using namespace solidity;
|
2018-08-03 17:30:18 +00:00
|
|
|
using namespace std;
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using namespace boost::unit_test;
|
|
|
|
|
2020-01-14 11:46:47 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-09-03 09:39:56 +00:00
|
|
|
string const sourceDelimiter("==== Source: ");
|
|
|
|
|
2020-01-14 11:46:47 +00:00
|
|
|
void replaceVersionWithTag(string& _input)
|
|
|
|
{
|
|
|
|
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%"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void replaceTagWithVersion(string& _input)
|
|
|
|
{
|
|
|
|
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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-03 17:30:18 +00:00
|
|
|
ASTJSONTest::ASTJSONTest(string const& _filename)
|
|
|
|
{
|
|
|
|
if (!boost::algorithm::ends_with(_filename, ".sol"))
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Invalid test contract file name: \"" + _filename + "\"."));
|
|
|
|
|
|
|
|
m_astFilename = _filename.substr(0, _filename.size() - 4) + ".json";
|
2020-07-08 20:08:50 +00:00
|
|
|
m_astParseOnlyFilename = _filename.substr(0, _filename.size() - 4) + "_parseOnly.json";
|
2018-08-03 17:30:18 +00:00
|
|
|
|
|
|
|
ifstream file(_filename);
|
|
|
|
if (!file)
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test contract: \"" + _filename + "\"."));
|
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
|
|
|
|
string sourceName;
|
|
|
|
string source;
|
|
|
|
string line;
|
|
|
|
string const delimiter("// ----");
|
|
|
|
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()
|
|
|
|
);
|
2018-08-03 17:30:18 +00:00
|
|
|
source = string();
|
|
|
|
}
|
|
|
|
else if (!line.empty() && !boost::algorithm::starts_with(line, delimiter))
|
|
|
|
source += line + "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
m_sources.emplace_back(sourceName.empty() ? "a" : sourceName, source);
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
file.open(m_astFilename);
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
while (getline(file, line))
|
|
|
|
m_expectation += line + "\n";
|
|
|
|
}
|
|
|
|
|
2020-07-08 20:08:50 +00:00
|
|
|
file.close();
|
|
|
|
file.open(m_astParseOnlyFilename);
|
|
|
|
if (file)
|
|
|
|
{
|
|
|
|
string line;
|
|
|
|
while (getline(file, line))
|
|
|
|
m_expectationParseOnly += line + "\n";
|
|
|
|
}
|
|
|
|
|
2018-08-03 17:30:18 +00:00
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult ASTJSONTest::run(ostream& _stream, 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;
|
2018-08-03 17:30:18 +00:00
|
|
|
map<string, unsigned> sourceIndices;
|
|
|
|
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;
|
2018-08-03 17:41:35 +00:00
|
|
|
sourceIndices[m_sources[i].first] = i + 1;
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
2019-03-20 18:07:12 +00:00
|
|
|
c.setSources(sources);
|
2020-01-14 16:48:17 +00:00
|
|
|
c.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
2020-08-26 17:01:25 +00:00
|
|
|
|
2020-07-08 20:08:50 +00:00
|
|
|
|
|
|
|
if (!c.compile(CompilerStack::State::Parsed))
|
|
|
|
{
|
2020-12-01 13:22:15 +00:00
|
|
|
SourceReferenceFormatter formatter(_stream, _formatted, false);
|
2020-07-08 20:08:50 +00:00
|
|
|
for (auto const& error: c.errors())
|
|
|
|
formatter.printErrorInformation(*error);
|
|
|
|
return TestResult::FatalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool resultsMatch = runTest(
|
|
|
|
m_expectationParseOnly,
|
|
|
|
m_resultParseOnly,
|
|
|
|
sourceIndices,
|
|
|
|
c,
|
|
|
|
"parseOnly",
|
|
|
|
_stream,
|
|
|
|
_linePrefix,
|
|
|
|
_formatted
|
|
|
|
);
|
|
|
|
|
|
|
|
c.reset();
|
|
|
|
c.setSources(sources);
|
|
|
|
c.setEVMVersion(solidity::test::CommonOptions::get().evmVersion());
|
2020-08-26 17:01:25 +00:00
|
|
|
if (!c.parse())
|
2019-05-07 13:33:22 +00:00
|
|
|
{
|
2020-07-08 20:08:50 +00:00
|
|
|
// Empty Expectations means we expect failure
|
2020-11-20 15:54:53 +00:00
|
|
|
if (m_expectation.empty())
|
2020-07-08 20:08:50 +00:00
|
|
|
return resultsMatch ? TestResult::Success : TestResult::Failure;
|
|
|
|
|
2020-12-01 13:22:15 +00:00
|
|
|
SourceReferenceFormatter formatter(_stream, _formatted, false);
|
2019-05-07 13:33:22 +00:00
|
|
|
for (auto const& error: c.errors())
|
|
|
|
formatter.printErrorInformation(*error);
|
|
|
|
return TestResult::FatalError;
|
|
|
|
}
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2020-08-26 17:01:25 +00:00
|
|
|
c.analyze();
|
|
|
|
|
2020-07-08 20:08:50 +00:00
|
|
|
resultsMatch = runTest(
|
2020-08-26 17:01:25 +00:00
|
|
|
m_expectation,
|
|
|
|
m_result,
|
|
|
|
sourceIndices,
|
|
|
|
c,
|
|
|
|
"",
|
|
|
|
_stream,
|
|
|
|
_linePrefix,
|
|
|
|
_formatted
|
2020-07-08 20:08:50 +00:00
|
|
|
) && resultsMatch;
|
2020-08-26 17:01:25 +00:00
|
|
|
|
|
|
|
return resultsMatch ? TestResult::Success : TestResult::Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTJSONTest::runTest(
|
|
|
|
string& _expectation,
|
|
|
|
string& _result,
|
|
|
|
map<string, unsigned> const& _sourceIndices,
|
|
|
|
CompilerStack& _compiler,
|
|
|
|
string const& _variation,
|
|
|
|
ostream& _stream,
|
|
|
|
string const& _linePrefix,
|
|
|
|
bool const _formatted
|
|
|
|
)
|
|
|
|
{
|
2020-05-19 12:26:08 +00:00
|
|
|
if (m_sources.size() > 1)
|
2020-08-26 17:01:25 +00:00
|
|
|
_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++)
|
|
|
|
{
|
|
|
|
ostringstream result;
|
2020-11-20 15:54:53 +00:00
|
|
|
ASTJsonConverter(_compiler.state(), _sourceIndices).print(result, _compiler.ast(m_sources[i].first));
|
2020-08-26 17:01:25 +00:00
|
|
|
_result += result.str();
|
2018-08-03 17:30:18 +00:00
|
|
|
if (i != m_sources.size() - 1)
|
2020-08-26 17:01:25 +00:00
|
|
|
_result += ",";
|
|
|
|
_result += "\n";
|
2018-08-03 17:30:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:26:08 +00:00
|
|
|
if (m_sources.size() > 1)
|
2020-08-26 17:01:25 +00:00
|
|
|
_result += "]\n";
|
2020-05-19 12:26:08 +00:00
|
|
|
|
2020-08-26 17:01:25 +00:00
|
|
|
replaceTagWithVersion(_expectation);
|
2018-08-03 17:30:18 +00:00
|
|
|
|
2020-08-26 17:01:25 +00:00
|
|
|
if (_expectation != _result)
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
|
|
|
string nextIndentLevel = _linePrefix + " ";
|
2020-08-26 17:01:25 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) <<
|
|
|
|
_linePrefix <<
|
|
|
|
"Expected result" <<
|
|
|
|
(!_variation.empty() ? " (" + _variation + "):" : ":") <<
|
|
|
|
endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
2020-08-26 17:01:25 +00:00
|
|
|
istringstream stream(_expectation);
|
2018-08-03 17:30:18 +00:00
|
|
|
string line;
|
|
|
|
while (getline(stream, line))
|
|
|
|
_stream << nextIndentLevel << line << endl;
|
|
|
|
}
|
|
|
|
_stream << 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" <<
|
|
|
|
(!_variation.empty() ? " (" + _variation + "):" : ":") <<
|
|
|
|
endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
{
|
2020-08-26 17:01:25 +00:00
|
|
|
istringstream stream(_result);
|
2018-08-03 17:30:18 +00:00
|
|
|
string line;
|
|
|
|
while (getline(stream, line))
|
|
|
|
_stream << nextIndentLevel << line << endl;
|
|
|
|
}
|
|
|
|
_stream << 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
|
|
|
}
|
|
|
|
|
|
|
|
void ASTJSONTest::printSource(ostream& _stream, string const& _linePrefix, bool const) const
|
|
|
|
{
|
|
|
|
for (auto const& source: m_sources)
|
|
|
|
{
|
|
|
|
if (m_sources.size() > 1 || source.first != "a")
|
2020-09-03 09:39:56 +00:00
|
|
|
_stream << _linePrefix << sourceDelimiter << source.first << endl << endl;
|
2018-08-03 17:30:18 +00:00
|
|
|
stringstream stream(source.second);
|
|
|
|
string line;
|
|
|
|
while (getline(stream, line))
|
|
|
|
_stream << _linePrefix << line << endl;
|
|
|
|
_stream << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTJSONTest::printUpdatedExpectations(std::ostream&, std::string const&) const
|
|
|
|
{
|
2020-08-26 17:01:25 +00:00
|
|
|
updateExpectation(m_astFilename, m_result, "");
|
2020-07-08 20:08:50 +00:00
|
|
|
updateExpectation(m_astParseOnlyFilename, m_resultParseOnly, "parseOnly ");
|
2020-08-26 17:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTJSONTest::updateExpectation(string const& _filename, string const& _expectation, string const& _variation) const
|
|
|
|
{
|
|
|
|
ofstream file(_filename.c_str());
|
|
|
|
if (!file) BOOST_THROW_EXCEPTION(runtime_error("Cannot write " + _variation + "AST expectation to \"" + _filename + "\"."));
|
2018-08-03 17:30:18 +00:00
|
|
|
file.exceptions(ios::badbit);
|
2020-01-14 11:46:47 +00:00
|
|
|
|
2020-08-26 17:01:25 +00:00
|
|
|
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();
|
|
|
|
}
|