1 // Copyright 2019 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_unit_test/simple_printing_event_handler.h"
19 #include <string_view>
21 namespace pw::unit_test {
23 void SimplePrintingEventHandler::RunAllTestsStart() {
24 WriteLine("[==========] Running all tests.");
27 void SimplePrintingEventHandler::RunAllTestsEnd(
28 const RunTestsSummary& run_tests_summary) {
29 WriteLine("[==========] Done running all tests.");
30 WriteLine("[ PASSED ] %d test(s).", run_tests_summary.passed_tests);
31 if (run_tests_summary.failed_tests) {
32 WriteLine("[ FAILED ] %d test(s).", run_tests_summary.failed_tests);
36 void SimplePrintingEventHandler::TestCaseStart(const TestCase& test_case) {
37 WriteLine("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
40 void SimplePrintingEventHandler::TestCaseEnd(const TestCase& test_case,
42 // Use a switch with no default to detect changes in the test result enum.
44 case TestResult::kSuccess:
46 "[ OK ] %s.%s", test_case.suite_name, test_case.test_name);
48 case TestResult::kFailure:
50 "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name);
55 void SimplePrintingEventHandler::TestCaseExpect(
56 const TestCase& test_case, const TestExpectation& expectation) {
57 if (!verbose_ && expectation.success) {
61 const char* result = expectation.success ? "Success" : "Failure";
62 WriteLine("%s:%d: %s", test_case.file_name, expectation.line_number, result);
63 WriteLine(" Expected: %s", expectation.expression);
65 write_(" Actual: ", false);
66 write_(expectation.evaluated_expression, true);
69 void SimplePrintingEventHandler::WriteLine(const char* format, ...) {
72 va_start(args, format);
73 std::vsnprintf(buffer_, sizeof(buffer_), format, args);
76 write_(buffer_, true);
79 void SimplePrintingEventHandler::TestCaseDisabled(const TestCase& test) {
81 WriteLine("Skipping disabled test %s.%s", test.suite_name, test.test_name);
85 } // namespace pw::unit_test