Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gtest-unittest-api_test.cc
Go to the documentation of this file.
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 
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 
53  public:
54  // Returns the array of pointers to all test suites sorted by the test suite
55  // name. The caller is responsible for deleting the array.
56  static TestSuite const** GetSortedTestSuites() {
57  UnitTest& unit_test = *UnitTest::GetInstance();
58  auto const** const test_suites = new const TestSuite*[static_cast<size_t>(
59  unit_test.total_test_suite_count())];
60 
61  for (int i = 0; i < unit_test.total_test_suite_count(); ++i)
62  test_suites[i] = unit_test.GetTestSuite(i);
63 
64  std::sort(test_suites,
65  test_suites + unit_test.total_test_suite_count(),
67  return test_suites;
68  }
69 
70  // Returns the test suite by its name. The caller doesn't own the returned
71  // pointer.
72  static const TestSuite* FindTestSuite(const char* name) {
73  UnitTest& unit_test = *UnitTest::GetInstance();
74  for (int i = 0; i < unit_test.total_test_suite_count(); ++i) {
75  const TestSuite* test_suite = unit_test.GetTestSuite(i);
76  if (0 == strcmp(test_suite->name(), name))
77  return test_suite;
78  }
79  return nullptr;
80  }
81 
82  // Returns the array of pointers to all tests in a particular test suite
83  // sorted by the test name. The caller is responsible for deleting the
84  // array.
85  static TestInfo const** GetSortedTests(const TestSuite* test_suite) {
86  TestInfo const** const tests = new const TestInfo*[static_cast<size_t>(
87  test_suite->total_test_count())];
88 
89  for (int i = 0; i < test_suite->total_test_count(); ++i)
90  tests[i] = test_suite->GetTestInfo(i);
91 
92  std::sort(tests, tests + test_suite->total_test_count(),
94  return tests;
95  }
96 };
97 
98 #if GTEST_HAS_TYPED_TEST
99 template <typename T> class TestSuiteWithCommentTest : public Test {};
100 TYPED_TEST_SUITE(TestSuiteWithCommentTest, Types<int>);
101 TYPED_TEST(TestSuiteWithCommentTest, Dummy) {}
102 
103 const int kTypedTestSuites = 1;
104 const int kTypedTests = 1;
105 #else
106 const int kTypedTestSuites = 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 
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 TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
123 
124  EXPECT_STREQ("ApiTest", test_suites[0]->name());
125  EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
126 #if GTEST_HAS_TYPED_TEST
127  EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
128 #endif // GTEST_HAS_TYPED_TEST
129 
130  delete[] test_suites;
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 != nullptr) {
141  return testing::AssertionFailure() << "argument is " << str;
142  }
143  return AssertionSuccess();
144 }
145 
146 TEST(ApiTest, TestSuiteImmutableAccessorsWork) {
147  const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest");
148  ASSERT_TRUE(test_suite != nullptr);
149 
150  EXPECT_STREQ("ApiTest", test_suite->name());
151  EXPECT_TRUE(IsNull(test_suite->type_param()));
152  EXPECT_TRUE(test_suite->should_run());
153  EXPECT_EQ(1, test_suite->disabled_test_count());
154  EXPECT_EQ(3, test_suite->test_to_run_count());
155  ASSERT_EQ(4, test_suite->total_test_count());
156 
157  const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
158 
159  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
160  EXPECT_STREQ("ApiTest", tests[0]->test_suite_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("TestSuiteDisabledAccessorsWork", tests[1]->name());
166  EXPECT_STREQ("ApiTest", tests[1]->test_suite_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("TestSuiteImmutableAccessorsWork", tests[2]->name());
172  EXPECT_STREQ("ApiTest", tests[2]->test_suite_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_suite_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 = nullptr;
185 
186 #if GTEST_HAS_TYPED_TEST
187  test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
188  ASSERT_TRUE(test_suite != nullptr);
189 
190  EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite->name());
191  EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), test_suite->type_param());
192  EXPECT_TRUE(test_suite->should_run());
193  EXPECT_EQ(0, test_suite->disabled_test_count());
194  EXPECT_EQ(1, test_suite->test_to_run_count());
195  ASSERT_EQ(1, test_suite->total_test_count());
196 
197  tests = UnitTestHelper::GetSortedTests(test_suite);
198 
199  EXPECT_STREQ("Dummy", tests[0]->name());
200  EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
201  EXPECT_TRUE(IsNull(tests[0]->value_param()));
202  EXPECT_STREQ(GetTypeName<Types<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, TestSuiteDisabledAccessorsWork) {
210  const TestSuite* test_suite = UnitTestHelper::FindTestSuite("DISABLED_Test");
211  ASSERT_TRUE(test_suite != nullptr);
212 
213  EXPECT_STREQ("DISABLED_Test", test_suite->name());
214  EXPECT_TRUE(IsNull(test_suite->type_param()));
215  EXPECT_FALSE(test_suite->should_run());
216  EXPECT_EQ(1, test_suite->disabled_test_count());
217  EXPECT_EQ(0, test_suite->test_to_run_count());
218  ASSERT_EQ(1, test_suite->total_test_count());
219 
220  const TestInfo* const test_info = test_suite->GetTestInfo(0);
221  EXPECT_STREQ("Dummy2", test_info->name());
222  EXPECT_STREQ("DISABLED_Test", test_info->test_suite_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_suite_to_run_count, disabled_test_count, and test_to_run_count.
230 TEST(ApiTest, DISABLED_Dummy1) {}
231 TEST(DISABLED_Test, Dummy2) {}
232 
234  protected:
235  void TearDown() override {
236  UnitTest* unit_test = UnitTest::GetInstance();
237 
239  EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
240  EXPECT_EQ(0, unit_test->failed_test_suite_count());
241  EXPECT_EQ(0, unit_test->failed_test_count());
242  EXPECT_TRUE(unit_test->Passed());
243  EXPECT_FALSE(unit_test->Failed());
245 
246  const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
247 
248  EXPECT_STREQ("ApiTest", test_suites[0]->name());
249  EXPECT_TRUE(IsNull(test_suites[0]->type_param()));
250  EXPECT_TRUE(test_suites[0]->should_run());
251  EXPECT_EQ(1, test_suites[0]->disabled_test_count());
252  ASSERT_EQ(4, test_suites[0]->total_test_count());
253  EXPECT_EQ(3, test_suites[0]->successful_test_count());
254  EXPECT_EQ(0, test_suites[0]->failed_test_count());
255  EXPECT_TRUE(test_suites[0]->Passed());
256  EXPECT_FALSE(test_suites[0]->Failed());
257 
258  EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
259  EXPECT_TRUE(IsNull(test_suites[1]->type_param()));
260  EXPECT_FALSE(test_suites[1]->should_run());
261  EXPECT_EQ(1, test_suites[1]->disabled_test_count());
262  ASSERT_EQ(1, test_suites[1]->total_test_count());
263  EXPECT_EQ(0, test_suites[1]->successful_test_count());
264  EXPECT_EQ(0, test_suites[1]->failed_test_count());
265 
266 #if GTEST_HAS_TYPED_TEST
267  EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
269  test_suites[2]->type_param());
270  EXPECT_TRUE(test_suites[2]->should_run());
271  EXPECT_EQ(0, test_suites[2]->disabled_test_count());
272  ASSERT_EQ(1, test_suites[2]->total_test_count());
273  EXPECT_EQ(1, test_suites[2]->successful_test_count());
274  EXPECT_EQ(0, test_suites[2]->failed_test_count());
275  EXPECT_TRUE(test_suites[2]->Passed());
276  EXPECT_FALSE(test_suites[2]->Failed());
277 #endif // GTEST_HAS_TYPED_TEST
278 
279  const TestSuite* test_suite = UnitTestHelper::FindTestSuite("ApiTest");
280  const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
281  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
282  EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
283  EXPECT_FALSE(tests[0]->should_run());
284 
285  EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
286  EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
287  EXPECT_TRUE(IsNull(tests[1]->value_param()));
288  EXPECT_TRUE(IsNull(tests[1]->type_param()));
289  EXPECT_TRUE(tests[1]->should_run());
290  EXPECT_TRUE(tests[1]->result()->Passed());
291  EXPECT_EQ(0, tests[1]->result()->test_property_count());
292 
293  EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
294  EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
295  EXPECT_TRUE(IsNull(tests[2]->value_param()));
296  EXPECT_TRUE(IsNull(tests[2]->type_param()));
297  EXPECT_TRUE(tests[2]->should_run());
298  EXPECT_TRUE(tests[2]->result()->Passed());
299  EXPECT_EQ(0, tests[2]->result()->test_property_count());
300 
301  EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
302  EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
303  EXPECT_TRUE(IsNull(tests[3]->value_param()));
304  EXPECT_TRUE(IsNull(tests[3]->type_param()));
305  EXPECT_TRUE(tests[3]->should_run());
306  EXPECT_TRUE(tests[3]->result()->Passed());
307  EXPECT_EQ(1, tests[3]->result()->test_property_count());
308  const TestProperty& property = tests[3]->result()->GetTestProperty(0);
309  EXPECT_STREQ("key", property.key());
310  EXPECT_STREQ("value", property.value());
311 
312  delete[] tests;
313 
314 #if GTEST_HAS_TYPED_TEST
315  test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
316  tests = UnitTestHelper::GetSortedTests(test_suite);
317 
318  EXPECT_STREQ("Dummy", tests[0]->name());
319  EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
320  EXPECT_TRUE(IsNull(tests[0]->value_param()));
321  EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), tests[0]->type_param());
322  EXPECT_TRUE(tests[0]->should_run());
323  EXPECT_TRUE(tests[0]->result()->Passed());
324  EXPECT_EQ(0, tests[0]->result()->test_property_count());
325 
326  delete[] tests;
327 #endif // GTEST_HAS_TYPED_TEST
328  delete[] test_suites;
329  }
330 };
331 
332 } // namespace internal
333 } // namespace testing
334 
335 int main(int argc, char **argv) {
336  InitGoogleTest(&argc, argv);
337 
339 
340  return RUN_ALL_TESTS();
341 }
int failed_test_count() const
Definition: gtest.cc:5060
TYPED_TEST(CodeLocationForTYPEDTEST, Verify)
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1492
const TestInfo * GetTestInfo(int i) const
Definition: gtest.cc:2979
AssertionResult AssertionFailure()
Definition: gtest.cc:1200
int total_test_count() const
Definition: gtest.cc:2947
bool should_run() const
Definition: gtest.h:873
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:2068
const TestProperty & GetTestProperty(int i) const
Definition: gtest.cc:2227
const char * name() const
Definition: gtest.h:863
bool operator()(const T *a, const T *b)
internal::ProxyTypeList< Ts...> Types
const TestSuite * GetTestSuite(int i) const
Definition: gtest.cc:5104
std::string GetTypeName()
int successful_test_suite_count() const
Definition: gtest.cc:5013
#define T
Definition: Sacado_rad.hpp:553
static TestSuite const ** GetSortedTestSuites()
int test_to_run_count() const
Definition: gtest.cc:5081
#define ASSERT_TRUE(condition)
Definition: gtest.h:1985
AssertionResult AssertionSuccess()
Definition: gtest.cc:1195
int successful_test_count() const
Definition: gtest.cc:5050
int main()
Definition: ad_example.cpp:171
int total_test_suite_count() const
Definition: gtest.cc:5023
const char * type_param() const
Definition: gtest.h:867
int test_to_run_count() const
Definition: gtest.cc:2942
#define EXPECT_STREQ(s1, s2)
Definition: gtest.h:2107
bool Failed() const
Definition: gtest.cc:5100
bool Passed() const
Definition: gtest.cc:5096
int disabled_test_count() const
Definition: gtest.cc:5068
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:2038
static const TestSuite * FindTestSuite(const char *name)
int total_test_count() const
Definition: gtest.cc:5078
static TestInfo const ** GetSortedTests(const TestSuite *test_suite)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2484
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6527
TYPED_TEST_SUITE(CodeLocationForTYPEDTEST, int)
const TestResult * result() const
Definition: gtest.h:769
#define EXPECT_TRUE(condition)
Definition: gtest.h:1979
static UnitTest * GetInstance()
Definition: gtest.cc:4998
int failed_test_suite_count() const
Definition: gtest.cc:5018
#define EXPECT_FALSE(condition)
Definition: gtest.h:1982
int disabled_test_count() const
Definition: gtest.cc:2932
int test_suite_to_run_count() const
Definition: gtest.cc:5029
AssertionResult IsNull(const char *str)
TEST(IsXDigitTest, WorksForNarrowAscii)