OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/test/gtest_xml_unittest_result_printer.h" | 5 #include "base/test/gtest_xml_unittest_result_printer.h" |
6 | 6 |
7 #include "base/base64.h" | |
7 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_util.h" | |
9 #include "base/time/time.h" | 11 #include "base/time/time.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 | 14 |
15 namespace { | |
16 | |
17 bool NeedsEscaping(const std::string& text) { | |
18 return !IsStringUTF8(text) || text.find('<') != std::string::npos || | |
19 text.find('>') != std::string::npos; | |
20 } | |
21 | |
22 } // namespace | |
23 | |
13 XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL) { | 24 XmlUnitTestResultPrinter::XmlUnitTestResultPrinter() : output_file_(NULL) { |
14 } | 25 } |
15 | 26 |
16 XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() { | 27 XmlUnitTestResultPrinter::~XmlUnitTestResultPrinter() { |
17 if (output_file_) { | 28 if (output_file_) { |
18 fprintf(output_file_, "</testsuites>\n"); | 29 fprintf(output_file_, "</testsuites>\n"); |
19 fflush(output_file_); | 30 fflush(output_file_); |
20 CloseFile(output_file_); | 31 CloseFile(output_file_); |
21 } | 32 } |
22 } | 33 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" | 68 " <testcase name=\"%s\" status=\"run\" time=\"%.3f\"" |
58 " classname=\"%s\">\n", | 69 " classname=\"%s\">\n", |
59 test_info.name(), | 70 test_info.name(), |
60 static_cast<double>(test_info.result()->elapsed_time()) / | 71 static_cast<double>(test_info.result()->elapsed_time()) / |
61 Time::kMillisecondsPerSecond, | 72 Time::kMillisecondsPerSecond, |
62 test_info.test_case_name()); | 73 test_info.test_case_name()); |
63 if (test_info.result()->Failed()) { | 74 if (test_info.result()->Failed()) { |
64 fprintf(output_file_, | 75 fprintf(output_file_, |
65 " <failure message=\"\" type=\"\"></failure>\n"); | 76 " <failure message=\"\" type=\"\"></failure>\n"); |
66 } | 77 } |
78 for (int i = 0; i < test_info.result()->total_part_count(); ++i) { | |
79 WriteTestPartResult(test_info.result()->GetTestPartResult(i)); | |
80 } | |
67 fprintf(output_file_, " </testcase>\n"); | 81 fprintf(output_file_, " </testcase>\n"); |
68 fflush(output_file_); | 82 fflush(output_file_); |
69 } | 83 } |
70 | 84 |
71 void XmlUnitTestResultPrinter::OnTestCaseEnd( | 85 void XmlUnitTestResultPrinter::OnTestCaseEnd( |
72 const testing::TestCase& test_case) { | 86 const testing::TestCase& test_case) { |
73 fprintf(output_file_, " </testsuite>\n"); | 87 fprintf(output_file_, " </testsuite>\n"); |
74 fflush(output_file_); | 88 fflush(output_file_); |
75 } | 89 } |
76 | 90 |
91 void XmlUnitTestResultPrinter::WriteTestPartResult( | |
92 const testing::TestPartResult& test_part_result) { | |
93 const char* type = "unknown"; | |
94 switch (test_part_result.type()) { | |
95 case testing::TestPartResult::kSuccess: | |
96 type = "success"; | |
97 break; | |
98 case testing::TestPartResult::kNonFatalFailure: | |
99 type = "failure"; | |
100 break; | |
101 case testing::TestPartResult::kFatalFailure: | |
102 type = "fatal_failure"; | |
103 break; | |
104 } | |
105 std::string summary = test_part_result.summary(); | |
106 std::string summary_tag("summary"); | |
107 if (!NeedsEscaping(summary)) { | |
Paweł Hajdan Jr.
2016/12/30 19:02:39
Shouldn't the condition be reversed?
By the way,
alex-ac
2016/12/30 22:10:20
I've deleted condition and now text is encoded in
| |
108 Base64Encode(summary, &summary); | |
109 summary_tag = "summary-encoded"; | |
110 } | |
111 std::string message = test_part_result.message(); | |
112 std::string message_tag("message"); | |
113 if (!NeedsEscaping(message)) { | |
114 Base64Encode(message, &message); | |
115 message_tag = "message-encoded"; | |
116 } | |
117 fprintf(output_file_, | |
118 " <x-test-result-part type=\"%s\" file=\"%s\" line=\"%d\">\n" | |
119 " <%s>%s</%s>\n" | |
120 " <%s>%s</%s>\n" | |
121 " </x-test-result-part>\n", | |
122 type, test_part_result.file_name(), test_part_result.line_number(), | |
123 summary_tag.c_str(), summary.c_str(), summary_tag.c_str(), | |
124 message_tag.c_str(), message.c_str(), message_tag.c_str()); | |
125 fflush(output_file_); | |
126 } | |
127 | |
77 } // namespace base | 128 } // namespace base |
OLD | NEW |