00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "gtest/gtest.h"
00042
00043 using ::testing::InitGoogleTest;
00044 using ::testing::TestEventListeners;
00045 using ::testing::TestWithParam;
00046 using ::testing::UnitTest;
00047 using ::testing::Test;
00048 using ::testing::Values;
00049
00050 class SuccessfulTest : public Test {
00051 };
00052
00053 TEST_F(SuccessfulTest, Succeeds) {
00054 SUCCEED() << "This is a success.";
00055 ASSERT_EQ(1, 1);
00056 }
00057
00058 class FailedTest : public Test {
00059 };
00060
00061 TEST_F(FailedTest, Fails) {
00062 ASSERT_EQ(1, 2);
00063 }
00064
00065 class DisabledTest : public Test {
00066 };
00067
00068 TEST_F(DisabledTest, DISABLED_test_not_run) {
00069 FAIL() << "Unexpected failure: Disabled test should not be run";
00070 }
00071
00072 TEST(MixedResultTest, Succeeds) {
00073 EXPECT_EQ(1, 1);
00074 ASSERT_EQ(1, 1);
00075 }
00076
00077 TEST(MixedResultTest, Fails) {
00078 EXPECT_EQ(1, 2);
00079 ASSERT_EQ(2, 3);
00080 }
00081
00082 TEST(MixedResultTest, DISABLED_test) {
00083 FAIL() << "Unexpected failure: Disabled test should not be run";
00084 }
00085
00086 TEST(XmlQuotingTest, OutputsCData) {
00087 FAIL() << "XML output: "
00088 "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
00089 }
00090
00091
00092
00093 TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
00094 FAIL() << "Invalid characters in brackets [\x1\x2]";
00095 }
00096
00097 class PropertyRecordingTest : public Test {
00098 public:
00099 static void SetUpTestCase() { RecordProperty("SetUpTestCase", "yes"); }
00100 static void TearDownTestCase() { RecordProperty("TearDownTestCase", "aye"); }
00101 };
00102
00103 TEST_F(PropertyRecordingTest, OneProperty) {
00104 RecordProperty("key_1", "1");
00105 }
00106
00107 TEST_F(PropertyRecordingTest, IntValuedProperty) {
00108 RecordProperty("key_int", 1);
00109 }
00110
00111 TEST_F(PropertyRecordingTest, ThreeProperties) {
00112 RecordProperty("key_1", "1");
00113 RecordProperty("key_2", "2");
00114 RecordProperty("key_3", "3");
00115 }
00116
00117 TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
00118 RecordProperty("key_1", "1");
00119 RecordProperty("key_1", "2");
00120 }
00121
00122 TEST(NoFixtureTest, RecordProperty) {
00123 RecordProperty("key", "1");
00124 }
00125
00126 void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
00127 testing::Test::RecordProperty(key, value);
00128 }
00129
00130 void ExternalUtilityThatCallsRecordProperty(const std::string& key,
00131 const std::string& value) {
00132 testing::Test::RecordProperty(key, value);
00133 }
00134
00135 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
00136 ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
00137 }
00138
00139 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
00140 ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
00141 }
00142
00143
00144
00145 class ValueParamTest : public TestWithParam<int> {};
00146 TEST_P(ValueParamTest, HasValueParamAttribute) {}
00147 TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
00148 INSTANTIATE_TEST_CASE_P(Single, ValueParamTest, Values(33, 42));
00149
00150 #if GTEST_HAS_TYPED_TEST
00151
00152
00153 template <typename T> class TypedTest : public Test {};
00154 typedef testing::Types<int, long> TypedTestTypes;
00155 TYPED_TEST_CASE(TypedTest, TypedTestTypes);
00156 TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
00157 #endif
00158
00159 #if GTEST_HAS_TYPED_TEST_P
00160
00161
00162 template <typename T> class TypeParameterizedTestCase : public Test {};
00163 TYPED_TEST_CASE_P(TypeParameterizedTestCase);
00164 TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
00165 REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
00166 typedef testing::Types<int, long> TypeParameterizedTestCaseTypes;
00167 INSTANTIATE_TYPED_TEST_CASE_P(Single,
00168 TypeParameterizedTestCase,
00169 TypeParameterizedTestCaseTypes);
00170 #endif
00171
00172 int main(int argc, char** argv) {
00173 InitGoogleTest(&argc, argv);
00174
00175 if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
00176 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
00177 delete listeners.Release(listeners.default_xml_generator());
00178 }
00179 testing::Test::RecordProperty("ad_hoc_property", "42");
00180 return RUN_ALL_TESTS();
00181 }