diff --git a/test/CommonSyntaxTest.cpp b/test/CommonSyntaxTest.cpp index 5b66aea30..b05c25959 100644 --- a/test/CommonSyntaxTest.cpp +++ b/test/CommonSyntaxTest.cpp @@ -154,7 +154,7 @@ void CommonSyntaxTest::printErrorList( vector const& _errorList, string const& _linePrefix, bool _formatted -) +) const { if (_errorList.empty()) util::AnsiColorized(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl; @@ -178,6 +178,9 @@ void CommonSyntaxTest::printErrorList( _stream << "-"; if (error.locationEnd >= 0) _stream << error.locationEnd; + string locString = locationString(error.sourceName, error.locationStart, error.locationEnd); + if (!locString.empty()) + _stream << "='" << locString << "'"; _stream << "): "; } _stream << error.message << endl; @@ -192,6 +195,26 @@ string CommonSyntaxTest::errorMessage(util::Exception const& _e) return "NONE"; } +string CommonSyntaxTest::locationString(string const& _sourceName, int _locationStart, int _locationEnd) const +{ + auto source = m_sources.sources.find(_sourceName); + if (source != m_sources.sources.end()) + { + std::string locationString; + std::string content{source->second}; + if ((_locationStart >= 0) && (_locationEnd >= 0)) + locationString = content.substr( + static_cast(_locationStart), + static_cast(_locationEnd) + - static_cast(_locationStart)); + boost::replace_all(locationString, "\r\n", "\n"); + boost::replace_all(locationString, "\n", " "); + if (!locationString.empty() && locationString.length() <= 32) + return boost::trim_copy(locationString); + } + return {}; +} + vector CommonSyntaxTest::parseExpectations(istream& _stream) { vector expectations; @@ -219,6 +242,7 @@ vector CommonSyntaxTest::parseExpectations(istream& _stream) expect(it, line.end(), ':'); skipWhitespace(it, line.end()); + std::string locString; int locationStart = -1; int locationEnd = -1; std::string sourceName; @@ -237,10 +261,19 @@ vector CommonSyntaxTest::parseExpectations(istream& _stream) locationStart = parseUnsignedInteger(it, line.end()); expect(it, line.end(), '-'); locationEnd = parseUnsignedInteger(it, line.end()); + locString = locationString(sourceName, locationStart, locationEnd); + if (*it != '=') + locString = ""; + else if (!locString.empty()) + { + expect(it, line.end(), '='); + expect(it, line.end(), '\''); + it += static_cast(locString.length()); + expect(it, line.end(), '\''); + } expect(it, line.end(), ')'); expect(it, line.end(), ':'); } - skipWhitespace(it, line.end()); string errorMessage(it, line.end()); @@ -249,6 +282,7 @@ vector CommonSyntaxTest::parseExpectations(istream& _stream) move(errorId), move(errorMessage), move(sourceName), + locString, locationStart, locationEnd }); diff --git a/test/CommonSyntaxTest.h b/test/CommonSyntaxTest.h index 169824252..f544d8f9b 100644 --- a/test/CommonSyntaxTest.h +++ b/test/CommonSyntaxTest.h @@ -38,6 +38,7 @@ struct SyntaxTestError std::optional errorId; std::string message; std::string sourceName; + std::string locationString; int locationStart = -1; int locationEnd = -1; bool operator==(SyntaxTestError const& _rhs) const @@ -46,6 +47,7 @@ struct SyntaxTestError errorId == _rhs.errorId && message == _rhs.message && sourceName == _rhs.sourceName && + locationString == _rhs.locationString && locationStart == _rhs.locationStart && locationEnd == _rhs.locationEnd; } @@ -70,17 +72,19 @@ public: protected: virtual void parseAndAnalyze() = 0; - static void printErrorList( + void printErrorList( std::ostream& _stream, std::vector const& _errors, std::string const& _linePrefix, bool _formatted = false - ); + ) const; TestResult conclude(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false); void printExpectationAndError(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false); - static std::vector parseExpectations(std::istream& _stream); + std::vector parseExpectations(std::istream& _stream); + + std::string locationString(std::string const& _sourceName, int _locationStart, int _locationEnd) const; frontend::test::SourceMap m_sources; std::vector m_expectations; diff --git a/test/libsolidity/SyntaxTest.cpp b/test/libsolidity/SyntaxTest.cpp index d3e512450..88168a987 100644 --- a/test/libsolidity/SyntaxTest.cpp +++ b/test/libsolidity/SyntaxTest.cpp @@ -108,6 +108,7 @@ void SyntaxTest::parseAndAnalyze() nullopt, errorMessage(_e), "", + "", -1, -1 }); @@ -149,6 +150,7 @@ void SyntaxTest::filterObtainedErrors() currentError->errorId(), errorMessage(*currentError), sourceName, + locationString(sourceName, locationStart, locationEnd), locationStart, locationEnd }); diff --git a/test/libyul/SyntaxTest.cpp b/test/libyul/SyntaxTest.cpp index 8ab5bb953..8f83ced18 100644 --- a/test/libyul/SyntaxTest.cpp +++ b/test/libyul/SyntaxTest.cpp @@ -65,6 +65,7 @@ void SyntaxTest::parseAndAnalyze() error->errorId(), errorMessage(*error), name, + locationString(name, locationStart, locationEnd), locationStart, locationEnd });