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