2018-06-08 12:17:50 +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/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-25 00:02:32 +00:00
|
|
|
#include <test/TestCase.h>
|
2018-06-08 12:17:50 +00:00
|
|
|
|
2019-04-01 13:31:48 +00:00
|
|
|
#include <libdevcore/StringUtils.h>
|
|
|
|
|
2019-03-18 13:08:56 +00:00
|
|
|
#include <boost/algorithm/cxx11/none_of.hpp>
|
2018-06-08 12:17:50 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2019-04-01 13:31:48 +00:00
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
#include <boost/range/adaptor/map.hpp>
|
2018-06-08 12:17:50 +00:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2019-04-01 13:31:48 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace dev::solidity::test;
|
|
|
|
using namespace std;
|
|
|
|
|
2019-04-01 14:33:46 +00:00
|
|
|
void TestCase::printUpdatedSettings(ostream& _stream, const string& _linePrefix, const bool)
|
2019-04-01 13:31:48 +00:00
|
|
|
{
|
|
|
|
if (m_validatedSettings.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_stream << _linePrefix << "// ====" << endl;
|
|
|
|
for (auto const& setting: m_validatedSettings)
|
|
|
|
_stream << _linePrefix << "// " << setting.first << ": " << setting.second << endl;
|
|
|
|
}
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
bool TestCase::isTestFilename(boost::filesystem::path const& _filename)
|
|
|
|
{
|
2018-10-09 13:43:55 +00:00
|
|
|
string extension = _filename.extension().string();
|
|
|
|
return (extension == ".sol" || extension == ".yul") &&
|
2018-06-08 12:17:50 +00:00
|
|
|
!boost::starts_with(_filename.string(), "~") &&
|
2019-04-01 13:31:48 +00:00
|
|
|
!boost::starts_with(_filename.string(), ".");
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 13:31:48 +00:00
|
|
|
bool TestCase::validateSettings(langutil::EVMVersion)
|
2019-03-15 16:22:04 +00:00
|
|
|
{
|
2019-04-01 13:31:48 +00:00
|
|
|
if (!m_settings.empty())
|
|
|
|
throw runtime_error(
|
|
|
|
"Unknown setting(s): " +
|
|
|
|
joinHumanReadable(m_settings | boost::adaptors::map_keys)
|
|
|
|
);
|
|
|
|
return true;
|
2019-03-15 16:22:04 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 12:50:31 +00:00
|
|
|
pair<map<string, string>, size_t> TestCase::parseSourcesAndSettingsWithLineNumbers(istream& _stream)
|
2018-06-08 12:17:50 +00:00
|
|
|
{
|
2019-08-16 12:50:31 +00:00
|
|
|
map<string, string> sources;
|
|
|
|
string currentSourceName;
|
|
|
|
string currentSource;
|
2018-06-08 12:17:50 +00:00
|
|
|
string line;
|
2019-07-13 10:04:22 +00:00
|
|
|
size_t lineNumber = 1;
|
2019-08-16 12:50:31 +00:00
|
|
|
static string const sourceDelimiterStart("==== Source:");
|
|
|
|
static string const sourceDelimiterEnd("====");
|
2019-05-08 08:46:19 +00:00
|
|
|
static string const comment("// ");
|
2019-04-01 13:31:48 +00:00
|
|
|
static string const settingsDelimiter("// ====");
|
2019-03-18 13:08:56 +00:00
|
|
|
static string const delimiter("// ----");
|
2019-04-01 13:31:48 +00:00
|
|
|
bool sourcePart = true;
|
2018-06-08 12:17:50 +00:00
|
|
|
while (getline(_stream, line))
|
2019-04-01 13:31:48 +00:00
|
|
|
{
|
2019-07-13 10:04:22 +00:00
|
|
|
lineNumber++;
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
if (boost::algorithm::starts_with(line, delimiter))
|
|
|
|
break;
|
2019-04-01 13:31:48 +00:00
|
|
|
else if (boost::algorithm::starts_with(line, settingsDelimiter))
|
|
|
|
sourcePart = false;
|
|
|
|
else if (sourcePart)
|
2019-08-16 12:50:31 +00:00
|
|
|
{
|
|
|
|
if (boost::algorithm::starts_with(line, sourceDelimiterStart) && boost::algorithm::ends_with(line, sourceDelimiterEnd))
|
|
|
|
{
|
|
|
|
if (!(currentSourceName.empty() && currentSource.empty()))
|
|
|
|
sources[currentSourceName] = std::move(currentSource);
|
|
|
|
currentSource = {};
|
|
|
|
currentSourceName = boost::trim_copy(line.substr(
|
|
|
|
sourceDelimiterStart.size(),
|
|
|
|
line.size() - sourceDelimiterEnd.size() - sourceDelimiterStart.size()
|
|
|
|
));
|
|
|
|
if (sources.count(currentSourceName))
|
|
|
|
throw runtime_error("Multiple definitions of test source \"" + currentSourceName + "\".");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
currentSource += line + "\n";
|
|
|
|
}
|
2019-05-08 08:46:19 +00:00
|
|
|
else if (boost::algorithm::starts_with(line, comment))
|
2019-04-01 13:31:48 +00:00
|
|
|
{
|
|
|
|
size_t colon = line.find(':');
|
|
|
|
if (colon == string::npos)
|
|
|
|
throw runtime_error(string("Expected \":\" inside setting."));
|
2019-05-08 08:46:19 +00:00
|
|
|
string key = line.substr(comment.size(), colon - comment.size());
|
2019-04-01 13:31:48 +00:00
|
|
|
string value = line.substr(colon + 1);
|
|
|
|
boost::algorithm::trim(key);
|
|
|
|
boost::algorithm::trim(value);
|
|
|
|
m_settings[key] = value;
|
2019-03-15 16:22:04 +00:00
|
|
|
}
|
2019-04-01 13:31:48 +00:00
|
|
|
else
|
|
|
|
throw runtime_error(string("Expected \"//\" or \"// ---\" to terminate settings and source."));
|
|
|
|
}
|
2019-08-16 12:50:31 +00:00
|
|
|
sources[currentSourceName] = currentSource;
|
|
|
|
return {sources, lineNumber};
|
|
|
|
}
|
|
|
|
|
|
|
|
map<string, string> TestCase::parseSourcesAndSettings(istream& _stream)
|
|
|
|
{
|
|
|
|
return get<0>(parseSourcesAndSettingsWithLineNumbers(_stream));
|
|
|
|
}
|
|
|
|
|
|
|
|
pair<string, size_t> TestCase::parseSourceAndSettingsWithLineNumbers(istream& _stream)
|
|
|
|
{
|
|
|
|
auto [sourceMap, lineOffset] = parseSourcesAndSettingsWithLineNumbers(_stream);
|
|
|
|
if (sourceMap.size() != 1)
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Expected single source definition, but got multiple sources."));
|
|
|
|
return {std::move(sourceMap.begin()->second), lineOffset};
|
2019-07-13 10:04:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string TestCase::parseSourceAndSettings(istream& _stream)
|
|
|
|
{
|
2019-08-16 12:50:31 +00:00
|
|
|
return parseSourceAndSettingsWithLineNumbers(_stream).first;
|
2018-06-08 12:17:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:05:45 +00:00
|
|
|
string TestCase::parseSimpleExpectations(std::istream& _file)
|
|
|
|
{
|
|
|
|
string result;
|
|
|
|
string line;
|
|
|
|
while (getline(_file, line))
|
|
|
|
if (boost::algorithm::starts_with(line, "// "))
|
|
|
|
result += line.substr(3) + "\n";
|
|
|
|
else if (line == "//")
|
|
|
|
result += "\n";
|
|
|
|
else
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Test expectations must start with \"// \"."));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-08 12:17:50 +00:00
|
|
|
void TestCase::expect(string::iterator& _it, string::iterator _end, string::value_type _c)
|
|
|
|
{
|
|
|
|
if (_it == _end || *_it != _c)
|
|
|
|
throw runtime_error(string("Invalid test expectation. Expected: \"") + _c + "\".");
|
|
|
|
++_it;
|
|
|
|
}
|
2019-04-01 13:31:48 +00:00
|
|
|
|
|
|
|
bool EVMVersionRestrictedTestCase::validateSettings(langutil::EVMVersion _evmVersion)
|
|
|
|
{
|
|
|
|
if (!m_settings.count("EVMVersion"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
string versionString = m_settings["EVMVersion"];
|
|
|
|
m_validatedSettings["EVMVersion"] = versionString;
|
|
|
|
m_settings.erase("EVMVersion");
|
|
|
|
|
|
|
|
if (!TestCase::validateSettings(_evmVersion))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (versionString.empty())
|
|
|
|
return true;
|
|
|
|
|
2019-04-24 10:08:49 +00:00
|
|
|
string comparator;
|
|
|
|
size_t versionBegin = 0;
|
|
|
|
for (auto character: versionString)
|
|
|
|
if (!isalpha(character))
|
|
|
|
{
|
|
|
|
comparator += character;
|
|
|
|
versionBegin++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
versionString = versionString.substr(versionBegin);
|
2019-04-01 13:31:48 +00:00
|
|
|
boost::optional<langutil::EVMVersion> version = langutil::EVMVersion::fromString(versionString);
|
|
|
|
if (!version)
|
|
|
|
throw runtime_error("Invalid EVM version: \"" + versionString + "\"");
|
|
|
|
|
2019-04-24 10:08:49 +00:00
|
|
|
if (comparator == ">")
|
|
|
|
return _evmVersion > version;
|
|
|
|
else if (comparator == ">=")
|
|
|
|
return _evmVersion >= version;
|
|
|
|
else if (comparator == "<")
|
|
|
|
return _evmVersion < version;
|
|
|
|
else if (comparator == "<=")
|
|
|
|
return _evmVersion <= version;
|
|
|
|
else if (comparator == "=")
|
|
|
|
return _evmVersion == version;
|
|
|
|
else if (comparator == "!")
|
|
|
|
return !(_evmVersion == version);
|
|
|
|
else
|
|
|
|
throw runtime_error("Invalid EVM comparator: \"" + comparator + "\"");
|
2019-04-01 13:31:48 +00:00
|
|
|
return false; // not reached
|
|
|
|
}
|