?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

  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. //
  30. // The Google C++ Testing and Mocking Framework (Google Test)
  31. //
  32. // This file contains tests verifying correctness of data provided via
  33. // UnitTest's public methods.
  34.  
  35. #include "gtest/gtest.h"
  36.  
  37. #include <string.h>  // For strcmp.
  38. #include <algorithm>
  39.  
  40. using ::testing::InitGoogleTest;
  41.  
  42. namespace testing {
  43. namespace internal {
  44.  
  45. template <typename T>
  46. struct LessByName {
  47.   bool operator()(const T* a, const T* b) {
  48.     return strcmp(a->name(), b->name()) < 0;
  49.   }
  50. };
  51.  
  52. class UnitTestHelper {
  53.  public:
  54.   // Returns the array of pointers to all test cases sorted by the test case
  55.   // name.  The caller is responsible for deleting the array.
  56.   static TestCase const** GetSortedTestCases() {
  57.     UnitTest& unit_test = *UnitTest::GetInstance();
  58.     TestCase const** const test_cases =
  59.         new const TestCase*[unit_test.total_test_case_count()];
  60.  
  61.     for (int i = 0; i < unit_test.total_test_case_count(); ++i)
  62.       test_cases[i] = unit_test.GetTestCase(i);
  63.  
  64.     std::sort(test_cases,
  65.               test_cases + unit_test.total_test_case_count(),
  66.               LessByName<TestCase>());
  67.     return test_cases;
  68.   }
  69.  
  70.   // Returns the test case by its name.  The caller doesn't own the returned
  71.   // pointer.
  72.   static const TestCase* FindTestCase(const char* name) {
  73.     UnitTest& unit_test = *UnitTest::GetInstance();
  74.     for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
  75.       const TestCase* test_case = unit_test.GetTestCase(i);
  76.       if (0 == strcmp(test_case->name(), name))
  77.         return test_case;
  78.     }
  79.     return NULL;
  80.   }
  81.  
  82.   // Returns the array of pointers to all tests in a particular test case
  83.   // sorted by the test name.  The caller is responsible for deleting the
  84.   // array.
  85.   static TestInfo const** GetSortedTests(const TestCase* test_case) {
  86.     TestInfo const** const tests =
  87.         new const TestInfo*[test_case->total_test_count()];
  88.  
  89.     for (int i = 0; i < test_case->total_test_count(); ++i)
  90.       tests[i] = test_case->GetTestInfo(i);
  91.  
  92.     std::sort(tests, tests + test_case->total_test_count(),
  93.               LessByName<TestInfo>());
  94.     return tests;
  95.   }
  96. };
  97.  
  98. #if GTEST_HAS_TYPED_TEST
  99. template <typename T> class TestCaseWithCommentTest : public Test {};
  100. TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
  101. TYPED_TEST(TestCaseWithCommentTest, Dummy) {}
  102.  
  103. const int kTypedTestCases = 1;
  104. const int kTypedTests = 1;
  105. #else
  106. const int kTypedTestCases = 0;
  107. const int kTypedTests = 0;
  108. #endif  // GTEST_HAS_TYPED_TEST
  109.  
  110. // We can only test the accessors that do not change value while tests run.
  111. // Since tests can be run in any order, the values the accessors that track
  112. // test execution (such as failed_test_count) can not be predicted.
  113. TEST(ApiTest, UnitTestImmutableAccessorsWork) {
  114.   UnitTest* unit_test = UnitTest::GetInstance();
  115.  
  116.   ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
  117.   EXPECT_EQ(1 + kTypedTestCases, unit_test->test_case_to_run_count());
  118.   EXPECT_EQ(2, unit_test->disabled_test_count());
  119.   EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
  120.   EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
  121.  
  122.   const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
  123.  
  124.   EXPECT_STREQ("ApiTest", test_cases[0]->name());
  125.   EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
  126. #if GTEST_HAS_TYPED_TEST
  127.   EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
  128. #endif  // GTEST_HAS_TYPED_TEST
  129.  
  130.   delete[] test_cases;
  131.  
  132.   // The following lines initiate actions to verify certain methods in
  133.   // FinalSuccessChecker::TearDown.
  134.  
  135.   // Records a test property to verify TestResult::GetTestProperty().
  136.   RecordProperty("key", "value");
  137. }
  138.  
  139. AssertionResult IsNull(const char* str) {
  140.   if (str != NULL) {
  141.     return testing::AssertionFailure() << "argument is " << str;
  142.   }
  143.   return AssertionSuccess();
  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_TRUE(IsNull(test_case->type_param()));
  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_TRUE(IsNull(tests[0]->value_param()));
  162.   EXPECT_TRUE(IsNull(tests[0]->type_param()));
  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_TRUE(IsNull(tests[1]->value_param()));
  168.   EXPECT_TRUE(IsNull(tests[1]->type_param()));
  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_TRUE(IsNull(tests[2]->value_param()));
  174.   EXPECT_TRUE(IsNull(tests[2]->type_param()));
  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_TRUE(IsNull(tests[3]->value_param()));
  180.   EXPECT_TRUE(IsNull(tests[3]->type_param()));
  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(GetTypeName<int>().c_str(), test_case->type_param());
  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_TRUE(IsNull(tests[0]->value_param()));
  202.   EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
  203.   EXPECT_TRUE(tests[0]->should_run());
  204.  
  205.   delete[] tests;
  206. #endif  // GTEST_HAS_TYPED_TEST
  207. }
  208.  
  209. TEST(ApiTest, TestCaseDisabledAccessorsWork) {
  210.   const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
  211.   ASSERT_TRUE(test_case != NULL);
  212.  
  213.   EXPECT_STREQ("DISABLED_Test", test_case->name());
  214.   EXPECT_TRUE(IsNull(test_case->type_param()));
  215.   EXPECT_FALSE(test_case->should_run());
  216.   EXPECT_EQ(1, test_case->disabled_test_count());
  217.   EXPECT_EQ(0, test_case->test_to_run_count());
  218.   ASSERT_EQ(1, test_case->total_test_count());
  219.  
  220.   const TestInfo* const test_info = test_case->GetTestInfo(0);
  221.   EXPECT_STREQ("Dummy2", test_info->name());
  222.   EXPECT_STREQ("DISABLED_Test", test_info->test_case_name());
  223.   EXPECT_TRUE(IsNull(test_info->value_param()));
  224.   EXPECT_TRUE(IsNull(test_info->type_param()));
  225.   EXPECT_FALSE(test_info->should_run());
  226. }
  227.  
  228. // These two tests are here to provide support for testing
  229. // test_case_to_run_count, disabled_test_count, and test_to_run_count.
  230. TEST(ApiTest, DISABLED_Dummy1) {}
  231. TEST(DISABLED_Test, Dummy2) {}
  232.  
  233. class FinalSuccessChecker : public Environment {
  234.  protected:
  235.   virtual void TearDown() {
  236.     UnitTest* unit_test = UnitTest::GetInstance();
  237.  
  238.     EXPECT_EQ(1 + kTypedTestCases, unit_test->successful_test_case_count());
  239.     EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
  240.     EXPECT_EQ(0, unit_test->failed_test_case_count());
  241.     EXPECT_EQ(0, unit_test->failed_test_count());
  242.     EXPECT_TRUE(unit_test->Passed());
  243.     EXPECT_FALSE(unit_test->Failed());
  244.     ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
  245.  
  246.     const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
  247.  
  248.     EXPECT_STREQ("ApiTest", test_cases[0]->name());
  249.     EXPECT_TRUE(IsNull(test_cases[0]->type_param()));
  250.     EXPECT_TRUE(test_cases[0]->should_run());
  251.     EXPECT_EQ(1, test_cases[0]->disabled_test_count());
  252.     ASSERT_EQ(4, test_cases[0]->total_test_count());
  253.     EXPECT_EQ(3, test_cases[0]->successful_test_count());
  254.     EXPECT_EQ(0, test_cases[0]->failed_test_count());
  255.     EXPECT_TRUE(test_cases[0]->Passed());
  256.     EXPECT_FALSE(test_cases[0]->Failed());
  257.  
  258.     EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
  259.     EXPECT_TRUE(IsNull(test_cases[1]->type_param()));
  260.     EXPECT_FALSE(test_cases[1]->should_run());
  261.     EXPECT_EQ(1, test_cases[1]->disabled_test_count());
  262.     ASSERT_EQ(1, test_cases[1]->total_test_count());
  263.     EXPECT_EQ(0, test_cases[1]->successful_test_count());
  264.     EXPECT_EQ(0, test_cases[1]->failed_test_count());
  265.  
  266. #if GTEST_HAS_TYPED_TEST
  267.     EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
  268.     EXPECT_STREQ(GetTypeName<int>().c_str(), test_cases[2]->type_param());
  269.     EXPECT_TRUE(test_cases[2]->should_run());
  270.     EXPECT_EQ(0, test_cases[2]->disabled_test_count());
  271.     ASSERT_EQ(1, test_cases[2]->total_test_count());
  272.     EXPECT_EQ(1, test_cases[2]->successful_test_count());
  273.     EXPECT_EQ(0, test_cases[2]->failed_test_count());
  274.     EXPECT_TRUE(test_cases[2]->Passed());
  275.     EXPECT_FALSE(test_cases[2]->Failed());
  276. #endif  // GTEST_HAS_TYPED_TEST
  277.  
  278.     const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
  279.     const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
  280.     EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
  281.     EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
  282.     EXPECT_FALSE(tests[0]->should_run());
  283.  
  284.     EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
  285.     EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
  286.     EXPECT_TRUE(IsNull(tests[1]->value_param()));
  287.     EXPECT_TRUE(IsNull(tests[1]->type_param()));
  288.     EXPECT_TRUE(tests[1]->should_run());
  289.     EXPECT_TRUE(tests[1]->result()->Passed());
  290.     EXPECT_EQ(0, tests[1]->result()->test_property_count());
  291.  
  292.     EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
  293.     EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
  294.     EXPECT_TRUE(IsNull(tests[2]->value_param()));
  295.     EXPECT_TRUE(IsNull(tests[2]->type_param()));
  296.     EXPECT_TRUE(tests[2]->should_run());
  297.     EXPECT_TRUE(tests[2]->result()->Passed());
  298.     EXPECT_EQ(0, tests[2]->result()->test_property_count());
  299.  
  300.     EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
  301.     EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
  302.     EXPECT_TRUE(IsNull(tests[3]->value_param()));
  303.     EXPECT_TRUE(IsNull(tests[3]->type_param()));
  304.     EXPECT_TRUE(tests[3]->should_run());
  305.     EXPECT_TRUE(tests[3]->result()->Passed());
  306.     EXPECT_EQ(1, tests[3]->result()->test_property_count());
  307.     const TestProperty& property = tests[3]->result()->GetTestProperty(0);
  308.     EXPECT_STREQ("key", property.key());
  309.     EXPECT_STREQ("value", property.value());
  310.  
  311.     delete[] tests;
  312.  
  313. #if GTEST_HAS_TYPED_TEST
  314.     test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
  315.     tests = UnitTestHelper::GetSortedTests(test_case);
  316.  
  317.     EXPECT_STREQ("Dummy", tests[0]->name());
  318.     EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
  319.     EXPECT_TRUE(IsNull(tests[0]->value_param()));
  320.     EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
  321.     EXPECT_TRUE(tests[0]->should_run());
  322.     EXPECT_TRUE(tests[0]->result()->Passed());
  323.     EXPECT_EQ(0, tests[0]->result()->test_property_count());
  324.  
  325.     delete[] tests;
  326. #endif  // GTEST_HAS_TYPED_TEST
  327.     delete[] test_cases;
  328.   }
  329. };
  330.  
  331. }  // namespace internal
  332. }  // namespace testing
  333.  
  334. int main(int argc, char **argv) {
  335.   InitGoogleTest(&argc, argv);
  336.  
  337.   AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
  338.  
  339.   return RUN_ALL_TESTS();
  340. }
  341.