googletest/googletest/test/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 
52 class UnitTestHelper {
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 template <typename T> class TestSuiteWithCommentTest : public Test {};
101 
102 const int kTypedTestSuites = 1;
103 const int kTypedTests = 1;
104 
105 // We can only test the accessors that do not change value while tests run.
106 // Since tests can be run in any order, the values the accessors that track
107 // test execution (such as failed_test_count) can not be predicted.
108 TEST(ApiTest, UnitTestImmutableAccessorsWork) {
109  UnitTest* unit_test = UnitTest::GetInstance();
110 
113  EXPECT_EQ(2, unit_test->disabled_test_count());
114  EXPECT_EQ(5 + kTypedTests, unit_test->total_test_count());
115  EXPECT_EQ(3 + kTypedTests, unit_test->test_to_run_count());
116 
117  const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
118 
119  EXPECT_STREQ("ApiTest", test_suites[0]->name());
120  EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
121  EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
122 
123  delete[] test_suites;
124 
125  // The following lines initiate actions to verify certain methods in
126  // FinalSuccessChecker::TearDown.
127 
128  // Records a test property to verify TestResult::GetTestProperty().
129  RecordProperty("key", "value");
130 }
131 
132 AssertionResult IsNull(const char* str) {
133  if (str != nullptr) {
134  return testing::AssertionFailure() << "argument is " << str;
135  }
136  return AssertionSuccess();
137 }
138 
139 TEST(ApiTest, TestSuiteImmutableAccessorsWork) {
141  ASSERT_TRUE(test_suite != nullptr);
142 
143  EXPECT_STREQ("ApiTest", test_suite->name());
144  EXPECT_TRUE(IsNull(test_suite->type_param()));
145  EXPECT_TRUE(test_suite->should_run());
146  EXPECT_EQ(1, test_suite->disabled_test_count());
147  EXPECT_EQ(3, test_suite->test_to_run_count());
148  ASSERT_EQ(4, test_suite->total_test_count());
149 
150  const TestInfo** tests = UnitTestHelper::GetSortedTests(test_suite);
151 
152  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
153  EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
154  EXPECT_TRUE(IsNull(tests[0]->value_param()));
155  EXPECT_TRUE(IsNull(tests[0]->type_param()));
156  EXPECT_FALSE(tests[0]->should_run());
157 
158  EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
159  EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
160  EXPECT_TRUE(IsNull(tests[1]->value_param()));
161  EXPECT_TRUE(IsNull(tests[1]->type_param()));
162  EXPECT_TRUE(tests[1]->should_run());
163 
164  EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
165  EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
166  EXPECT_TRUE(IsNull(tests[2]->value_param()));
167  EXPECT_TRUE(IsNull(tests[2]->type_param()));
168  EXPECT_TRUE(tests[2]->should_run());
169 
170  EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
171  EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
172  EXPECT_TRUE(IsNull(tests[3]->value_param()));
173  EXPECT_TRUE(IsNull(tests[3]->type_param()));
174  EXPECT_TRUE(tests[3]->should_run());
175 
176  delete[] tests;
177  tests = nullptr;
178 
179  test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
180  ASSERT_TRUE(test_suite != nullptr);
181 
182  EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suite->name());
183  EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), test_suite->type_param());
184  EXPECT_TRUE(test_suite->should_run());
185  EXPECT_EQ(0, test_suite->disabled_test_count());
186  EXPECT_EQ(1, test_suite->test_to_run_count());
187  ASSERT_EQ(1, test_suite->total_test_count());
188 
190 
191  EXPECT_STREQ("Dummy", tests[0]->name());
192  EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
193  EXPECT_TRUE(IsNull(tests[0]->value_param()));
194  EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), tests[0]->type_param());
195  EXPECT_TRUE(tests[0]->should_run());
196 
197  delete[] tests;
198 }
199 
200 TEST(ApiTest, TestSuiteDisabledAccessorsWork) {
201  const TestSuite* test_suite = UnitTestHelper::FindTestSuite("DISABLED_Test");
202  ASSERT_TRUE(test_suite != nullptr);
203 
204  EXPECT_STREQ("DISABLED_Test", test_suite->name());
205  EXPECT_TRUE(IsNull(test_suite->type_param()));
206  EXPECT_FALSE(test_suite->should_run());
207  EXPECT_EQ(1, test_suite->disabled_test_count());
208  EXPECT_EQ(0, test_suite->test_to_run_count());
209  ASSERT_EQ(1, test_suite->total_test_count());
210 
211  const TestInfo* const test_info = test_suite->GetTestInfo(0);
212  EXPECT_STREQ("Dummy2", test_info->name());
213  EXPECT_STREQ("DISABLED_Test", test_info->test_suite_name());
214  EXPECT_TRUE(IsNull(test_info->value_param()));
215  EXPECT_TRUE(IsNull(test_info->type_param()));
216  EXPECT_FALSE(test_info->should_run());
217 }
218 
219 // These two tests are here to provide support for testing
220 // test_suite_to_run_count, disabled_test_count, and test_to_run_count.
221 TEST(ApiTest, DISABLED_Dummy1) {}
222 TEST(DISABLED_Test, Dummy2) {}
223 
224 class FinalSuccessChecker : public Environment {
225  protected:
226  void TearDown() override {
227  UnitTest* unit_test = UnitTest::GetInstance();
228 
230  EXPECT_EQ(3 + kTypedTests, unit_test->successful_test_count());
231  EXPECT_EQ(0, unit_test->failed_test_suite_count());
232  EXPECT_EQ(0, unit_test->failed_test_count());
233  EXPECT_TRUE(unit_test->Passed());
234  EXPECT_FALSE(unit_test->Failed());
236 
237  const TestSuite** const test_suites = UnitTestHelper::GetSortedTestSuites();
238 
239  EXPECT_STREQ("ApiTest", test_suites[0]->name());
240  EXPECT_TRUE(IsNull(test_suites[0]->type_param()));
241  EXPECT_TRUE(test_suites[0]->should_run());
242  EXPECT_EQ(1, test_suites[0]->disabled_test_count());
243  ASSERT_EQ(4, test_suites[0]->total_test_count());
244  EXPECT_EQ(3, test_suites[0]->successful_test_count());
245  EXPECT_EQ(0, test_suites[0]->failed_test_count());
246  EXPECT_TRUE(test_suites[0]->Passed());
247  EXPECT_FALSE(test_suites[0]->Failed());
248 
249  EXPECT_STREQ("DISABLED_Test", test_suites[1]->name());
250  EXPECT_TRUE(IsNull(test_suites[1]->type_param()));
251  EXPECT_FALSE(test_suites[1]->should_run());
252  EXPECT_EQ(1, test_suites[1]->disabled_test_count());
253  ASSERT_EQ(1, test_suites[1]->total_test_count());
254  EXPECT_EQ(0, test_suites[1]->successful_test_count());
255  EXPECT_EQ(0, test_suites[1]->failed_test_count());
256 
257  EXPECT_STREQ("TestSuiteWithCommentTest/0", test_suites[2]->name());
259  test_suites[2]->type_param());
260  EXPECT_TRUE(test_suites[2]->should_run());
261  EXPECT_EQ(0, test_suites[2]->disabled_test_count());
262  ASSERT_EQ(1, test_suites[2]->total_test_count());
263  EXPECT_EQ(1, test_suites[2]->successful_test_count());
264  EXPECT_EQ(0, test_suites[2]->failed_test_count());
265  EXPECT_TRUE(test_suites[2]->Passed());
266  EXPECT_FALSE(test_suites[2]->Failed());
267 
270  EXPECT_STREQ("DISABLED_Dummy1", tests[0]->name());
271  EXPECT_STREQ("ApiTest", tests[0]->test_suite_name());
272  EXPECT_FALSE(tests[0]->should_run());
273 
274  EXPECT_STREQ("TestSuiteDisabledAccessorsWork", tests[1]->name());
275  EXPECT_STREQ("ApiTest", tests[1]->test_suite_name());
276  EXPECT_TRUE(IsNull(tests[1]->value_param()));
277  EXPECT_TRUE(IsNull(tests[1]->type_param()));
278  EXPECT_TRUE(tests[1]->should_run());
279  EXPECT_TRUE(tests[1]->result()->Passed());
280  EXPECT_EQ(0, tests[1]->result()->test_property_count());
281 
282  EXPECT_STREQ("TestSuiteImmutableAccessorsWork", tests[2]->name());
283  EXPECT_STREQ("ApiTest", tests[2]->test_suite_name());
284  EXPECT_TRUE(IsNull(tests[2]->value_param()));
285  EXPECT_TRUE(IsNull(tests[2]->type_param()));
286  EXPECT_TRUE(tests[2]->should_run());
287  EXPECT_TRUE(tests[2]->result()->Passed());
288  EXPECT_EQ(0, tests[2]->result()->test_property_count());
289 
290  EXPECT_STREQ("UnitTestImmutableAccessorsWork", tests[3]->name());
291  EXPECT_STREQ("ApiTest", tests[3]->test_suite_name());
292  EXPECT_TRUE(IsNull(tests[3]->value_param()));
293  EXPECT_TRUE(IsNull(tests[3]->type_param()));
294  EXPECT_TRUE(tests[3]->should_run());
295  EXPECT_TRUE(tests[3]->result()->Passed());
296  EXPECT_EQ(1, tests[3]->result()->test_property_count());
297  const TestProperty& property = tests[3]->result()->GetTestProperty(0);
298  EXPECT_STREQ("key", property.key());
299  EXPECT_STREQ("value", property.value());
300 
301  delete[] tests;
302 
303  test_suite = UnitTestHelper::FindTestSuite("TestSuiteWithCommentTest/0");
305 
306  EXPECT_STREQ("Dummy", tests[0]->name());
307  EXPECT_STREQ("TestSuiteWithCommentTest/0", tests[0]->test_suite_name());
308  EXPECT_TRUE(IsNull(tests[0]->value_param()));
309  EXPECT_STREQ(GetTypeName<Types<int>>().c_str(), tests[0]->type_param());
310  EXPECT_TRUE(tests[0]->should_run());
311  EXPECT_TRUE(tests[0]->result()->Passed());
312  EXPECT_EQ(0, tests[0]->result()->test_property_count());
313 
314  delete[] tests;
315  delete[] test_suites;
316  }
317 };
318 
319 } // namespace internal
320 } // namespace testing
321 
322 int main(int argc, char **argv) {
323  InitGoogleTest(&argc, argv);
324 
326 
327  return RUN_ALL_TESTS();
328 }
testing::UnitTest::failed_test_count
int failed_test_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4678
xds_interop_client.str
str
Definition: xds_interop_client.py:487
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
testing
Definition: aws_request_signer_test.cc:25
testing::AssertionFailure
AssertionResult AssertionFailure()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:1028
testing::internal::TYPED_TEST_SUITE
TYPED_TEST_SUITE(TestSuiteWithCommentTest, Types< int >)
testing::internal::UnitTestHelper::FindTestSuite
static const TestSuite * FindTestSuite(const char *name)
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:72
testing::TestInfo
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:695
testing::UnitTest::total_test_suite_count
int total_test_suite_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4641
testing::UnitTest::Passed
bool Passed() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4713
testing::internal::IsNull
AssertionResult IsNull(const char *str)
Definition: bloaty/third_party/googletest/googletest/test/gtest-unittest-api_test.cc:139
testing::UnitTest::test_to_run_count
int test_to_run_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4699
testing::internal::FinalSuccessChecker
Definition: bloaty/third_party/googletest/googletest/test/gtest-unittest-api_test.cc:233
string.h
testing::internal::TYPED_TEST
TYPED_TEST(TestSuiteWithCommentTest, Dummy)
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:100
testing::internal::LessByName
Definition: bloaty/third_party/googletest/googletest/test/gtest-unittest-api_test.cc:46
setup.name
name
Definition: setup.py:542
testing::internal::LessByName::operator()
bool operator()(const T *a, const T *b)
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:47
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
testing::internal::GetTypeName
std::string GetTypeName()
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-type-util.h:80
testing::UnitTest::successful_test_suite_count
int successful_test_suite_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4631
testing::UnitTest::failed_test_suite_count
int failed_test_suite_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4636
T
#define T(upbtypeconst, upbtype, ctype, default_value)
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
testing::AddGlobalTestEnvironment
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1474
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
testing::internal::FinalSuccessChecker::TearDown
void TearDown() override
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:226
testing::internal::Types
Definition: googletest/googletest/include/gtest/internal/gtest-type-util.h:141
testing::internal::TestSuiteWithCommentTest
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:98
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
testing::gmock_generated_actions_test::Dummy
int Dummy(bool flag)
Definition: bloaty/third_party/googletest/googlemock/test/gmock-generated-actions_test.cc:492
testing::internal::kTypedTests
const int kTypedTests
Definition: bloaty/third_party/googletest/googletest/test/gtest-unittest-api_test.cc:107
testing::UnitTest::test_suite_to_run_count
int test_suite_to_run_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4647
testing::TestProperty
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:525
testing::AssertionSuccess
AssertionResult AssertionSuccess()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:1023
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
TestSuite
Definition: cavp_main.cc:32
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
tests
static BloatyTestEntry tests[]
Definition: bloaty_test_pe.cc:112
testing::UnitTest::total_test_count
int total_test_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4696
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
testing::UnitTest::GetTestSuite
const TestSuite * GetTestSuite(int i) const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4721
tests
Definition: src/python/grpcio_tests/tests/__init__.py:1
testing::internal::kTypedTestSuites
const int kTypedTestSuites
Definition: bloaty/third_party/googletest/googletest/test/gtest-unittest-api_test.cc:106
testing::UnitTest
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1257
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
testing::UnitTest::GetInstance
static UnitTest * GetInstance()
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4616
testing::UnitTest::successful_test_count
int successful_test_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4668
main
int main(int argc, char **argv)
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:322
testing::internal::TEST
TEST(IsXDigitTest, WorksForNarrowAscii)
Definition: bloaty/third_party/googletest/googletest/test/googletest-port-test.cc:54
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
internal
Definition: benchmark/test/output_test_helper.cc:20
testing::UnitTest::disabled_test_count
int disabled_test_count() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4686
testing::internal::UnitTestHelper::GetSortedTests
static TestInfo const ** GetSortedTests(const TestSuite *test_suite)
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:85
testing::internal::UnitTestHelper::GetSortedTestSuites
static TestSuite const ** GetSortedTestSuites()
Definition: googletest/googletest/test/gtest-unittest-api_test.cc:56
testing::UnitTest::Failed
bool Failed() const
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:4717
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
setup.test_suite
test_suite
Definition: src/python/grpcio_tests/setup.py:108
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
testing::TestSuite
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:830


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:50