aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity/SyntaxTest.cpp
diff options
context:
space:
mode:
authorDaniel Kirchner <daniel@ekpyron.org>2018-04-03 18:05:26 +0800
committerDaniel Kirchner <daniel@ekpyron.org>2018-04-04 01:54:45 +0800
commit6f9644add18b27363a423e2c7ccb0e578ba800bc (patch)
treec6158178cf44a68563acd92ef5f3ccad98ce56c6 /test/libsolidity/SyntaxTest.cpp
parent104a9736b35495cf50bf1a895d61aed9a1ba830a (diff)
downloaddexon-solidity-6f9644add18b27363a423e2c7ccb0e578ba800bc.tar.gz
dexon-solidity-6f9644add18b27363a423e2c7ccb0e578ba800bc.tar.zst
dexon-solidity-6f9644add18b27363a423e2c7ccb0e578ba800bc.zip
SyntaxTests: extend syntax tests and isoltest to support parser errors and compiler exceptions.
Diffstat (limited to 'test/libsolidity/SyntaxTest.cpp')
-rw-r--r--test/libsolidity/SyntaxTest.cpp98
1 files changed, 30 insertions, 68 deletions
diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp
index ca051138..329543bf 100644
--- a/test/libsolidity/SyntaxTest.cpp
+++ b/test/libsolidity/SyntaxTest.cpp
@@ -16,6 +16,7 @@
*/
#include <test/libsolidity/SyntaxTest.h>
+#include <test/Options.h>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/throw_exception.hpp>
@@ -59,93 +60,52 @@ SyntaxTest::SyntaxTest(string const& _filename)
bool SyntaxTest::run(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
- m_errorList = parseAnalyseAndReturnError(m_source, true, true, true).second;
- if (!matchesExpectations(m_errorList))
+ m_compiler.reset();
+ m_compiler.addSource("", "pragma solidity >=0.0;\n" + m_source);
+ m_compiler.setEVMVersion(dev::test::Options::get().evmVersion());
+
+ if (m_compiler.parse())
+ m_compiler.analyze();
+
+ for (auto const& currentError: filterErrors(m_compiler.errors(), true))
+ m_errorList.emplace_back(SyntaxTestError{currentError->typeName(), errorMessage(*currentError)});
+
+ if (m_expectations != m_errorList)
{
- std::string nextIndentLevel = _linePrefix + " ";
+ string nextIndentLevel = _linePrefix + " ";
FormattedScope(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
- printExpected(_stream, nextIndentLevel, _formatted);
+ printErrorList(_stream, m_expectations, nextIndentLevel, _formatted);
FormattedScope(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:\n";
- printErrorList(_stream, m_errorList, nextIndentLevel, false, false, _formatted);
+ printErrorList(_stream, m_errorList, nextIndentLevel, _formatted);
return false;
}
return true;
}
-void SyntaxTest::printExpected(ostream& _stream, string const& _linePrefix, bool const _formatted) const
-{
- if (m_expectations.empty())
- FormattedScope(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl;
- else
- for (auto const& expectation: m_expectations)
- {
- FormattedScope(_stream, _formatted, {BOLD, expectation.type == "Warning" ? YELLOW : RED}) <<
- _linePrefix << expectation.type << ": ";
- _stream << expectation.message << endl;
- }
-}
-
void SyntaxTest::printErrorList(
ostream& _stream,
- ErrorList const& _errorList,
+ vector<SyntaxTestError> const& _errorList,
string const& _linePrefix,
- bool const _ignoreWarnings,
- bool const _lineNumbers,
bool const _formatted
-) const
+)
{
if (_errorList.empty())
FormattedScope(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl;
else
for (auto const& error: _errorList)
{
- bool isWarning = (error->type() == Error::Type::Warning);
- if (isWarning && _ignoreWarnings) continue;
-
{
- FormattedScope scope(_stream, _formatted, {BOLD, isWarning ? YELLOW : RED});
+ FormattedScope scope(_stream, _formatted, {BOLD, (error.type == "Warning") ? YELLOW : RED});
_stream << _linePrefix;
- if (_lineNumbers)
- {
- int line = offsetToLineNumber(
- boost::get_error_info<errinfo_sourceLocation>(*error)->start
- );
- if (line >= 0)
- _stream << "(" << line << "): ";
- }
- _stream << error->typeName() << ": ";
+ _stream << error.type << ": ";
}
- _stream << errorMessage(*error) << endl;
+ _stream << error.message << endl;
}
}
-int SyntaxTest::offsetToLineNumber(int _location) const
-{
- // parseAnalyseAndReturnError(...) prepends a version pragma
- _location -= strlen("pragma solidity >=0.0;\n");
- if (_location < 0 || static_cast<size_t>(_location) >= m_source.size())
- return -1;
- else
- return 1 + std::count(m_source.begin(), m_source.begin() + _location, '\n');
-}
-
-bool SyntaxTest::matchesExpectations(ErrorList const& _errorList) const
-{
- if (_errorList.size() != m_expectations.size())
- return false;
- else
- for (size_t i = 0; i < _errorList.size(); i++)
- if (
- (_errorList[i]->typeName() != m_expectations[i].type) ||
- (errorMessage(*_errorList[i]) != m_expectations[i].message)
- )
- return false;
- return true;
-}
-
-string SyntaxTest::errorMessage(Error const& _e)
+string SyntaxTest::errorMessage(Exception const& _e)
{
- if (_e.comment())
+ if (_e.comment() && !_e.comment()->empty())
return boost::replace_all_copy(*_e.comment(), "\n", "\\n");
else
return "NONE";
@@ -164,9 +124,9 @@ string SyntaxTest::parseSource(istream& _stream)
return source;
}
-vector<SyntaxTestExpectation> SyntaxTest::parseExpectations(istream& _stream)
+vector<SyntaxTestError> SyntaxTest::parseExpectations(istream& _stream)
{
- vector<SyntaxTestExpectation> expectations;
+ vector<SyntaxTestError> expectations;
string line;
while (getline(_stream, line))
{
@@ -188,7 +148,7 @@ vector<SyntaxTestExpectation> SyntaxTest::parseExpectations(istream& _stream)
skipWhitespace(it, line.end());
string errorMessage(it, line.end());
- expectations.emplace_back(SyntaxTestExpectation{move(errorType), move(errorMessage)});
+ expectations.emplace_back(SyntaxTestError{move(errorType), move(errorMessage)});
}
return expectations;
}
@@ -239,9 +199,11 @@ int SyntaxTest::registerTests(
_suite.add(make_test_case(
[fullpath]
{
- std::stringstream errorStream;
- if (!SyntaxTest(fullpath.string()).run(errorStream))
- BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str());
+ BOOST_REQUIRE_NO_THROW({
+ stringstream errorStream;
+ if (!SyntaxTest(fullpath.string()).run(errorStream))
+ BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str());
+ });
},
_path.stem().string(),
*filenames.back(),