[test] Add location string to error messages.

This commit is contained in:
Alexander Arlt 2022-04-01 22:38:18 -05:00
parent c4909e99c1
commit d4c02f2270
4 changed files with 46 additions and 5 deletions

View File

@ -154,7 +154,7 @@ void CommonSyntaxTest::printErrorList(
vector<SyntaxTestError> const& _errorList, vector<SyntaxTestError> const& _errorList,
string const& _linePrefix, string const& _linePrefix,
bool _formatted bool _formatted
) ) const
{ {
if (_errorList.empty()) if (_errorList.empty())
util::AnsiColorized(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl; util::AnsiColorized(_stream, _formatted, {BOLD, GREEN}) << _linePrefix << "Success" << endl;
@ -178,6 +178,9 @@ void CommonSyntaxTest::printErrorList(
_stream << "-"; _stream << "-";
if (error.locationEnd >= 0) if (error.locationEnd >= 0)
_stream << error.locationEnd; _stream << error.locationEnd;
string locString = locationString(error.sourceName, error.locationStart, error.locationEnd);
if (!locString.empty())
_stream << "='" << locString << "'";
_stream << "): "; _stream << "): ";
} }
_stream << error.message << endl; _stream << error.message << endl;
@ -192,6 +195,26 @@ string CommonSyntaxTest::errorMessage(util::Exception const& _e)
return "NONE"; 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<unsigned long>(_locationStart),
static_cast<unsigned long>(_locationEnd)
- static_cast<unsigned long>(_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<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream) vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
{ {
vector<SyntaxTestError> expectations; vector<SyntaxTestError> expectations;
@ -219,6 +242,7 @@ vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
expect(it, line.end(), ':'); expect(it, line.end(), ':');
skipWhitespace(it, line.end()); skipWhitespace(it, line.end());
std::string locString;
int locationStart = -1; int locationStart = -1;
int locationEnd = -1; int locationEnd = -1;
std::string sourceName; std::string sourceName;
@ -237,10 +261,19 @@ vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
locationStart = parseUnsignedInteger(it, line.end()); locationStart = parseUnsignedInteger(it, line.end());
expect(it, line.end(), '-'); expect(it, line.end(), '-');
locationEnd = parseUnsignedInteger(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<unsigned>(locString.length());
expect(it, line.end(), '\'');
}
expect(it, line.end(), ')'); expect(it, line.end(), ')');
expect(it, line.end(), ':'); expect(it, line.end(), ':');
} }
skipWhitespace(it, line.end()); skipWhitespace(it, line.end());
string errorMessage(it, line.end()); string errorMessage(it, line.end());
@ -249,6 +282,7 @@ vector<SyntaxTestError> CommonSyntaxTest::parseExpectations(istream& _stream)
move(errorId), move(errorId),
move(errorMessage), move(errorMessage),
move(sourceName), move(sourceName),
locString,
locationStart, locationStart,
locationEnd locationEnd
}); });

View File

@ -38,6 +38,7 @@ struct SyntaxTestError
std::optional<langutil::ErrorId> errorId; std::optional<langutil::ErrorId> errorId;
std::string message; std::string message;
std::string sourceName; std::string sourceName;
std::string locationString;
int locationStart = -1; int locationStart = -1;
int locationEnd = -1; int locationEnd = -1;
bool operator==(SyntaxTestError const& _rhs) const bool operator==(SyntaxTestError const& _rhs) const
@ -46,6 +47,7 @@ struct SyntaxTestError
errorId == _rhs.errorId && errorId == _rhs.errorId &&
message == _rhs.message && message == _rhs.message &&
sourceName == _rhs.sourceName && sourceName == _rhs.sourceName &&
locationString == _rhs.locationString &&
locationStart == _rhs.locationStart && locationStart == _rhs.locationStart &&
locationEnd == _rhs.locationEnd; locationEnd == _rhs.locationEnd;
} }
@ -70,17 +72,19 @@ public:
protected: protected:
virtual void parseAndAnalyze() = 0; virtual void parseAndAnalyze() = 0;
static void printErrorList( void printErrorList(
std::ostream& _stream, std::ostream& _stream,
std::vector<SyntaxTestError> const& _errors, std::vector<SyntaxTestError> const& _errors,
std::string const& _linePrefix, std::string const& _linePrefix,
bool _formatted = false bool _formatted = false
); ) const;
TestResult conclude(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false); TestResult conclude(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false);
void printExpectationAndError(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<SyntaxTestError> parseExpectations(std::istream& _stream); std::vector<SyntaxTestError> parseExpectations(std::istream& _stream);
std::string locationString(std::string const& _sourceName, int _locationStart, int _locationEnd) const;
frontend::test::SourceMap m_sources; frontend::test::SourceMap m_sources;
std::vector<SyntaxTestError> m_expectations; std::vector<SyntaxTestError> m_expectations;

View File

@ -108,6 +108,7 @@ void SyntaxTest::parseAndAnalyze()
nullopt, nullopt,
errorMessage(_e), errorMessage(_e),
"", "",
"",
-1, -1,
-1 -1
}); });
@ -149,6 +150,7 @@ void SyntaxTest::filterObtainedErrors()
currentError->errorId(), currentError->errorId(),
errorMessage(*currentError), errorMessage(*currentError),
sourceName, sourceName,
locationString(sourceName, locationStart, locationEnd),
locationStart, locationStart,
locationEnd locationEnd
}); });

View File

@ -65,6 +65,7 @@ void SyntaxTest::parseAndAnalyze()
error->errorId(), error->errorId(),
errorMessage(*error), errorMessage(*error),
name, name,
locationString(name, locationStart, locationEnd),
locationStart, locationStart,
locationEnd locationEnd
}); });