solidity/test/CommonSyntaxTest.cpp

296 lines
8.7 KiB
C++
Raw Permalink Normal View History

2020-02-04 18:40:32 +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/>.
*/
// SPDX-License-Identifier: GPL-3.0
2020-02-04 18:40:32 +00:00
#include <test/CommonSyntaxTest.h>
#include <test/Common.h>
#include <test/TestCase.h>
#include <libsolutil/CommonIO.h>
2020-02-04 18:40:32 +00:00
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/throw_exception.hpp>
#include <fstream>
#include <memory>
#include <stdexcept>
using namespace std;
using namespace solidity;
using namespace solidity::util;
2020-02-04 18:40:32 +00:00
using namespace solidity::util::formatting;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace solidity::test;
using namespace boost::unit_test;
namespace fs = boost::filesystem;
namespace
{
int parseUnsignedInteger(string::iterator& _it, string::iterator _end)
{
2022-03-10 22:36:46 +00:00
if (_it == _end || !util::isDigit(*_it))
BOOST_THROW_EXCEPTION(runtime_error("Invalid test expectation. Source location expected."));
2020-02-04 18:40:32 +00:00
int result = 0;
2022-03-10 22:36:46 +00:00
while (_it != _end && util::isDigit(*_it))
2020-02-04 18:40:32 +00:00
{
result *= 10;
result += *_it - '0';
++_it;
}
return result;
}
}
2020-03-06 00:22:51 +00:00
CommonSyntaxTest::CommonSyntaxTest(string const& _filename, langutil::EVMVersion _evmVersion):
EVMVersionRestrictedTestCase(_filename),
m_sources(m_reader.sources()),
m_expectations(parseExpectations(m_reader.stream())),
2020-03-06 00:22:51 +00:00
m_evmVersion(_evmVersion)
2020-02-04 18:40:32 +00:00
{
}
TestCase::TestResult CommonSyntaxTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
{
parseCustomExpectations(m_reader.stream());
2020-02-04 18:40:32 +00:00
parseAndAnalyze();
return conclude(_stream, _linePrefix, _formatted);
2020-02-04 18:40:32 +00:00
}
TestCase::TestResult CommonSyntaxTest::conclude(ostream& _stream, string const& _linePrefix, bool _formatted)
2020-02-04 18:40:32 +00:00
{
if (expectationsMatch())
return TestResult::Success;
printExpectationAndError(_stream, _linePrefix, _formatted);
return TestResult::Failure;
}
void CommonSyntaxTest::printExpectationAndError(ostream& _stream, string const& _linePrefix, bool _formatted)
{
string nextIndentLevel = _linePrefix + " ";
2022-03-10 23:16:12 +00:00
util::AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
printExpectedResult(_stream, nextIndentLevel, _formatted);
2022-03-10 23:16:12 +00:00
util::AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
printObtainedResult(_stream, nextIndentLevel, _formatted);
2020-02-04 18:40:32 +00:00
}
void CommonSyntaxTest::printSource(ostream& _stream, string const& _linePrefix, bool _formatted) const
{
if (m_sources.sources.empty())
2020-02-04 18:40:32 +00:00
return;
assert(m_sources.externalSources.empty());
bool outputSourceNames = (m_sources.sources.size() != 1 || !m_sources.sources.begin()->first.empty());
2020-02-04 18:40:32 +00:00
for (auto const& [name, source]: m_sources.sources)
if (_formatted)
2020-02-04 18:40:32 +00:00
{
if (source.empty())
continue;
if (outputSourceNames)
2022-03-10 23:16:12 +00:00
_stream << _linePrefix << util::formatting::CYAN << "==== Source: " << name << " ====" << util::formatting::RESET << endl;
vector<char const*> sourceFormatting(source.length(), util::formatting::RESET);
2020-02-04 18:40:32 +00:00
for (auto const& error: m_errorList)
if (error.sourceName == name && error.locationStart >= 0 && error.locationEnd >= 0)
{
assert(static_cast<size_t>(error.locationStart) <= source.length());
assert(static_cast<size_t>(error.locationEnd) <= source.length());
bool isWarning = error.type == "Warning";
for (int i = error.locationStart; i < error.locationEnd; i++)
if (isWarning)
{
2022-03-10 23:16:12 +00:00
if (sourceFormatting[static_cast<size_t>(i)] == util::formatting::RESET)
sourceFormatting[static_cast<size_t>(i)] = util::formatting::ORANGE_BACKGROUND_256;
2020-02-04 18:40:32 +00:00
}
else
2022-03-10 23:16:12 +00:00
sourceFormatting[static_cast<size_t>(i)] = util::formatting::RED_BACKGROUND;
2020-02-04 18:40:32 +00:00
}
_stream << _linePrefix << sourceFormatting.front() << source.front();
for (size_t i = 1; i < source.length(); i++)
{
if (sourceFormatting[i] != sourceFormatting[i - 1])
_stream << sourceFormatting[i];
if (source[i] != '\n')
_stream << source[i];
else
{
2022-03-10 23:16:12 +00:00
_stream << util::formatting::RESET << endl;
2020-02-04 18:40:32 +00:00
if (i + 1 < source.length())
_stream << _linePrefix << sourceFormatting[i];
}
}
2022-03-10 23:16:12 +00:00
_stream << util::formatting::RESET;
2020-02-04 18:40:32 +00:00
}
else
2020-02-04 18:40:32 +00:00
{
if (outputSourceNames)
_stream << _linePrefix << "==== Source: " + name << " ====" << endl;
stringstream stream(source);
string line;
while (getline(stream, line))
_stream << _linePrefix << line << endl;
}
}
void CommonSyntaxTest::parseCustomExpectations(istream& _stream)
{
string remainingExpectations = boost::trim_copy(readUntilEnd(_stream));
soltestAssert(
remainingExpectations.empty(),
"Found custom expectations not supported by the test case:\n" + remainingExpectations
);
}
bool CommonSyntaxTest::expectationsMatch()
{
return m_expectations == m_errorList;
}
void CommonSyntaxTest::printExpectedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const
{
printErrorList(_stream, m_expectations, _linePrefix, _formatted);
}
void CommonSyntaxTest::printObtainedResult(ostream& _stream, string const& _linePrefix, bool _formatted) const
{
printErrorList(_stream, m_errorList, _linePrefix, _formatted);
}
2020-02-04 18:40:32 +00:00
void CommonSyntaxTest::printErrorList(
ostream& _stream,
vector<SyntaxTestError> const& _errorList,
string const& _linePrefix,
bool _formatted
)
{
if (_errorList.empty())
{
if (_formatted)
util::AnsiColorized(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl;
}
2020-02-04 18:40:32 +00:00
else
for (auto const& error: _errorList)
{
{
2022-03-10 23:16:12 +00:00
util::AnsiColorized scope(_stream, _formatted, {BOLD, (error.type == "Warning") ? YELLOW : RED});
_stream << _linePrefix << error.type;
if (error.errorId.has_value())
_stream << ' ' << error.errorId->error;
_stream << ": ";
2020-02-04 18:40:32 +00:00
}
if (!error.sourceName.empty() || error.locationStart >= 0 || error.locationEnd >= 0)
{
_stream << "(";
if (!error.sourceName.empty())
_stream << error.sourceName << ":";
if (error.locationStart >= 0)
_stream << error.locationStart;
_stream << "-";
if (error.locationEnd >= 0)
_stream << error.locationEnd;
_stream << "): ";
}
_stream << error.message << endl;
}
}
2022-03-10 23:16:12 +00:00
string CommonSyntaxTest::errorMessage(util::Exception const& _e)
2020-02-04 18:40:32 +00:00
{
if (_e.comment() && !_e.comment()->empty())
return boost::replace_all_copy(*_e.comment(), "\n", "\\n");
else
return "NONE";
}
vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
{
static string const customExpectationsDelimiter("// ----");
2020-02-04 18:40:32 +00:00
vector<SyntaxTestError> expectations;
string line;
while (getline(_stream, line))
{
auto it = line.begin();
// Anything below the delimiter is left up to the derived class to process in a custom way.
// The delimiter is optional and identical to the one that starts error expectations in
// TestCaseReader::parseSourcesAndSettingsWithLineNumber().
if (boost::algorithm::starts_with(line, customExpectationsDelimiter))
break;
2020-02-04 18:40:32 +00:00
skipSlashes(it, line.end());
skipWhitespace(it, line.end());
if (it == line.end()) continue;
auto typeBegin = it;
while (it != line.end() && isalpha(*it, locale::classic()))
2020-02-04 18:40:32 +00:00
++it;
string errorType(typeBegin, it);
skipWhitespace(it, line.end());
optional<ErrorId> errorId;
2022-03-10 22:36:46 +00:00
if (it != line.end() && util::isDigit(*it))
errorId = ErrorId{static_cast<unsigned long long>(parseUnsignedInteger(it, line.end()))};
2020-02-04 18:40:32 +00:00
expect(it, line.end(), ':');
2020-02-04 18:40:32 +00:00
skipWhitespace(it, line.end());
int locationStart = -1;
int locationEnd = -1;
std::string sourceName;
if (it != line.end() && *it == '(')
{
++it;
2022-03-10 22:36:46 +00:00
if (it != line.end() && !util::isDigit(*it))
2020-02-04 18:40:32 +00:00
{
auto sourceNameStart = it;
while (it != line.end() && *it != ':')
++it;
sourceName = std::string(sourceNameStart, it);
expect(it, line.end(), ':');
}
locationStart = parseUnsignedInteger(it, line.end());
expect(it, line.end(), '-');
locationEnd = parseUnsignedInteger(it, line.end());
expect(it, line.end(), ')');
expect(it, line.end(), ':');
}
skipWhitespace(it, line.end());
string errorMessage(it, line.end());
expectations.emplace_back(SyntaxTestError{
2022-08-23 17:28:45 +00:00
std::move(errorType),
std::move(errorId),
std::move(errorMessage),
std::move(sourceName),
2020-02-04 18:40:32 +00:00
locationStart,
locationEnd
});
}
return expectations;
}