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::TestWithParam;
46 using ::testing::UnitTest;
47 using ::testing::Test;
48 using ::testing::Values;
49 
50 class SuccessfulTest : public Test {
51 };
52 
TEST_F(SuccessfulTest,Succeeds)53 TEST_F(SuccessfulTest, Succeeds) {
54   SUCCEED() << "This is a success.";
55   ASSERT_EQ(1, 1);
56 }
57 
58 class FailedTest : public Test {
59 };
60 
TEST_F(FailedTest,Fails)61 TEST_F(FailedTest, Fails) {
62   ASSERT_EQ(1, 2);
63 }
64 
65 class DisabledTest : public Test {
66 };
67 
TEST_F(DisabledTest,DISABLED_test_not_run)68 TEST_F(DisabledTest, DISABLED_test_not_run) {
69   FAIL() << "Unexpected failure: Disabled test should not be run";
70 }
71 
TEST(MixedResultTest,Succeeds)72 TEST(MixedResultTest, Succeeds) {
73   EXPECT_EQ(1, 1);
74   ASSERT_EQ(1, 1);
75 }
76 
TEST(MixedResultTest,Fails)77 TEST(MixedResultTest, Fails) {
78   EXPECT_EQ(1, 2);
79   ASSERT_EQ(2, 3);
80 }
81 
TEST(MixedResultTest,DISABLED_test)82 TEST(MixedResultTest, DISABLED_test) {
83   FAIL() << "Unexpected failure: Disabled test should not be run";
84 }
85 
TEST(XmlQuotingTest,OutputsCData)86 TEST(XmlQuotingTest, OutputsCData) {
87   FAIL() << "XML output: "
88             "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
89 }
90 
91 // Helps to test that invalid characters produced by test code do not make
92 // it into the XML file.
TEST(InvalidCharactersTest,InvalidCharactersInMessage)93 TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
94   FAIL() << "Invalid characters in brackets [\x1\x2]";
95 }
96 
97 class PropertyRecordingTest : public Test {
98 };
99 
TEST_F(PropertyRecordingTest,OneProperty)100 TEST_F(PropertyRecordingTest, OneProperty) {
101   RecordProperty("key_1", "1");
102 }
103 
TEST_F(PropertyRecordingTest,IntValuedProperty)104 TEST_F(PropertyRecordingTest, IntValuedProperty) {
105   RecordProperty("key_int", 1);
106 }
107 
TEST_F(PropertyRecordingTest,ThreeProperties)108 TEST_F(PropertyRecordingTest, ThreeProperties) {
109   RecordProperty("key_1", "1");
110   RecordProperty("key_2", "2");
111   RecordProperty("key_3", "3");
112 }
113 
TEST_F(PropertyRecordingTest,TwoValuesForOneKeyUsesLastValue)114 TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
115   RecordProperty("key_1", "1");
116   RecordProperty("key_1", "2");
117 }
118 
TEST(NoFixtureTest,RecordProperty)119 TEST(NoFixtureTest, RecordProperty) {
120   RecordProperty("key", "1");
121 }
122 
ExternalUtilityThatCallsRecordProperty(const char * key,int value)123 void ExternalUtilityThatCallsRecordProperty(const char* key, int value) {
124   testing::Test::RecordProperty(key, value);
125 }
126 
ExternalUtilityThatCallsRecordProperty(const char * key,const char * value)127 void ExternalUtilityThatCallsRecordProperty(const char* key,
128                                             const char* value) {
129   testing::Test::RecordProperty(key, value);
130 }
131 
TEST(NoFixtureTest,ExternalUtilityThatCallsRecordIntValuedProperty)132 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
133   ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
134 }
135 
TEST(NoFixtureTest,ExternalUtilityThatCallsRecordStringValuedProperty)136 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
137   ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
138 }
139 
140 // Verifies that the test parameter value is output in the 'value_param'
141 // XML attribute for value-parameterized tests.
142 class ValueParamTest : public TestWithParam<int> {};
TEST_P(ValueParamTest,HasValueParamAttribute)143 TEST_P(ValueParamTest, HasValueParamAttribute) {}
TEST_P(ValueParamTest,AnotherTestThatHasValueParamAttribute)144 TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
145 INSTANTIATE_TEST_CASE_P(Single, ValueParamTest, Values(33, 42));
146 
147 #if GTEST_HAS_TYPED_TEST
148 // Verifies that the type parameter name is output in the 'type_param'
149 // XML attribute for typed tests.
150 template <typename T> class TypedTest : public Test {};
151 typedef testing::Types<int, long> TypedTestTypes;
152 TYPED_TEST_CASE(TypedTest, TypedTestTypes);
TYPED_TEST(TypedTest,HasTypeParamAttribute)153 TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
154 #endif
155 
156 #if GTEST_HAS_TYPED_TEST_P
157 // Verifies that the type parameter name is output in the 'type_param'
158 // XML attribute for type-parameterized tests.
159 template <typename T> class TypeParameterizedTestCase : public Test {};
160 TYPED_TEST_CASE_P(TypeParameterizedTestCase);
TYPED_TEST_P(TypeParameterizedTestCase,HasTypeParamAttribute)161 TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
162 REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
163 typedef testing::Types<int, long> TypeParameterizedTestCaseTypes;
164 INSTANTIATE_TYPED_TEST_CASE_P(Single,
165                               TypeParameterizedTestCase,
166                               TypeParameterizedTestCaseTypes);
167 #endif
168 
main(int argc,char ** argv)169 int main(int argc, char** argv) {
170   InitGoogleTest(&argc, argv);
171 
172   if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
173     TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
174     delete listeners.Release(listeners.default_xml_generator());
175   }
176   return RUN_ALL_TESTS();
177 }
178