blob: e1ad0803e9a919c5dedb63232038fb1660c3ce07 [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);
34 if (run_tests_summary.failed_tests) {
35 PW_LOG_ERROR("[ FAILED ] %d test(s).", run_tests_summary.failed_tests);
36 }
37}
38
39void LoggingEventHandler::TestCaseStart(const TestCase& test_case) {
40 PW_LOG_INFO("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
41}
42
43void 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
58void 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,
Keir Mierlec4725d72020-06-04 12:17:38 -070067 PW_LOG_DEFAULT_FLAGS,
Keir Mierleda0bccb2020-01-17 13:51:35 -080068 "%s:%d: %s",
69 test_case.file_name,
70 expectation.line_number,
71 result);
Keir Mierleda0bccb2020-01-17 13:51:35 -080072 PW_LOG(level,
Keir Mierlec4725d72020-06-04 12:17:38 -070073 PW_LOG_DEFAULT_FLAGS,
74 " Expected: %s",
75 expectation.expression);
76 PW_LOG(level,
77 PW_LOG_DEFAULT_FLAGS,
Keir Mierleda0bccb2020-01-17 13:51:35 -080078 " Actual: %s",
79 expectation.evaluated_expression);
80}
81
Wyatt Hepler1c0e65e2020-01-27 13:10:18 -080082void LoggingEventHandler::TestCaseDisabled(const TestCase& test) {
83 PW_LOG_DEBUG("Skipping disabled test %s.%s", test.suite_name, test.test_name);
84}
85
Keir Mierleda0bccb2020-01-17 13:51:35 -080086} // namespace pw::unit_test