gtest-unittest-api_test.cc
Go to the documentation of this file.
00001 // Copyright 2009 Google Inc.  All rights reserved.
00002 //
00003 // Redistribution and use in source and binary forms, with or without
00004 // modification, are permitted provided that the following conditions are
00005 // met:
00006 //
00007 //     * Redistributions of source code must retain the above copyright
00008 // notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above
00010 // copyright notice, this list of conditions and the following disclaimer
00011 // in the documentation and/or other materials provided with the
00012 // distribution.
00013 //     * Neither the name of Google Inc. nor the names of its
00014 // contributors may be used to endorse or promote products derived from
00015 // this software without specific prior written permission.
00016 //
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00018 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00020 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00021 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00022 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00027 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028 //
00029 // Author: vladl@google.com (Vlad Losev)
00030 //
00031 // The Google C++ Testing Framework (Google Test)
00032 //
00033 // This file contains tests verifying correctness of data provided via
00034 // UnitTest's public methods.
00035 
00036 #include "gtest/gtest.h"
00037 
00038 #include <string.h>  // For strcmp.
00039 #include <algorithm>
00040 
00041 using ::testing::InitGoogleTest;
00042 
00043 namespace testing {
00044 namespace internal {
00045 
00046 template <typename T>
00047 struct LessByName {
00048   bool operator()(const T* a, const T* b) {
00049     return strcmp(a->name(), b->name()) < 0;
00050   }
00051 };
00052 
00053 class UnitTestHelper {
00054  public:
00055   // Returns the array of pointers to all test cases sorted by the test case
00056   // name.  The caller is responsible for deleting the array.
00057   static TestCase const** const GetSortedTestCases() {
00058     UnitTest& unit_test = *UnitTest::GetInstance();
00059     TestCase const** const test_cases =
00060         new const TestCase*[unit_test.total_test_case_count()];
00061 
00062     for (int i = 0; i < unit_test.total_test_case_count(); ++i)
00063       test_cases[i] = unit_test.GetTestCase(i);
00064 
00065     std::sort(test_cases,
00066               test_cases + unit_test.total_test_case_count(),
00067               LessByName<TestCase>());
00068     return test_cases;
00069   }
00070 
00071   // Returns the test case by its name.  The caller doesn't own the returned
00072   // pointer.
00073   static const TestCase* FindTestCase(const char* name) {
00074     UnitTest& unit_test = *UnitTest::GetInstance();
00075     for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
00076       const TestCase* test_case = unit_test.GetTestCase(i);
00077       if (0 == strcmp(test_case->name(), name))
00078         return test_case;
00079     }
00080     return NULL;
00081   }
00082 
00083   // Returns the array of pointers to all tests in a particular test case
00084   // sorted by the test name.  The caller is responsible for deleting the
00085   // array.
00086   static TestInfo const** const GetSortedTests(const TestCase* test_case) {
00087     TestInfo const** const tests =
00088         new const TestInfo*[test_case->total_test_count()];
00089 
00090     for (int i = 0; i < test_case->total_test_count(); ++i)
00091       tests[i] = test_case->GetTestInfo(i);
00092 
00093     std::sort(tests, tests + test_case->total_test_count(),
00094               LessByName<TestInfo>());
00095     return tests;
00096   }
00097 };
00098 
00099 #if GTEST_HAS_TYPED_TEST
00100 template <typename T> class TestCaseWithCommentTest : public Test {};
00101 TYPED_TEST_CASE(TestCaseWithCommentTest, Types<int>);
00102 TYPED_TEST(TestCaseWithCommentTest, Dummy) {}
00103 
00104 const int kTypedTestCases = 1;
00105 const int kTypedTests = 1;
00106 #else
00107 const int kTypedTestCases = 0;
00108 const int kTypedTests = 0;
00109 #endif  // GTEST_HAS_TYPED_TEST
00110 
00111 // We can only test the accessors that do not change value while tests run.
00112 // Since tests can be run in any order, the values the accessors that track
00113 // test execution (such as failed_test_count) can not be predicted.
00114 TEST(ApiTest, UnitTestImmutableAccessorsWork) {
00115   UnitTest* unit_test = UnitTest::GetInstance();
00116 
00117   ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
00118   EXPECT_EQ(1 + kTypedTestCases, unit_test->test_case_to_run_count());
00119   EXPECT_EQ(2, unit_test->disabled_test_count());
00120   EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
00121   EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
00122 
00123   const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
00124 
00125   EXPECT_STREQ("ApiTest", test_cases[0]->name());
00126   EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
00127 #if GTEST_HAS_TYPED_TEST
00128   EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
00129 #endif  // GTEST_HAS_TYPED_TEST
00130 
00131   delete[] test_cases;
00132 
00133   // The following lines initiate actions to verify certain methods in
00134   // FinalSuccessChecker::TearDown.
00135 
00136   // Records a test property to verify TestResult::GetTestProperty().
00137   RecordProperty("key", "value");
00138 }
00139 
00140 AssertionResult IsNull(const char* str) {
00141   if (str != NULL) {
00142     return testing::AssertionFailure() << "argument is " << str;
00143   }
00144   return AssertionSuccess();
00145 }
00146 
00147 TEST(ApiTest, TestCaseImmutableAccessorsWork) {
00148   const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
00149   ASSERT_TRUE(test_case != NULL);
00150 
00151   EXPECT_STREQ("ApiTest", test_case->name());
00152   EXPECT_TRUE(IsNull(test_case->type_param()));
00153   EXPECT_TRUE(test_case->should_run());
00154   EXPECT_EQ(1, test_case->disabled_test_count());
00155   EXPECT_EQ(3, test_case->test_to_run_count());
00156   ASSERT_EQ(4, test_case->total_test_count());
00157 
00158   const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
00159 
00160   EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
00161   EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
00162   EXPECT_TRUE(IsNull(tests[0]->value_param()));
00163   EXPECT_TRUE(IsNull(tests[0]->type_param()));
00164   EXPECT_FALSE(tests[0]->should_run());
00165 
00166   EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
00167   EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
00168   EXPECT_TRUE(IsNull(tests[1]->value_param()));
00169   EXPECT_TRUE(IsNull(tests[1]->type_param()));
00170   EXPECT_TRUE(tests[1]->should_run());
00171 
00172   EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
00173   EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
00174   EXPECT_TRUE(IsNull(tests[2]->value_param()));
00175   EXPECT_TRUE(IsNull(tests[2]->type_param()));
00176   EXPECT_TRUE(tests[2]->should_run());
00177 
00178   EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
00179   EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
00180   EXPECT_TRUE(IsNull(tests[3]->value_param()));
00181   EXPECT_TRUE(IsNull(tests[3]->type_param()));
00182   EXPECT_TRUE(tests[3]->should_run());
00183 
00184   delete[] tests;
00185   tests = NULL;
00186 
00187 #if GTEST_HAS_TYPED_TEST
00188   test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
00189   ASSERT_TRUE(test_case != NULL);
00190 
00191   EXPECT_STREQ("TestCaseWithCommentTest/0", test_case->name());
00192   EXPECT_STREQ(GetTypeName<int>().c_str(), test_case->type_param());
00193   EXPECT_TRUE(test_case->should_run());
00194   EXPECT_EQ(0, test_case->disabled_test_count());
00195   EXPECT_EQ(1, test_case->test_to_run_count());
00196   ASSERT_EQ(1, test_case->total_test_count());
00197 
00198   tests = UnitTestHelper::GetSortedTests(test_case);
00199 
00200   EXPECT_STREQ("Dummy", tests[0]->name());
00201   EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
00202   EXPECT_TRUE(IsNull(tests[0]->value_param()));
00203   EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
00204   EXPECT_TRUE(tests[0]->should_run());
00205 
00206   delete[] tests;
00207 #endif  // GTEST_HAS_TYPED_TEST
00208 }
00209 
00210 TEST(ApiTest, TestCaseDisabledAccessorsWork) {
00211   const TestCase* test_case = UnitTestHelper::FindTestCase("DISABLED_Test");
00212   ASSERT_TRUE(test_case != NULL);
00213 
00214   EXPECT_STREQ("DISABLED_Test", test_case->name());
00215   EXPECT_TRUE(IsNull(test_case->type_param()));
00216   EXPECT_FALSE(test_case->should_run());
00217   EXPECT_EQ(1, test_case->disabled_test_count());
00218   EXPECT_EQ(0, test_case->test_to_run_count());
00219   ASSERT_EQ(1, test_case->total_test_count());
00220 
00221   const TestInfo* const test_info = test_case->GetTestInfo(0);
00222   EXPECT_STREQ("Dummy2", test_info->name());
00223   EXPECT_STREQ("DISABLED_Test", test_info->test_case_name());
00224   EXPECT_TRUE(IsNull(test_info->value_param()));
00225   EXPECT_TRUE(IsNull(test_info->type_param()));
00226   EXPECT_FALSE(test_info->should_run());
00227 }
00228 
00229 // These two tests are here to provide support for testing
00230 // test_case_to_run_count, disabled_test_count, and test_to_run_count.
00231 TEST(ApiTest, DISABLED_Dummy1) {}
00232 TEST(DISABLED_Test, Dummy2) {}
00233 
00234 class FinalSuccessChecker : public Environment {
00235  protected:
00236   virtual void TearDown() {
00237     UnitTest* unit_test = UnitTest::GetInstance();
00238 
00239     EXPECT_EQ(1 + kTypedTestCases, unit_test->successful_test_case_count());
00240     EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
00241     EXPECT_EQ(0, unit_test->failed_test_case_count());
00242     EXPECT_EQ(0, unit_test->failed_test_count());
00243     EXPECT_TRUE(unit_test->Passed());
00244     EXPECT_FALSE(unit_test->Failed());
00245     ASSERT_EQ(2 + kTypedTestCases, unit_test->total_test_case_count());
00246 
00247     const TestCase** const test_cases = UnitTestHelper::GetSortedTestCases();
00248 
00249     EXPECT_STREQ("ApiTest", test_cases[0]->name());
00250     EXPECT_TRUE(IsNull(test_cases[0]->type_param()));
00251     EXPECT_TRUE(test_cases[0]->should_run());
00252     EXPECT_EQ(1, test_cases[0]->disabled_test_count());
00253     ASSERT_EQ(4, test_cases[0]->total_test_count());
00254     EXPECT_EQ(3, test_cases[0]->successful_test_count());
00255     EXPECT_EQ(0, test_cases[0]->failed_test_count());
00256     EXPECT_TRUE(test_cases[0]->Passed());
00257     EXPECT_FALSE(test_cases[0]->Failed());
00258 
00259     EXPECT_STREQ("DISABLED_Test", test_cases[1]->name());
00260     EXPECT_TRUE(IsNull(test_cases[1]->type_param()));
00261     EXPECT_FALSE(test_cases[1]->should_run());
00262     EXPECT_EQ(1, test_cases[1]->disabled_test_count());
00263     ASSERT_EQ(1, test_cases[1]->total_test_count());
00264     EXPECT_EQ(0, test_cases[1]->successful_test_count());
00265     EXPECT_EQ(0, test_cases[1]->failed_test_count());
00266 
00267 #if GTEST_HAS_TYPED_TEST
00268     EXPECT_STREQ("TestCaseWithCommentTest/0", test_cases[2]->name());
00269     EXPECT_STREQ(GetTypeName<int>().c_str(), test_cases[2]->type_param());
00270     EXPECT_TRUE(test_cases[2]->should_run());
00271     EXPECT_EQ(0, test_cases[2]->disabled_test_count());
00272     ASSERT_EQ(1, test_cases[2]->total_test_count());
00273     EXPECT_EQ(1, test_cases[2]->successful_test_count());
00274     EXPECT_EQ(0, test_cases[2]->failed_test_count());
00275     EXPECT_TRUE(test_cases[2]->Passed());
00276     EXPECT_FALSE(test_cases[2]->Failed());
00277 #endif  // GTEST_HAS_TYPED_TEST
00278 
00279     const TestCase* test_case = UnitTestHelper::FindTestCase("ApiTest");
00280     const TestInfo** tests = UnitTestHelper::GetSortedTests(test_case);
00281     EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
00282     EXPECT_STREQ("ApiTest", tests[0]->test_case_name());
00283     EXPECT_FALSE(tests[0]->should_run());
00284 
00285     EXPECT_STREQ("TestCaseDisabledAccessorsWork", tests[1]->name());
00286     EXPECT_STREQ("ApiTest", tests[1]->test_case_name());
00287     EXPECT_TRUE(IsNull(tests[1]->value_param()));
00288     EXPECT_TRUE(IsNull(tests[1]->type_param()));
00289     EXPECT_TRUE(tests[1]->should_run());
00290     EXPECT_TRUE(tests[1]->result()->Passed());
00291     EXPECT_EQ(0, tests[1]->result()->test_property_count());
00292 
00293     EXPECT_STREQ("TestCaseImmutableAccessorsWork", tests[2]->name());
00294     EXPECT_STREQ("ApiTest", tests[2]->test_case_name());
00295     EXPECT_TRUE(IsNull(tests[2]->value_param()));
00296     EXPECT_TRUE(IsNull(tests[2]->type_param()));
00297     EXPECT_TRUE(tests[2]->should_run());
00298     EXPECT_TRUE(tests[2]->result()->Passed());
00299     EXPECT_EQ(0, tests[2]->result()->test_property_count());
00300 
00301     EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
00302     EXPECT_STREQ("ApiTest", tests[3]->test_case_name());
00303     EXPECT_TRUE(IsNull(tests[3]->value_param()));
00304     EXPECT_TRUE(IsNull(tests[3]->type_param()));
00305     EXPECT_TRUE(tests[3]->should_run());
00306     EXPECT_TRUE(tests[3]->result()->Passed());
00307     EXPECT_EQ(1, tests[3]->result()->test_property_count());
00308     const TestProperty& property = tests[3]->result()->GetTestProperty(0);
00309     EXPECT_STREQ("key", property.key());
00310     EXPECT_STREQ("value", property.value());
00311 
00312     delete[] tests;
00313 
00314 #if GTEST_HAS_TYPED_TEST
00315     test_case = UnitTestHelper::FindTestCase("TestCaseWithCommentTest/0");
00316     tests = UnitTestHelper::GetSortedTests(test_case);
00317 
00318     EXPECT_STREQ("Dummy", tests[0]->name());
00319     EXPECT_STREQ("TestCaseWithCommentTest/0", tests[0]->test_case_name());
00320     EXPECT_TRUE(IsNull(tests[0]->value_param()));
00321     EXPECT_STREQ(GetTypeName<int>().c_str(), tests[0]->type_param());
00322     EXPECT_TRUE(tests[0]->should_run());
00323     EXPECT_TRUE(tests[0]->result()->Passed());
00324     EXPECT_EQ(0, tests[0]->result()->test_property_count());
00325 
00326     delete[] tests;
00327 #endif  // GTEST_HAS_TYPED_TEST
00328     delete[] test_cases;
00329   }
00330 };
00331 
00332 }  // namespace internal
00333 }  // namespace testing
00334 
00335 int main(int argc, char **argv) {
00336   InitGoogleTest(&argc, argv);
00337 
00338   AddGlobalTestEnvironment(new testing::internal::FinalSuccessChecker());
00339 
00340   return RUN_ALL_TESTS();
00341 }


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:48