1 // Copyright 2006, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 // Author: eefacm@gmail.com (Sean Mcafee)
31
32 // Unit test for Google Test XML output.
33 //
34 // A user can specify XML output in a Google Test program to run via
35 // either the GTEST_OUTPUT environment variable or the --gtest_output
36 // flag. This is used for testing such functionality.
37 //
38 // This program will be invoked from a Python unit test. Don't run it
39 // directly.
40
41 #include <gtest/gtest.h>
42
43 using ::testing::InitGoogleTest;
44 using ::testing::TestEventListeners;
45 using ::testing::UnitTest;
46
47 class SuccessfulTest : public testing::Test {
48 };
49
TEST_F(SuccessfulTest,Succeeds)50 TEST_F(SuccessfulTest, Succeeds) {
51 SUCCEED() << "This is a success.";
52 ASSERT_EQ(1, 1);
53 }
54
55 class FailedTest : public testing::Test {
56 };
57
TEST_F(FailedTest,Fails)58 TEST_F(FailedTest, Fails) {
59 ASSERT_EQ(1, 2);
60 }
61
62 class DisabledTest : public testing::Test {
63 };
64
TEST_F(DisabledTest,DISABLED_test_not_run)65 TEST_F(DisabledTest, DISABLED_test_not_run) {
66 FAIL() << "Unexpected failure: Disabled test should not be run";
67 }
68
TEST(MixedResultTest,Succeeds)69 TEST(MixedResultTest, Succeeds) {
70 EXPECT_EQ(1, 1);
71 ASSERT_EQ(1, 1);
72 }
73
TEST(MixedResultTest,Fails)74 TEST(MixedResultTest, Fails) {
75 EXPECT_EQ(1, 2);
76 ASSERT_EQ(2, 3);
77 }
78
TEST(MixedResultTest,DISABLED_test)79 TEST(MixedResultTest, DISABLED_test) {
80 FAIL() << "Unexpected failure: Disabled test should not be run";
81 }
82
TEST(XmlQuotingTest,OutputsCData)83 TEST(XmlQuotingTest, OutputsCData) {
84 FAIL() << "XML output: "
85 "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
86 }
87
88 // Helps to test that invalid characters produced by test code do not make
89 // it into the XML file.
TEST(InvalidCharactersTest,InvalidCharactersInMessage)90 TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
91 FAIL() << "Invalid characters in brackets [\x1\x2]";
92 }
93
94 class PropertyRecordingTest : public testing::Test {
95 };
96
TEST_F(PropertyRecordingTest,OneProperty)97 TEST_F(PropertyRecordingTest, OneProperty) {
98 RecordProperty("key_1", "1");
99 }
100
TEST_F(PropertyRecordingTest,IntValuedProperty)101 TEST_F(PropertyRecordingTest, IntValuedProperty) {
102 RecordProperty("key_int", 1);
103 }
104
TEST_F(PropertyRecordingTest,ThreeProperties)105 TEST_F(PropertyRecordingTest, ThreeProperties) {
106 RecordProperty("key_1", "1");
107 RecordProperty("key_2", "2");
108 RecordProperty("key_3", "3");
109 }
110
TEST_F(PropertyRecordingTest,TwoValuesForOneKeyUsesLastValue)111 TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
112 RecordProperty("key_1", "1");
113 RecordProperty("key_1", "2");
114 }
115
TEST(NoFixtureTest,RecordProperty)116 TEST(NoFixtureTest, RecordProperty) {
117 RecordProperty("key", "1");
118 }
119
ExternalUtilityThatCallsRecordProperty(const char * key,int value)120 void ExternalUtilityThatCallsRecordProperty(const char* key, int value) {
121 testing::Test::RecordProperty(key, value);
122 }
123
ExternalUtilityThatCallsRecordProperty(const char * key,const char * value)124 void ExternalUtilityThatCallsRecordProperty(const char* key,
125 const char* value) {
126 testing::Test::RecordProperty(key, value);
127 }
128
TEST(NoFixtureTest,ExternalUtilityThatCallsRecordIntValuedProperty)129 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
130 ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
131 }
132
TEST(NoFixtureTest,ExternalUtilityThatCallsRecordStringValuedProperty)133 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
134 ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
135 }
136
main(int argc,char ** argv)137 int main(int argc, char** argv) {
138 InitGoogleTest(&argc, argv);
139
140 if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
141 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
142 delete listeners.Release(listeners.default_xml_generator());
143 }
144 return RUN_ALL_TESTS();
145 }
146