Home | History | Annotate | Download | only in test
      1 // Copyright 2009 Google Inc.  All rights reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //     * Redistributions of source code must retain the above copyright
      8 // notice, this list of conditions and the following disclaimer.
      9 //     * Redistributions in binary form must reproduce the above
     10 // copyright notice, this list of conditions and the following disclaimer
     11 // in the documentation and/or other materials provided with the
     12 // distribution.
     13 //     * Neither the name of Google Inc. nor the names of its
     14 // contributors may be used to endorse or promote products derived from
     15 // this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 //
     29 // Author: vladl (at) google.com (Vlad Losev)
     30 //
     31 // The Google C++ Testing Framework (Google Test)
     32 //
     33 // This file contains tests verifying correctness of data provided via
     34 // UnitTest's public methods.
     35 
     36 #include <gtest/gtest.h>
     37 
     38 #include <string.h>  // For strcmp.
     39 #include <algorithm>
     40 
     41 using ::testing::InitGoogleTest;
     42 
     43 namespace testing {
     44 namespace internal {
     45 
     46 template <typename T>
     47 struct LessByName {
     48   bool operator()(const T* a, const T* b) {
     49     return strcmp(a->name(), b->name()) < 0;
     50   }
     51 };
     52 
     53 class UnitTestHelper {
     54  public:
     55   // Returns the array of pointers to all test cases sorted by the test case
     56   // name.  The caller is responsible for deleting the array.
     57   static TestCase const** const GetSortedTestCases() {
     58     UnitTest& unit_test = *UnitTest::GetInstance();
     59     TestCase const** const test_cases =
     60         new const TestCase*[unit_test.total_test_case_count()];
     61 
     62     for (int i = 0; i < unit_test.total_test_case_count(); ++i)
     63       test_cases[i] = unit_test.GetTestCase(i);
     64 
     65     std::sort(test_cases,
     66               test_cases + unit_test.total_test_case_count(),
     67               LessByName<TestCase>());
     68     return test_cases;
     69   }
     70 
     71   // Returns the test case by its name.  The caller doesn't own the returned
     72   // pointer.
     73   static const TestCase* FindTestCase(const char* name) {
     74     UnitTest& unit_test = *UnitTest::GetInstance();
     75     for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
     76       const TestCase* test_case = unit_test.GetTestCase(i);
     77       if (0 == strcmp(test_case->name(), name))
     78         return test_case;
     79     }
     80     return NULL;
     81   }
     82 
     83   // Returns the array of pointers to all tests in a particular test case
     84   // sorted by the test name.  The caller is responsible for deleting the
     85   // array.
     86   static TestInfo const** const GetSortedTests(const TestCase* test_case) {
     87     TestInfo const** const tests =
     88         new const TestInfo*[test_case->total_test_count()];
     89 
     90     for (int i = 0; i < test_case->total_test_count(); ++i)
     91       tests[i] = test_case->GetTestInfo(i);
     92 
     93     std::sort(tests, tests + test_case->total_test_count(),
     94               LessByName<TestInfo>());
     95     return tests;
     96   }
     97 };
     98 
     99 #if GTEST_HAS_TYPED_TEST
    100 template <typename T> class TestCaseWithCommentTest : public Test {};
    101 TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
    102 TYPED_TEST(TestCaseWithCommentTest, Dummy) {}
    103 
    104 const int kTypedTestCases = 1;
    105 const int kTypedTests = 1;
    106 
    107 String GetExpectedTestCaseComment() {
    108   Message comment;
    109   comment << "TypeParam = " << GetTypeName<int>().c_str();
    110   return comment.GetString();
    111 }
    112 #else
    113 const int kTypedTestCases = 0;
    114 const int kTypedTests = 0;
    115 #endif  // GTEST_HAS_TYPED_TEST
    116 
    117 // We can only test the accessors that do not change value while tests run.
    118 // Since tests can be run in any order, the values the accessors that track
    119 // test execution (such as failed_test_count) can not be predicted.
    120 TEST(ApiTest, UnitTestImmutableAccessorsWork) {
    121   UnitTest* unit_test = UnitTest::GetInstance();
    122 
    123   ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
    124   EXPECT_EQ(1 + kTypedTestCases, unit_test->test_case_to_run_count());
    125   EXPECT_EQ(2, unit_test->disabled_test_count());
    126   EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
    127   EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
    128 
    129   const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
    130 
    131   EXPECT_STREQ("ApiTest", test_cases[0]->name());
    132   EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
    133 #if GTEST_HAS_TYPED_TEST
    134   EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
    135 #endif  // GTEST_HAS_TYPED_TEST
    136 
    137   delete[] test_cases;
    138 
    139   // The following lines initiate actions to verify certain methods in
    140   // FinalSuccessChecker::TearDown.
    141 
    142   // Records a test property to verify TestResult::GetTestProperty().
    143   RecordProperty("key", "value");
    144 }
    145 
    146 TEST(ApiTest, TestCaseImmutableAccessorsWork) {
    147   const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
    148   ASSERT_TRUE(test_case != NULL);
    149 
    150   EXPECT_STREQ("ApiTest", test_case->name());
    151   EXPECT_STREQ("", test_case->comment());
    152   EXPECT_TRUE(test_case->should_run());
    153   EXPECT_EQ(1, test_case->disabled_test_count());
    154   EXPECT_EQ(3, test_case->test_to_run_count());
    155   ASSERT_EQ(4, test_case->total_test_count());
    156 
    157   const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
    158 
    159   EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
    160   EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
    161   EXPECT_STREQ("", tests[0]->comment());
    162   EXPECT_STREQ("", tests[0]->test_case_comment());
    163   EXPECT_FALSE(tests[0]->should_run());
    164 
    165   EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
    166   EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
    167   EXPECT_STREQ("", tests[1]->comment());
    168   EXPECT_STREQ("", tests[1]->test_case_comment());
    169   EXPECT_TRUE(tests[1]->should_run());
    170 
    171   EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
    172   EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
    173   EXPECT_STREQ("", tests[2]->comment());
    174   EXPECT_STREQ("", tests[2]->test_case_comment());
    175   EXPECT_TRUE(tests[2]->should_run());
    176 
    177   EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
    178   EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
    179   EXPECT_STREQ("", tests[3]->comment());
    180   EXPECT_STREQ("", tests[3]->test_case_comment());
    181   EXPECT_TRUE(tests[3]->should_run());
    182 
    183   delete[] tests;
    184   tests = NULL;
    185 
    186 #if GTEST_HAS_TYPED_TEST
    187   test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
    188   ASSERT_TRUE(test_case != NULL);
    189 
    190   EXPECT_STREQ("TestCaseWithCommentTest/0", test_case->name());
    191   EXPECT_STREQ(GetExpectedTestCaseComment().c_str(), test_case->comment());
    192   EXPECT_TRUE(test_case->should_run());
    193   EXPECT_EQ(0, test_case->disabled_test_count());
    194   EXPECT_EQ(1, test_case->test_to_run_count());
    195   ASSERT_EQ(1, test_case->total_test_count());
    196 
    197   tests = UnitTestHelper::GetSortedTests(test_case);
    198 
    199   EXPECT_STREQ("Dummy", tests[0]->name());
    200   EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
    201   EXPECT_STREQ("", tests[0]->comment());
    202   EXPECT_STREQ(GetExpectedTestCaseComment().c_str(),
    203                tests[0]->test_case_comment());
    204   EXPECT_TRUE(tests[0]->should_run());
    205 
    206   delete[] tests;
    207 #endif  // GTEST_HAS_TYPED_TEST
    208 }
    209 
    210 TEST(ApiTest, TestCaseDisabledAccessorsWork) {
    211   const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
    212   ASSERT_TRUE(test_case != NULL);
    213 
    214   EXPECT_STREQ("DISABLED_Test", test_case->name());
    215   EXPECT_STREQ("", test_case->comment());
    216   EXPECT_FALSE(test_case->should_run());
    217   EXPECT_EQ(1, test_case->disabled_test_count());
    218   EXPECT_EQ(0, test_case->test_to_run_count());
    219   ASSERT_EQ(1, test_case->total_test_count());
    220 
    221   const TestInfo* const test_info = test_case->GetTestInfo(0);
    222   EXPECT_STREQ("Dummy2", test_info->name());
    223   EXPECT_STREQ("DISABLED_Test", test_info->test_case_name());
    224   EXPECT_STREQ("", test_info->comment());
    225   EXPECT_STREQ("", test_info->test_case_comment());
    226   EXPECT_FALSE(test_info->should_run());
    227 }
    228 
    229 // These two tests are here to provide support for testing
    230 // test_case_to_run_count, disabled_test_count, and test_to_run_count.
    231 TEST(ApiTest, DISABLED_Dummy1) {}
    232 TEST(DISABLED_Test, Dummy2) {}
    233 
    234 class FinalSuccessChecker : public Environment {
    235  protected:
    236   virtual void TearDown() {
    237     UnitTest* unit_test = UnitTest::GetInstance();
    238 
    239     EXPECT_EQ(1 + kTypedTestCases, unit_test->successful_test_case_count());
    240     EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
    241     EXPECT_EQ(0, unit_test->failed_test_case_count());
    242     EXPECT_EQ(0, unit_test->failed_test_count());
    243     EXPECT_TRUE(unit_test->Passed());
    244     EXPECT_FALSE(unit_test->Failed());
    245     ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
    246 
    247     const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
    248 
    249     EXPECT_STREQ("ApiTest", test_cases[0]->name());
    250     EXPECT_STREQ("", test_cases[0]->comment());
    251     EXPECT_TRUE(test_cases[0]->should_run());
    252     EXPECT_EQ(1, test_cases[0]->disabled_test_count());
    253     ASSERT_EQ(4, test_cases[0]->total_test_count());
    254     EXPECT_EQ(3, test_cases[0]->successful_test_count());
    255     EXPECT_EQ(0, test_cases[0]->failed_test_count());
    256     EXPECT_TRUE(test_cases[0]->Passed());
    257     EXPECT_FALSE(test_cases[0]->Failed());
    258 
    259     EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
    260     EXPECT_STREQ("", test_cases[1]->comment());
    261     EXPECT_FALSE(test_cases[1]->should_run());
    262     EXPECT_EQ(1, test_cases[1]->disabled_test_count());
    263     ASSERT_EQ(1, test_cases[1]->total_test_count());
    264     EXPECT_EQ(0, test_cases[1]->successful_test_count());
    265     EXPECT_EQ(0, test_cases[1]->failed_test_count());
    266 
    267 #if GTEST_HAS_TYPED_TEST
    268     EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
    269     EXPECT_STREQ(GetExpectedTestCaseComment().c_str(),
    270                  test_cases[2]->comment());
    271     EXPECT_TRUE(test_cases[2]->should_run());
    272     EXPECT_EQ(0, test_cases[2]->disabled_test_count());
    273     ASSERT_EQ(1, test_cases[2]->total_test_count());
    274     EXPECT_EQ(1, test_cases[2]->successful_test_count());
    275     EXPECT_EQ(0, test_cases[2]->failed_test_count());
    276     EXPECT_TRUE(test_cases[2]->Passed());
    277     EXPECT_FALSE(test_cases[2]->Failed());
    278 #endif  // GTEST_HAS_TYPED_TEST
    279 
    280     const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
    281     const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
    282     EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
    283     EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
    284     EXPECT_FALSE(tests[0]->should_run());
    285 
    286     EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
    287     EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
    288     EXPECT_STREQ("", tests[1]->comment());
    289     EXPECT_STREQ("", tests[1]->test_case_comment());
    290     EXPECT_TRUE(tests[1]->should_run());
    291     EXPECT_TRUE(tests[1]->result()->Passed());
    292     EXPECT_EQ(0, tests[1]->result()->test_property_count());
    293 
    294     EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
    295     EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
    296     EXPECT_STREQ("", tests[2]->comment());
    297     EXPECT_STREQ("", tests[2]->test_case_comment());
    298     EXPECT_TRUE(tests[2]->should_run());
    299     EXPECT_TRUE(tests[2]->result()->Passed());
    300     EXPECT_EQ(0, tests[2]->result()->test_property_count());
    301 
    302     EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
    303     EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
    304     EXPECT_STREQ("", tests[3]->comment());
    305     EXPECT_STREQ("", tests[3]->test_case_comment());
    306     EXPECT_TRUE(tests[3]->should_run());
    307     EXPECT_TRUE(tests[3]->result()->Passed());
    308     EXPECT_EQ(1, tests[3]->result()->test_property_count());
    309     const TestProperty& property = tests[3]->result()->GetTestProperty(0);
    310     EXPECT_STREQ("key", property.key());
    311     EXPECT_STREQ("value", property.value());
    312 
    313     delete[] tests;
    314 
    315 #if GTEST_HAS_TYPED_TEST
    316     test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
    317     tests = UnitTestHelper::GetSortedTests(test_case);
    318 
    319     EXPECT_STREQ("Dummy", tests[0]->name());
    320     EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
    321     EXPECT_STREQ("", tests[0]->comment());
    322     EXPECT_STREQ(GetExpectedTestCaseComment().c_str(),
    323                  tests[0]->test_case_comment());
    324     EXPECT_TRUE(tests[0]->should_run());
    325     EXPECT_TRUE(tests[0]->result()->Passed());
    326     EXPECT_EQ(0, tests[0]->result()->test_property_count());
    327 
    328     delete[] tests;
    329 #endif  // GTEST_HAS_TYPED_TEST
    330     delete[] test_cases;
    331   }
    332 };
    333 
    334 }  // namespace internal
    335 }  // namespace testing
    336 
    337 int main(int argc, char **argv) {
    338   InitGoogleTest(&argc, argv);
    339 
    340   AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
    341 
    342   return RUN_ALL_TESTS();
    343 }
    344