2018-03-12 12:33:37 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/libsolidity/SyntaxTest.h>
|
2018-04-03 10:05:26 +00:00
|
|
|
#include <test/Options.h>
|
2018-03-12 12:33:37 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
#include <cctype>
|
|
|
|
#include <fstream>
|
2018-03-13 16:54:22 +00:00
|
|
|
#include <memory>
|
2018-03-12 12:33:37 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace dev::solidity::test;
|
2018-03-12 12:57:48 +00:00
|
|
|
using namespace dev::solidity::test::formatting;
|
2018-03-12 12:33:37 +00:00
|
|
|
using namespace std;
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
using namespace boost::unit_test;
|
|
|
|
|
|
|
|
template<typename IteratorType>
|
2018-04-06 16:37:01 +00:00
|
|
|
void skipWhitespace(IteratorType& _it, IteratorType _end)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-06 16:37:01 +00:00
|
|
|
while (_it != _end && isspace(*_it))
|
|
|
|
++_it;
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename IteratorType>
|
2018-04-06 16:37:01 +00:00
|
|
|
void skipSlashes(IteratorType& _it, IteratorType _end)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-06 16:37:01 +00:00
|
|
|
while (_it != _end && *_it == '/')
|
|
|
|
++_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
void 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parseUnsignedInteger(string::iterator &_it, string::iterator _end)
|
|
|
|
{
|
|
|
|
if (_it == _end || !isdigit(*_it))
|
|
|
|
throw runtime_error("Invalid test expectation. Source location expected.");
|
|
|
|
int result = 0;
|
|
|
|
while (_it != _end && isdigit(*_it))
|
|
|
|
{
|
|
|
|
result *= 10;
|
|
|
|
result += *_it - '0';
|
|
|
|
++_it;
|
|
|
|
}
|
|
|
|
return result;
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SyntaxTest::SyntaxTest(string const& _filename)
|
|
|
|
{
|
|
|
|
ifstream file(_filename);
|
|
|
|
if (!file)
|
2018-03-13 11:30:56 +00:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Cannot open test contract: \"" + _filename + "\"."));
|
2018-03-12 12:33:37 +00:00
|
|
|
file.exceptions(ios::badbit);
|
|
|
|
|
|
|
|
m_source = parseSource(file);
|
|
|
|
m_expectations = parseExpectations(file);
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:57:48 +00:00
|
|
|
bool SyntaxTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-06 16:37:01 +00:00
|
|
|
string const versionPragma = "pragma solidity >=0.0;\n";
|
2018-04-03 10:05:26 +00:00
|
|
|
m_compiler.reset();
|
2018-04-06 16:37:01 +00:00
|
|
|
m_compiler.addSource("", versionPragma + m_source);
|
2018-04-03 10:05:26 +00:00
|
|
|
m_compiler.setEVMVersion(dev::test::Options::get().evmVersion());
|
|
|
|
|
|
|
|
if (m_compiler.parse())
|
|
|
|
m_compiler.analyze();
|
|
|
|
|
|
|
|
for (auto const& currentError: filterErrors(m_compiler.errors(), true))
|
2018-04-06 16:37:01 +00:00
|
|
|
{
|
|
|
|
int locationStart = -1, locationEnd = -1;
|
|
|
|
if (auto location = boost::get_error_info<errinfo_sourceLocation>(*currentError))
|
|
|
|
{
|
|
|
|
// ignore the version pragma inserted by the testing tool when calculating locations.
|
|
|
|
if (location->start >= static_cast<int>(versionPragma.size()))
|
|
|
|
locationStart = location->start - versionPragma.size();
|
|
|
|
if (location->end >= static_cast<int>(versionPragma.size()))
|
|
|
|
locationEnd = location->end - versionPragma.size();
|
|
|
|
}
|
|
|
|
m_errorList.emplace_back(SyntaxTestError{
|
|
|
|
currentError->typeName(),
|
|
|
|
errorMessage(*currentError),
|
|
|
|
locationStart,
|
|
|
|
locationEnd
|
|
|
|
});
|
|
|
|
}
|
2018-04-03 10:05:26 +00:00
|
|
|
|
|
|
|
if (m_expectations != m_errorList)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
string nextIndentLevel = _linePrefix + " ";
|
2018-03-12 12:57:48 +00:00
|
|
|
FormattedScope(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
printErrorList(_stream, m_expectations, nextIndentLevel, _formatted);
|
2018-04-06 16:37:01 +00:00
|
|
|
FormattedScope(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
|
2018-04-03 10:05:26 +00:00
|
|
|
printErrorList(_stream, m_errorList, nextIndentLevel, _formatted);
|
2018-03-12 12:33:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyntaxTest::printErrorList(
|
|
|
|
ostream& _stream,
|
2018-04-03 10:05:26 +00:00
|
|
|
vector<SyntaxTestError> const& _errorList,
|
2018-03-12 12:57:48 +00:00
|
|
|
string const& _linePrefix,
|
|
|
|
bool const _formatted
|
2018-04-03 10:05:26 +00:00
|
|
|
)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
|
|
|
if (_errorList.empty())
|
2018-03-12 12:57:48 +00:00
|
|
|
FormattedScope(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl;
|
2018-03-12 12:33:37 +00:00
|
|
|
else
|
|
|
|
for (auto const& error: _errorList)
|
2018-03-12 12:57:48 +00:00
|
|
|
{
|
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
FormattedScope scope(_stream, _formatted, {BOLD, (error.type == "Warning") ? YELLOW : RED});
|
2018-03-15 15:27:34 +00:00
|
|
|
_stream << _linePrefix;
|
2018-04-03 10:05:26 +00:00
|
|
|
_stream << error.type << ": ";
|
2018-03-12 12:57:48 +00:00
|
|
|
}
|
2018-04-06 16:37:01 +00:00
|
|
|
if (error.locationStart >= 0 || error.locationEnd >= 0)
|
|
|
|
{
|
|
|
|
_stream << "(";
|
|
|
|
if (error.locationStart >= 0)
|
|
|
|
_stream << error.locationStart;
|
|
|
|
_stream << "-";
|
|
|
|
if (error.locationEnd >= 0)
|
|
|
|
_stream << error.locationEnd;
|
|
|
|
_stream << "): ";
|
|
|
|
}
|
2018-04-03 10:05:26 +00:00
|
|
|
_stream << error.message << endl;
|
2018-03-12 12:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
string SyntaxTest::errorMessage(Exception const& _e)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
if (_e.comment() && !_e.comment()->empty())
|
2018-03-12 12:33:37 +00:00
|
|
|
return boost::replace_all_copy(*_e.comment(), "\n", "\\n");
|
|
|
|
else
|
|
|
|
return "NONE";
|
|
|
|
}
|
|
|
|
|
|
|
|
string SyntaxTest::parseSource(istream& _stream)
|
|
|
|
{
|
|
|
|
string source;
|
|
|
|
string line;
|
|
|
|
string const delimiter("// ----");
|
|
|
|
while (getline(_stream, line))
|
|
|
|
if (boost::algorithm::starts_with(line, delimiter))
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
source += line + "\n";
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2018-04-03 10:05:26 +00:00
|
|
|
vector<SyntaxTestError> SyntaxTest::parseExpectations(istream& _stream)
|
2018-03-12 12:33:37 +00:00
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
vector<SyntaxTestError> expectations;
|
2018-03-12 12:33:37 +00:00
|
|
|
string line;
|
|
|
|
while (getline(_stream, line))
|
|
|
|
{
|
|
|
|
auto it = line.begin();
|
|
|
|
|
|
|
|
skipSlashes(it, line.end());
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
|
|
|
|
if (it == line.end()) continue;
|
|
|
|
|
|
|
|
auto typeBegin = it;
|
|
|
|
while (it != line.end() && *it != ':')
|
|
|
|
++it;
|
|
|
|
string errorType(typeBegin, it);
|
|
|
|
|
|
|
|
// skip colon
|
|
|
|
if (it != line.end()) it++;
|
|
|
|
|
|
|
|
skipWhitespace(it, line.end());
|
|
|
|
|
2018-04-06 16:37:01 +00:00
|
|
|
int locationStart = -1;
|
|
|
|
int locationEnd = -1;
|
|
|
|
|
|
|
|
if (it != line.end() && *it == '(')
|
|
|
|
{
|
|
|
|
++it;
|
|
|
|
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());
|
|
|
|
|
2018-03-12 12:33:37 +00:00
|
|
|
string errorMessage(it, line.end());
|
2018-04-06 16:37:01 +00:00
|
|
|
expectations.emplace_back(SyntaxTestError{
|
|
|
|
move(errorType),
|
|
|
|
move(errorMessage),
|
|
|
|
locationStart,
|
|
|
|
locationEnd
|
|
|
|
});
|
2018-03-12 12:33:37 +00:00
|
|
|
}
|
|
|
|
return expectations;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if BOOST_VERSION < 105900
|
|
|
|
test_case *make_test_case(
|
|
|
|
function<void()> const& _fn,
|
|
|
|
string const& _name,
|
|
|
|
string const& /* _filename */,
|
|
|
|
size_t /* _line */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return make_test_case(_fn, _name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-16 10:40:03 +00:00
|
|
|
bool SyntaxTest::isTestFilename(boost::filesystem::path const& _filename)
|
|
|
|
{
|
|
|
|
return _filename.extension().string() == ".sol" &&
|
|
|
|
!boost::starts_with(_filename.string(), "~") &&
|
|
|
|
!boost::starts_with(_filename.string(), ".");
|
|
|
|
}
|
|
|
|
|
2018-03-12 12:33:37 +00:00
|
|
|
int SyntaxTest::registerTests(
|
|
|
|
boost::unit_test::test_suite& _suite,
|
|
|
|
boost::filesystem::path const& _basepath,
|
|
|
|
boost::filesystem::path const& _path
|
|
|
|
)
|
|
|
|
{
|
|
|
|
int numTestsAdded = 0;
|
|
|
|
fs::path fullpath = _basepath / _path;
|
|
|
|
if (fs::is_directory(fullpath))
|
|
|
|
{
|
|
|
|
test_suite* sub_suite = BOOST_TEST_SUITE(_path.filename().string());
|
|
|
|
for (auto const& entry: boost::iterator_range<fs::directory_iterator>(
|
|
|
|
fs::directory_iterator(fullpath),
|
|
|
|
fs::directory_iterator()
|
|
|
|
))
|
2018-03-16 10:40:03 +00:00
|
|
|
if (fs::is_directory(entry.path()) || isTestFilename(entry.path().filename()))
|
|
|
|
numTestsAdded += registerTests(*sub_suite, _basepath, _path / entry.path().filename());
|
2018-03-12 12:33:37 +00:00
|
|
|
_suite.add(sub_suite);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-13 16:54:22 +00:00
|
|
|
static vector<unique_ptr<string>> filenames;
|
|
|
|
|
|
|
|
filenames.emplace_back(new string(_path.string()));
|
2018-03-12 12:33:37 +00:00
|
|
|
_suite.add(make_test_case(
|
|
|
|
[fullpath]
|
|
|
|
{
|
2018-04-03 10:05:26 +00:00
|
|
|
BOOST_REQUIRE_NO_THROW({
|
2018-06-12 09:06:14 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
stringstream errorStream;
|
|
|
|
if (!SyntaxTest(fullpath.string()).run(errorStream))
|
|
|
|
BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str());
|
|
|
|
}
|
|
|
|
catch (boost::exception const& _e)
|
|
|
|
{
|
|
|
|
BOOST_ERROR("Exception during syntax test: " << boost::diagnostic_information(_e));
|
|
|
|
}
|
2018-04-03 10:05:26 +00:00
|
|
|
});
|
2018-03-12 12:33:37 +00:00
|
|
|
},
|
|
|
|
_path.stem().string(),
|
2018-03-13 16:54:22 +00:00
|
|
|
*filenames.back(),
|
2018-03-12 12:33:37 +00:00
|
|
|
0
|
|
|
|
));
|
|
|
|
numTestsAdded = 1;
|
|
|
|
}
|
|
|
|
return numTestsAdded;
|
|
|
|
}
|