Skip to content

Commit b4aae50

Browse files
Abseil Teamcopybara-github
authored andcommitted
Export testsuite properties as XML elements, not attributes.
With this change, arbitrary property names in the testsuite no longer cause the produced XML output to be ill-formed. PiperOrigin-RevId: 721549090 Change-Id: Iedffa89bf914478f563c8f3b82cd50557762a665
1 parent 2b6b042 commit b4aae50

File tree

4 files changed

+80
-88
lines changed

4 files changed

+80
-88
lines changed

googletest/src/gtest.cc

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4012,16 +4012,11 @@ class XmlUnitTestResultPrinter : public EmptyTestEventListener {
40124012
static void PrintXmlUnitTest(::std::ostream* stream,
40134013
const UnitTest& unit_test);
40144014

4015-
// Produces a string representing the test properties in a result as space
4016-
// delimited XML attributes based on the property key="value" pairs.
4017-
// When the std::string is not empty, it includes a space at the beginning,
4018-
// to delimit this attribute from prior attributes.
4019-
static std::string TestPropertiesAsXmlAttributes(const TestResult& result);
4020-
40214015
// Streams an XML representation of the test properties of a TestResult
40224016
// object.
40234017
static void OutputXmlTestProperties(std::ostream* stream,
4024-
const TestResult& result);
4018+
const TestResult& result,
4019+
const std::string& indent);
40254020

40264021
// The output file.
40274022
const std::string output_file_;
@@ -4355,7 +4350,7 @@ void XmlUnitTestResultPrinter::OutputXmlTestResult(::std::ostream* stream,
43554350
if (failures == 0 && skips == 0) {
43564351
*stream << ">\n";
43574352
}
4358-
OutputXmlTestProperties(stream, result);
4353+
OutputXmlTestProperties(stream, result, /*indent=*/" ");
43594354
*stream << " </testcase>\n";
43604355
}
43614356
}
@@ -4384,9 +4379,10 @@ void XmlUnitTestResultPrinter::PrintXmlTestSuite(std::ostream* stream,
43844379
OutputXmlAttribute(
43854380
stream, kTestsuite, "timestamp",
43864381
FormatEpochTimeInMillisAsIso8601(test_suite.start_timestamp()));
4387-
*stream << TestPropertiesAsXmlAttributes(test_suite.ad_hoc_test_result());
43884382
}
43894383
*stream << ">\n";
4384+
OutputXmlTestProperties(stream, test_suite.ad_hoc_test_result(),
4385+
/*indent=*/" ");
43904386
for (int i = 0; i < test_suite.total_test_count(); ++i) {
43914387
if (test_suite.GetTestInfo(i)->is_reportable())
43924388
OutputXmlTestInfo(stream, test_suite.name(), *test_suite.GetTestInfo(i));
@@ -4424,11 +4420,12 @@ void XmlUnitTestResultPrinter::PrintXmlUnitTest(std::ostream* stream,
44244420
OutputXmlAttribute(stream, kTestsuites, "random_seed",
44254421
StreamableToString(unit_test.random_seed()));
44264422
}
4427-
*stream << TestPropertiesAsXmlAttributes(unit_test.ad_hoc_test_result());
44284423

44294424
OutputXmlAttribute(stream, kTestsuites, "name", "AllTests");
44304425
*stream << ">\n";
44314426

4427+
OutputXmlTestProperties(stream, unit_test.ad_hoc_test_result(),
4428+
/*indent=*/" ");
44324429
for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
44334430
if (unit_test.GetTestSuite(i)->reportable_test_count() > 0)
44344431
PrintXmlTestSuite(stream, *unit_test.GetTestSuite(i));
@@ -4465,37 +4462,24 @@ void XmlUnitTestResultPrinter::PrintXmlTestsList(
44654462
*stream << "</" << kTestsuites << ">\n";
44664463
}
44674464

4468-
// Produces a string representing the test properties in a result as space
4469-
// delimited XML attributes based on the property key="value" pairs.
4470-
std::string XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
4471-
const TestResult& result) {
4472-
Message attributes;
4473-
for (int i = 0; i < result.test_property_count(); ++i) {
4474-
const TestProperty& property = result.GetTestProperty(i);
4475-
attributes << " " << property.key() << "=" << "\""
4476-
<< EscapeXmlAttribute(property.value()) << "\"";
4477-
}
4478-
return attributes.GetString();
4479-
}
4480-
44814465
void XmlUnitTestResultPrinter::OutputXmlTestProperties(
4482-
std::ostream* stream, const TestResult& result) {
4466+
std::ostream* stream, const TestResult& result, const std::string& indent) {
44834467
const std::string kProperties = "properties";
44844468
const std::string kProperty = "property";
44854469

44864470
if (result.test_property_count() <= 0) {
44874471
return;
44884472
}
44894473

4490-
*stream << " <" << kProperties << ">\n";
4474+
*stream << indent << "<" << kProperties << ">\n";
44914475
for (int i = 0; i < result.test_property_count(); ++i) {
44924476
const TestProperty& property = result.GetTestProperty(i);
4493-
*stream << " <" << kProperty;
4477+
*stream << indent << " <" << kProperty;
44944478
*stream << " name=\"" << EscapeXmlAttribute(property.key()) << "\"";
44954479
*stream << " value=\"" << EscapeXmlAttribute(property.value()) << "\"";
44964480
*stream << "/>\n";
44974481
}
4498-
*stream << " </" << kProperties << ">\n";
4482+
*stream << indent << "</" << kProperties << ">\n";
44994483
}
45004484

45014485
// End XmlUnitTestResultPrinter

googletest/test/googletest-json-output-unittest.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,14 @@
323323
'time': '*',
324324
'timestamp': '*',
325325
'SetUpTestSuite': 'yes',
326+
'SetUpTestSuite (with whitespace)': 'yes and yes',
326327
'TearDownTestSuite': 'aye',
328+
'TearDownTestSuite (with whitespace)': 'aye and aye',
327329
'testsuite': [
328330
{
329331
'name': 'OneProperty',
330332
'file': 'gtest_xml_output_unittest_.cc',
331-
'line': 121,
333+
'line': 125,
332334
'status': 'RUN',
333335
'result': 'COMPLETED',
334336
'time': '*',
@@ -339,7 +341,7 @@
339341
{
340342
'name': 'IntValuedProperty',
341343
'file': 'gtest_xml_output_unittest_.cc',
342-
'line': 125,
344+
'line': 129,
343345
'status': 'RUN',
344346
'result': 'COMPLETED',
345347
'time': '*',
@@ -350,7 +352,7 @@
350352
{
351353
'name': 'ThreeProperties',
352354
'file': 'gtest_xml_output_unittest_.cc',
353-
'line': 129,
355+
'line': 133,
354356
'status': 'RUN',
355357
'result': 'COMPLETED',
356358
'time': '*',
@@ -363,7 +365,7 @@
363365
{
364366
'name': 'TwoValuesForOneKeyUsesLastValue',
365367
'file': 'gtest_xml_output_unittest_.cc',
366-
'line': 135,
368+
'line': 139,
367369
'status': 'RUN',
368370
'result': 'COMPLETED',
369371
'time': '*',
@@ -385,7 +387,7 @@
385387
{
386388
'name': 'RecordProperty',
387389
'file': 'gtest_xml_output_unittest_.cc',
388-
'line': 140,
390+
'line': 144,
389391
'status': 'RUN',
390392
'result': 'COMPLETED',
391393
'time': '*',
@@ -396,7 +398,7 @@
396398
{
397399
'name': 'ExternalUtilityThatCallsRecordIntValuedProperty',
398400
'file': 'gtest_xml_output_unittest_.cc',
399-
'line': 153,
401+
'line': 157,
400402
'status': 'RUN',
401403
'result': 'COMPLETED',
402404
'time': '*',
@@ -409,7 +411,7 @@
409411
'ExternalUtilityThatCallsRecordStringValuedProperty'
410412
),
411413
'file': 'gtest_xml_output_unittest_.cc',
412-
'line': 157,
414+
'line': 161,
413415
'status': 'RUN',
414416
'result': 'COMPLETED',
415417
'time': '*',
@@ -431,7 +433,7 @@
431433
{
432434
'name': 'NoopPassingTest',
433435
'file': 'gtest_xml_output_unittest_.cc',
434-
'line': 168,
436+
'line': 172,
435437
'status': 'RUN',
436438
'result': 'SKIPPED',
437439
'timestamp': '*',
@@ -471,7 +473,7 @@
471473
{
472474
'name': 'NoopPassingTest',
473475
'file': 'gtest_xml_output_unittest_.cc',
474-
'line': 175,
476+
'line': 179,
475477
'status': 'RUN',
476478
'result': 'COMPLETED',
477479
'timestamp': '*',
@@ -508,7 +510,7 @@
508510
'name': 'HasTypeParamAttribute',
509511
'type_param': 'int',
510512
'file': 'gtest_xml_output_unittest_.cc',
511-
'line': 189,
513+
'line': 193,
512514
'status': 'RUN',
513515
'result': 'COMPLETED',
514516
'time': '*',
@@ -528,7 +530,7 @@
528530
'name': 'HasTypeParamAttribute',
529531
'type_param': 'long',
530532
'file': 'gtest_xml_output_unittest_.cc',
531-
'line': 189,
533+
'line': 193,
532534
'status': 'RUN',
533535
'result': 'COMPLETED',
534536
'time': '*',
@@ -548,7 +550,7 @@
548550
'name': 'HasTypeParamAttribute',
549551
'type_param': 'int',
550552
'file': 'gtest_xml_output_unittest_.cc',
551-
'line': 196,
553+
'line': 200,
552554
'status': 'RUN',
553555
'result': 'COMPLETED',
554556
'time': '*',
@@ -568,7 +570,7 @@
568570
'name': 'HasTypeParamAttribute',
569571
'type_param': 'long',
570572
'file': 'gtest_xml_output_unittest_.cc',
571-
'line': 196,
573+
'line': 200,
572574
'status': 'RUN',
573575
'result': 'COMPLETED',
574576
'time': '*',
@@ -589,7 +591,7 @@
589591
'name': 'HasValueParamAttribute/0',
590592
'value_param': '33',
591593
'file': 'gtest_xml_output_unittest_.cc',
592-
'line': 180,
594+
'line': 184,
593595
'status': 'RUN',
594596
'result': 'COMPLETED',
595597
'time': '*',
@@ -600,7 +602,7 @@
600602
'name': 'HasValueParamAttribute/1',
601603
'value_param': '42',
602604
'file': 'gtest_xml_output_unittest_.cc',
603-
'line': 180,
605+
'line': 184,
604606
'status': 'RUN',
605607
'result': 'COMPLETED',
606608
'time': '*',
@@ -611,7 +613,7 @@
611613
'name': 'AnotherTestThatHasValueParamAttribute/0',
612614
'value_param': '33',
613615
'file': 'gtest_xml_output_unittest_.cc',
614-
'line': 181,
616+
'line': 185,
615617
'status': 'RUN',
616618
'result': 'COMPLETED',
617619
'time': '*',
@@ -622,7 +624,7 @@
622624
'name': 'AnotherTestThatHasValueParamAttribute/1',
623625
'value_param': '42',
624626
'file': 'gtest_xml_output_unittest_.cc',
625-
'line': 181,
627+
'line': 185,
626628
'status': 'RUN',
627629
'result': 'COMPLETED',
628630
'time': '*',

0 commit comments

Comments
 (0)