From 9f546cfafcc4a12c6574550724a82074bb0afb66 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 12 Jun 2018 11:06:14 +0200 Subject: Improve exception handling in soltest and isoltest. --- test/libsolidity/SyntaxTest.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'test/libsolidity/SyntaxTest.cpp') diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp index 1c2355d5..430073a0 100644 --- a/test/libsolidity/SyntaxTest.cpp +++ b/test/libsolidity/SyntaxTest.cpp @@ -268,9 +268,16 @@ int SyntaxTest::registerTests( [fullpath] { BOOST_REQUIRE_NO_THROW({ - stringstream errorStream; - if (!SyntaxTest(fullpath.string()).run(errorStream)) - BOOST_ERROR("Test expectation mismatch.\n" + errorStream.str()); + 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)); + } }); }, _path.stem().string(), -- cgit v1.2.3 From 14d0f8c2f194bfed0ae8d23042f6210cff1f93c9 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 8 Jun 2018 14:17:50 +0200 Subject: Refactor syntax test infrastructure to prepare introducing semantics tests. --- test/libsolidity/SyntaxTest.cpp | 154 ++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 101 deletions(-) (limited to 'test/libsolidity/SyntaxTest.cpp') diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp index 430073a0..1de42300 100644 --- a/test/libsolidity/SyntaxTest.cpp +++ b/test/libsolidity/SyntaxTest.cpp @@ -33,28 +33,10 @@ using namespace std; namespace fs = boost::filesystem; using namespace boost::unit_test; -template -void skipWhitespace(IteratorType& _it, IteratorType _end) +namespace { - while (_it != _end && isspace(*_it)) - ++_it; -} - -template -void skipSlashes(IteratorType& _it, IteratorType _end) -{ - 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) +int parseUnsignedInteger(string::iterator& _it, string::iterator _end) { if (_it == _end || !isdigit(*_it)) throw runtime_error("Invalid test expectation. Source location expected."); @@ -68,6 +50,8 @@ int parseUnsignedInteger(string::iterator &_it, string::iterator _end) return result; } +} + SyntaxTest::SyntaxTest(string const& _filename) { ifstream file(_filename); @@ -120,6 +104,55 @@ bool SyntaxTest::run(ostream& _stream, string const& _linePrefix, bool const _fo return true; } +void SyntaxTest::printSource(ostream& _stream, string const& _linePrefix, bool const _formatted) const +{ + if (_formatted) + { + if (m_source.empty()) + return; + + vector sourceFormatting(m_source.length(), formatting::RESET); + for (auto const& error: m_errorList) + if (error.locationStart >= 0 && error.locationEnd >= 0) + { + assert(static_cast(error.locationStart) <= m_source.length()); + assert(static_cast(error.locationEnd) <= m_source.length()); + bool isWarning = error.type == "Warning"; + for (int i = error.locationStart; i < error.locationEnd; i++) + if (isWarning) + { + if (sourceFormatting[i] == formatting::RESET) + sourceFormatting[i] = formatting::ORANGE_BACKGROUND; + } + else + sourceFormatting[i] = formatting::RED_BACKGROUND; + } + + _stream << _linePrefix << sourceFormatting.front() << m_source.front(); + for (size_t i = 1; i < m_source.length(); i++) + { + if (sourceFormatting[i] != sourceFormatting[i - 1]) + _stream << sourceFormatting[i]; + if (m_source[i] != '\n') + _stream << m_source[i]; + else + { + _stream << formatting::RESET << endl; + if (i + 1 < m_source.length()) + _stream << _linePrefix << sourceFormatting[i]; + } + } + _stream << formatting::RESET; + } + else + { + stringstream stream(m_source); + string line; + while (getline(stream, line)) + _stream << _linePrefix << line << endl; + } +} + void SyntaxTest::printErrorList( ostream& _stream, vector const& _errorList, @@ -159,19 +192,6 @@ string SyntaxTest::errorMessage(Exception const& _e) 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; -} - vector SyntaxTest::parseExpectations(istream& _stream) { vector expectations; @@ -220,71 +240,3 @@ vector SyntaxTest::parseExpectations(istream& _stream) } return expectations; } - -#if BOOST_VERSION < 105900 -test_case *make_test_case( - function const& _fn, - string const& _name, - string const& /* _filename */, - size_t /* _line */ -) -{ - return make_test_case(_fn, _name); -} -#endif - -bool SyntaxTest::isTestFilename(boost::filesystem::path const& _filename) -{ - return _filename.extension().string() == ".sol" && - !boost::starts_with(_filename.string(), "~") && - !boost::starts_with(_filename.string(), "."); -} - -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(fullpath), - fs::directory_iterator() - )) - if (fs::is_directory(entry.path()) || isTestFilename(entry.path().filename())) - numTestsAdded += registerTests(*sub_suite, _basepath, _path / entry.path().filename()); - _suite.add(sub_suite); - } - else - { - static vector> filenames; - - filenames.emplace_back(new string(_path.string())); - _suite.add(make_test_case( - [fullpath] - { - BOOST_REQUIRE_NO_THROW({ - 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)); - } - }); - }, - _path.stem().string(), - *filenames.back(), - 0 - )); - numTestsAdded = 1; - } - return numTestsAdded; -} -- cgit v1.2.3 From 6c3d12d85bc7977817e8dc1c5bd0af4b29406b58 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 9 Oct 2018 19:06:22 +0100 Subject: Do not require ctype/stdio if not needed --- test/libsolidity/SyntaxTest.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'test/libsolidity/SyntaxTest.cpp') diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp index 1de42300..91d1681f 100644 --- a/test/libsolidity/SyntaxTest.cpp +++ b/test/libsolidity/SyntaxTest.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include -- cgit v1.2.3