You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.7 KiB

  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. // Author: eefacm@gmail.com (Sean Mcafee)
  30. // Unit test for Google Test XML output.
  31. //
  32. // A user can specify XML output in a Google Test program to run via
  33. // either the GTEST_OUTPUT environment variable or the --gtest_output
  34. // flag. This is used for testing such functionality.
  35. //
  36. // This program will be invoked from a Python unit test. Don't run it
  37. // directly.
  38. #include "gtest/gtest.h"
  39. using ::testing::InitGoogleTest;
  40. using ::testing::TestEventListeners;
  41. using ::testing::TestWithParam;
  42. using ::testing::UnitTest;
  43. using ::testing::Test;
  44. using ::testing::Types;
  45. using ::testing::Values;
  46. class SuccessfulTest : public Test {
  47. };
  48. TEST_F(SuccessfulTest, Succeeds) {
  49. SUCCEED() << "This is a success.";
  50. ASSERT_EQ(1, 1);
  51. }
  52. class FailedTest : public Test {
  53. };
  54. TEST_F(FailedTest, Fails) {
  55. ASSERT_EQ(1, 2);
  56. }
  57. class DisabledTest : public Test {
  58. };
  59. TEST_F(DisabledTest, DISABLED_test_not_run) {
  60. FAIL() << "Unexpected failure: Disabled test should not be run";
  61. }
  62. TEST(MixedResultTest, Succeeds) {
  63. EXPECT_EQ(1, 1);
  64. ASSERT_EQ(1, 1);
  65. }
  66. TEST(MixedResultTest, Fails) {
  67. EXPECT_EQ(1, 2);
  68. ASSERT_EQ(2, 3);
  69. }
  70. TEST(MixedResultTest, DISABLED_test) {
  71. FAIL() << "Unexpected failure: Disabled test should not be run";
  72. }
  73. TEST(XmlQuotingTest, OutputsCData) {
  74. FAIL() << "XML output: "
  75. "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
  76. }
  77. // Helps to test that invalid characters produced by test code do not make
  78. // it into the XML file.
  79. TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
  80. FAIL() << "Invalid characters in brackets [\x1\x2]";
  81. }
  82. class PropertyRecordingTest : public Test {
  83. };
  84. TEST_F(PropertyRecordingTest, OneProperty) {
  85. RecordProperty("key_1", "1");
  86. }
  87. TEST_F(PropertyRecordingTest, IntValuedProperty) {
  88. RecordProperty("key_int", 1);
  89. }
  90. TEST_F(PropertyRecordingTest, ThreeProperties) {
  91. RecordProperty("key_1", "1");
  92. RecordProperty("key_2", "2");
  93. RecordProperty("key_3", "3");
  94. }
  95. TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
  96. RecordProperty("key_1", "1");
  97. RecordProperty("key_1", "2");
  98. }
  99. TEST(NoFixtureTest, RecordProperty) {
  100. RecordProperty("key", "1");
  101. }
  102. void ExternalUtilityThatCallsRecordProperty(const char* key, int value) {
  103. testing::Test::RecordProperty(key, value);
  104. }
  105. void ExternalUtilityThatCallsRecordProperty(const char* key,
  106. const char* value) {
  107. testing::Test::RecordProperty(key, value);
  108. }
  109. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
  110. ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
  111. }
  112. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
  113. ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
  114. }
  115. // Verifies that the test parameter value is output in the 'value_param'
  116. // XML attribute for value-parameterized tests.
  117. class ValueParamTest : public TestWithParam<int> {};
  118. TEST_P(ValueParamTest, HasValueParamAttribute) {}
  119. TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
  120. INSTANTIATE_TEST_CASE_P(Single, ValueParamTest, Values(33, 42));
  121. // Verifies that the type parameter name is output in the 'type_param'
  122. // XML attribute for typed tests.
  123. template <typename T> class TypedTest : public Test {};
  124. typedef Types<int, long> TypedTestTypes;
  125. TYPED_TEST_CASE(TypedTest, TypedTestTypes);
  126. TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
  127. // Verifies that the type parameter name is output in the 'type_param'
  128. // XML attribute for type-parameterized tests.
  129. template <typename T> class TypeParameterizedTestCase : public Test {};
  130. TYPED_TEST_CASE_P(TypeParameterizedTestCase);
  131. TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
  132. REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
  133. typedef Types<int, long> TypeParameterizedTestCaseTypes;
  134. INSTANTIATE_TYPED_TEST_CASE_P(Single,
  135. TypeParameterizedTestCase,
  136. TypeParameterizedTestCaseTypes);
  137. int main(int argc, char** argv) {
  138. InitGoogleTest(&argc, argv);
  139. if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
  140. TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
  141. delete listeners.Release(listeners.default_xml_generator());
  142. }
  143. return RUN_ALL_TESTS();
  144. }