• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_unit_test/googletest_handler_adapter.h"
16 
17 #include <cstdlib>
18 
19 namespace pw::unit_test {
20 
OnTestProgramStart(const testing::UnitTest & unit_test)21 void GoogleTestHandlerAdapter::OnTestProgramStart(
22     const testing::UnitTest& unit_test) {
23   handler_.TestProgramStart(
24       {unit_test.test_to_run_count(), unit_test.test_suite_to_run_count(), {}});
25 }
26 
OnEnvironmentsSetUpEnd(const::testing::UnitTest &)27 void GoogleTestHandlerAdapter::OnEnvironmentsSetUpEnd(
28     const ::testing::UnitTest&) {
29   handler_.EnvironmentsSetUpEnd();
30 }
31 
OnTestSuiteStart(const::testing::TestSuite & ts)32 void GoogleTestHandlerAdapter::OnTestSuiteStart(
33     const ::testing::TestSuite& ts) {
34   handler_.TestSuiteStart({ts.name(), ts.test_to_run_count()});
35 }
36 
OnTestStart(const::testing::TestInfo & ti)37 void GoogleTestHandlerAdapter::OnTestStart(const ::testing::TestInfo& ti) {
38   handler_.TestCaseStart({ti.test_suite_name(), ti.name(), ti.file()});
39 }
40 
OnTestPartResult(const::testing::TestPartResult & tpr)41 void GoogleTestHandlerAdapter::OnTestPartResult(
42     const ::testing::TestPartResult& tpr) {
43   handler_.TestCaseExpect(
44       {.suite_name = "", .test_name = "", .file_name = tpr.file_name()},
45       {.expression = "",
46        .evaluated_expression = tpr.summary(),
47        .line_number = tpr.line_number(),
48        .success = tpr.passed() || tpr.skipped()});
49 }
50 
OnTestEnd(const::testing::TestInfo & ti)51 void GoogleTestHandlerAdapter::OnTestEnd(const ::testing::TestInfo& ti) {
52   auto result = ti.result()->Passed()
53                     ? TestResult::kSuccess
54                     : (ti.result()->Failed() ? TestResult::kFailure
55                                              : TestResult::kSkipped);
56 
57   handler_.TestCaseEnd({ti.test_suite_name(), ti.name(), ti.file()}, result);
58 }
59 
OnTestSuiteEnd(const::testing::TestSuite & ts)60 void GoogleTestHandlerAdapter::OnTestSuiteEnd(const ::testing::TestSuite& ts) {
61   handler_.TestSuiteEnd({ts.name(), ts.test_to_run_count()});
62 }
63 
OnEnvironmentsTearDownEnd(const::testing::UnitTest &)64 void GoogleTestHandlerAdapter::OnEnvironmentsTearDownEnd(
65     const ::testing::UnitTest&) {
66   handler_.EnvironmentsTearDownEnd();
67 }
68 
OnTestProgramEnd(const::testing::UnitTest & unit_test)69 void GoogleTestHandlerAdapter::OnTestProgramEnd(
70     const ::testing::UnitTest& unit_test) {
71   handler_.TestProgramEnd({unit_test.test_to_run_count(),
72                            unit_test.test_suite_to_run_count(),
73                            {unit_test.successful_test_count(),
74                             unit_test.failed_test_count(),
75                             unit_test.skipped_test_count(),
76                             unit_test.disabled_test_count()}});
77 }
78 
79 }  // namespace pw::unit_test
80