blob: 7e80c321c3e6db6636c24249ce07d1931315724f [file] [log] [blame]
Keir Mierleda0bccb2020-01-17 13:51:35 -08001// 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
24namespace pw::unit_test {
25
26void LoggingEventHandler::RunAllTestsStart() {
27 PW_LOG_INFO("[==========] Running all tests.");
28}
29
30void 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);
Alex Deymo8b4bb782022-02-09 00:27:45 +010034 if (run_tests_summary.skipped_tests) {
35 PW_LOG_WARN("[ SKIPPED ] %d test(s).", run_tests_summary.skipped_tests);
36 }
Keir Mierleda0bccb2020-01-17 13:51:35 -080037 if (run_tests_summary.failed_tests) {
38 PW_LOG_ERROR("[ FAILED ] %d test(s).", run_tests_summary.failed_tests);
39 }
40}
41
42void LoggingEventHandler::TestCaseStart(const TestCase& test_case) {
43 PW_LOG_INFO("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
44}
45
46void LoggingEventHandler::TestCaseEnd(const TestCase& test_case,
47 TestResult result) {
48 // Use a switch with no default to detect changes in the test result enum.
49 switch (result) {
50 case TestResult::kSuccess:
51 PW_LOG_INFO(
52 "[ OK ] %s.%s", test_case.suite_name, test_case.test_name);
53 break;
54 case TestResult::kFailure:
55 PW_LOG_ERROR(
56 "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name);
57 break;
Alex Deymo8b4bb782022-02-09 00:27:45 +010058 case TestResult::kSkipped:
59 PW_LOG_WARN(
60 "[ SKIPPED ] %s.%s", test_case.suite_name, test_case.test_name);
61 break;
Keir Mierleda0bccb2020-01-17 13:51:35 -080062 }
63}
64
65void LoggingEventHandler::TestCaseExpect(const TestCase& test_case,
66 const TestExpectation& expectation) {
67 if (!verbose_ && expectation.success) {
68 return;
69 }
70
71 const char* result = expectation.success ? "Success" : "Failure";
72 uint32_t level = expectation.success ? PW_LOG_LEVEL_INFO : PW_LOG_LEVEL_ERROR;
73 PW_LOG(level,
Armando Montaneze221d842021-11-17 17:27:41 -080074 PW_LOG_FLAGS,
Keir Mierleda0bccb2020-01-17 13:51:35 -080075 "%s:%d: %s",
76 test_case.file_name,
77 expectation.line_number,
78 result);
Armando Montaneze221d842021-11-17 17:27:41 -080079 PW_LOG(level, PW_LOG_FLAGS, " Expected: %s", expectation.expression);
Keir Mierleda0bccb2020-01-17 13:51:35 -080080 PW_LOG(level,
Armando Montaneze221d842021-11-17 17:27:41 -080081 PW_LOG_FLAGS,
Keir Mierleda0bccb2020-01-17 13:51:35 -080082 " Actual: %s",
83 expectation.evaluated_expression);
84}
85
Wyatt Hepler1c0e65e2020-01-27 13:10:18 -080086void LoggingEventHandler::TestCaseDisabled(const TestCase& test) {
87 PW_LOG_DEBUG("Skipping disabled test %s.%s", test.suite_name, test.test_name);
88}
89
Keir Mierleda0bccb2020-01-17 13:51:35 -080090} // namespace pw::unit_test