From 30552030bea75bf2d27c49437e757b8a5d3b2e98 Mon Sep 17 00:00:00 2001 From: vladlosev Date: Wed, 2 Feb 2011 10:07:04 +0000 Subject: Adds null check for file locations in XML output printer. git-svn-id: http://googletest.googlecode.com/svn/trunk@540 861a406c-534a-0410-8894-cb66d6ee9925 --- src/gtest-port.cc | 32 ++++++++++++++++++++++++++++++++ src/gtest.cc | 5 +++-- 2 files changed, 35 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/gtest-port.cc b/src/gtest-port.cc index da62fba..4310a73 100644 --- a/src/gtest-port.cc +++ b/src/gtest-port.cc @@ -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) { diff --git a/src/gtest.cc b/src/gtest.cc index 575a8a5..1c58c6f 100644 --- a/src/gtest.cc +++ b/src/gtest.cc @@ -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 << "\n"; -- cgit v1.2.1