xref: /aosp_15_r20/external/pigweed/pw_unit_test/googletest_style_event_handler.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 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_style_event_handler.h"
16 
17 #include <cstdarg>
18 
19 namespace pw {
20 namespace unit_test {
21 
TestProgramStart(const ProgramSummary & program_summary)22 void GoogleTestStyleEventHandler::TestProgramStart(
23     const ProgramSummary& program_summary) {
24   WriteLine(PW_UNIT_TEST_GOOGLETEST_TEST_PROGRAM_START,
25             program_summary.tests_to_run,
26             program_summary.test_suites,
27             program_summary.test_suites > 1 ? "s" : "");
28 }
29 
EnvironmentsSetUpEnd()30 void GoogleTestStyleEventHandler::EnvironmentsSetUpEnd() {
31   WriteLine(PW_UNIT_TEST_GOOGLETEST_ENVIRONMENTS_SETUP_END);
32 }
33 
TestSuiteStart(const TestSuite & test_suite)34 void GoogleTestStyleEventHandler::TestSuiteStart(const TestSuite& test_suite) {
35   WriteLine(PW_UNIT_TEST_GOOGLETEST_TEST_SUITE_START,
36             test_suite.test_to_run_count,
37             test_suite.name);
38 }
39 
TestSuiteEnd(const TestSuite & test_suite)40 void GoogleTestStyleEventHandler::TestSuiteEnd(const TestSuite& test_suite) {
41   WriteLine(PW_UNIT_TEST_GOOGLETEST_TEST_SUITE_END,
42             test_suite.test_to_run_count,
43             test_suite.name);
44 }
45 
EnvironmentsTearDownEnd()46 void GoogleTestStyleEventHandler::EnvironmentsTearDownEnd() {
47   WriteLine(PW_UNIT_TEST_GOOGLETEST_ENVIRONMENTS_TEAR_DOWN_END);
48 }
49 
TestProgramEnd(const ProgramSummary & program_summary)50 void GoogleTestStyleEventHandler::TestProgramEnd(
51     const ProgramSummary& program_summary) {
52   WriteLine(PW_UNIT_TEST_GOOGLETEST_TEST_PROGRAM_END,
53             program_summary.tests_to_run -
54                 program_summary.tests_summary.skipped_tests -
55                 program_summary.tests_summary.disabled_tests,
56             program_summary.tests_to_run,
57             program_summary.test_suites,
58             program_summary.test_suites > 1 ? "s" : "");
59   WriteLine(PW_UNIT_TEST_GOOGLETEST_PASSED_SUMMARY,
60             program_summary.tests_summary.passed_tests);
61   if (program_summary.tests_summary.skipped_tests ||
62       program_summary.tests_summary.disabled_tests) {
63     WriteLine(PW_UNIT_TEST_GOOGLETEST_DISABLED_SUMMARY,
64               program_summary.tests_summary.skipped_tests +
65                   program_summary.tests_summary.disabled_tests);
66   }
67   if (program_summary.tests_summary.failed_tests) {
68     WriteLine(PW_UNIT_TEST_GOOGLETEST_FAILED_SUMMARY,
69               program_summary.tests_summary.failed_tests);
70   }
71 }
72 
RunAllTestsStart()73 void GoogleTestStyleEventHandler::RunAllTestsStart() {
74   WriteLine(PW_UNIT_TEST_GOOGLETEST_RUN_ALL_TESTS_START);
75 }
76 
RunAllTestsEnd(const RunTestsSummary & run_tests_summary)77 void GoogleTestStyleEventHandler::RunAllTestsEnd(
78     const RunTestsSummary& run_tests_summary) {
79   WriteLine(PW_UNIT_TEST_GOOGLETEST_RUN_ALL_TESTS_END);
80   WriteLine(PW_UNIT_TEST_GOOGLETEST_PASSED_SUMMARY,
81             run_tests_summary.passed_tests);
82   if (run_tests_summary.skipped_tests) {
83     WriteLine(PW_UNIT_TEST_GOOGLETEST_DISABLED_SUMMARY,
84               run_tests_summary.skipped_tests);
85   }
86   if (run_tests_summary.failed_tests) {
87     WriteLine(PW_UNIT_TEST_GOOGLETEST_FAILED_SUMMARY,
88               run_tests_summary.failed_tests);
89   }
90 }
91 
TestCaseStart(const TestCase & test_case)92 void GoogleTestStyleEventHandler::TestCaseStart(const TestCase& test_case) {
93   WriteLine(PW_UNIT_TEST_GOOGLETEST_CASE_START,
94             test_case.suite_name,
95             test_case.test_name);
96 }
97 
TestCaseEnd(const TestCase & test_case,TestResult result)98 void GoogleTestStyleEventHandler::TestCaseEnd(const TestCase& test_case,
99                                               TestResult result) {
100   // Use a switch with no default to detect changes in the test result enum.
101   switch (result) {
102     case TestResult::kSuccess:
103       WriteLine(PW_UNIT_TEST_GOOGLETEST_CASE_OK,
104                 test_case.suite_name,
105                 test_case.test_name);
106       break;
107     case TestResult::kFailure:
108       WriteLine(PW_UNIT_TEST_GOOGLETEST_CASE_FAILED,
109                 test_case.suite_name,
110                 test_case.test_name);
111       break;
112     case TestResult::kSkipped:
113       WriteLine(PW_UNIT_TEST_GOOGLETEST_CASE_DISABLED,
114                 test_case.suite_name,
115                 test_case.test_name);
116       break;
117   }
118 }
119 
TestCaseExpect(const TestCase & test_case,const TestExpectation & expectation)120 void GoogleTestStyleEventHandler::TestCaseExpect(
121     const TestCase& test_case, const TestExpectation& expectation) {
122   if (!verbose_ && expectation.success) {
123     return;
124   }
125 
126   const char* result = expectation.success ? "Success" : "Failure";
127   WriteLine("%s:%d: %s", test_case.file_name, expectation.line_number, result);
128   WriteLine("      Expected: %s", expectation.expression);
129 
130   Write("        Actual: ");
131   WriteLine("%s", expectation.evaluated_expression);
132 }
133 
TestCaseDisabled(const TestCase & test)134 void GoogleTestStyleEventHandler::TestCaseDisabled(const TestCase& test) {
135   if (verbose_) {
136     WriteLine("Skipping disabled test %s.%s", test.suite_name, test.test_name);
137   }
138 }
139 
140 }  // namespace unit_test
141 }  // namespace pw
142