Adds null check for file locations in XML output printer.
This commit is contained in:
@@ -424,6 +424,38 @@ void RE::Init(const char* regex) {
|
||||
|
||||
#endif // GTEST_USES_POSIX_RE
|
||||
|
||||
const char kUnknownFile[] = "unknown file";
|
||||
|
||||
// Formats a source file path and a line number as they would appear
|
||||
// in an error message from the compiler used to compile this code.
|
||||
GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
|
||||
const char* const file_name = file == NULL ? kUnknownFile : file;
|
||||
|
||||
if (line < 0) {
|
||||
return String::Format("%s:", file_name).c_str();
|
||||
}
|
||||
#ifdef _MSC_VER
|
||||
return String::Format("%s(%d):", file_name, line).c_str();
|
||||
#else
|
||||
return String::Format("%s:%d:", file_name, line).c_str();
|
||||
#endif // _MSC_VER
|
||||
}
|
||||
|
||||
// Formats a file location for compiler-independent XML output.
|
||||
// Although this function is not platform dependent, we put it next to
|
||||
// FormatFileLocation in order to contrast the two functions.
|
||||
// Note that FormatCompilerIndependentFileLocation() does NOT append colon
|
||||
// to the file location it produces, unlike FormatFileLocation().
|
||||
GTEST_API_ ::std::string FormatCompilerIndependentFileLocation(
|
||||
const char* file, int line) {
|
||||
const char* const file_name = file == NULL ? kUnknownFile : file;
|
||||
|
||||
if (line < 0)
|
||||
return file_name;
|
||||
else
|
||||
return String::Format("%s:%d", file_name, line).c_str();
|
||||
}
|
||||
|
||||
|
||||
GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line)
|
||||
: severity_(severity) {
|
||||
|
||||
@@ -3245,8 +3245,9 @@ void XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream,
|
||||
<< EscapeXmlAttribute(part.summary()).c_str()
|
||||
<< "\" type=\"\">";
|
||||
const String message = RemoveInvalidXmlCharacters(String::Format(
|
||||
"%s:%d\n%s",
|
||||
part.file_name(), part.line_number(),
|
||||
"%s\n%s",
|
||||
internal::FormatCompilerIndependentFileLocation(
|
||||
part.file_name(), part.line_number()).c_str(),
|
||||
part.message()).c_str());
|
||||
OutputXmlCDataSection(stream, message.c_str());
|
||||
*stream << "</failure>\n";
|
||||
|
||||
Reference in New Issue
Block a user