1 // Copyright 2020 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 <cstdarg>
18 //#include <cstdio>
19 //#include <string_view>
20 #include <cstdint>
21
22 #include "pw_log/log.h"
23
24 namespace pw::unit_test {
25
RunAllTestsStart()26 void LoggingEventHandler::RunAllTestsStart() {
27 PW_LOG_INFO("[==========] Running all tests.");
28 }
29
RunAllTestsEnd(const RunTestsSummary & run_tests_summary)30 void LoggingEventHandler::RunAllTestsEnd(
31 const RunTestsSummary& run_tests_summary) {
32 PW_LOG_INFO("[==========] Done running all tests.");
33 PW_LOG_INFO("[ PASSED ] %d test(s).", run_tests_summary.passed_tests);
34 if (run_tests_summary.failed_tests) {
35 PW_LOG_ERROR("[ FAILED ] %d test(s).", run_tests_summary.failed_tests);
36 }
37 }
38
TestCaseStart(const TestCase & test_case)39 void LoggingEventHandler::TestCaseStart(const TestCase& test_case) {
40 PW_LOG_INFO("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
41 }
42
TestCaseEnd(const TestCase & test_case,TestResult result)43 void LoggingEventHandler::TestCaseEnd(const TestCase& test_case,
44 TestResult result) {
45 // Use a switch with no default to detect changes in the test result enum.
46 switch (result) {
47 case TestResult::kSuccess:
48 PW_LOG_INFO(
49 "[ OK ] %s.%s", test_case.suite_name, test_case.test_name);
50 break;
51 case TestResult::kFailure:
52 PW_LOG_ERROR(
53 "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name);
54 break;
55 }
56 }
57
TestCaseExpect(const TestCase & test_case,const TestExpectation & expectation)58 void LoggingEventHandler::TestCaseExpect(const TestCase& test_case,
59 const TestExpectation& expectation) {
60 if (!verbose_ && expectation.success) {
61 return;
62 }
63
64 const char* result = expectation.success ? "Success" : "Failure";
65 uint32_t level = expectation.success ? PW_LOG_LEVEL_INFO : PW_LOG_LEVEL_ERROR;
66 PW_LOG(level,
67 PW_LOG_DEFAULT_FLAGS,
68 "%s:%d: %s",
69 test_case.file_name,
70 expectation.line_number,
71 result);
72 PW_LOG(level,
73 PW_LOG_DEFAULT_FLAGS,
74 " Expected: %s",
75 expectation.expression);
76 PW_LOG(level,
77 PW_LOG_DEFAULT_FLAGS,
78 " Actual: %s",
79 expectation.evaluated_expression);
80 }
81
TestCaseDisabled(const TestCase & test)82 void LoggingEventHandler::TestCaseDisabled(const TestCase& test) {
83 PW_LOG_DEBUG("Skipping disabled test %s.%s", test.suite_name, test.test_name);
84 }
85
86 } // namespace pw::unit_test
87