1 // Copyright 2019 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/simple_printing_event_handler.h"
16
17 #include <cstdarg>
18 #include <cstdio>
19 #include <string_view>
20
21 namespace pw::unit_test {
22
RunAllTestsStart()23 void SimplePrintingEventHandler::RunAllTestsStart() {
24 WriteLine("[==========] Running all tests.");
25 }
26
RunAllTestsEnd(const RunTestsSummary & run_tests_summary)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);
33 }
34 }
35
TestCaseStart(const TestCase & test_case)36 void SimplePrintingEventHandler::TestCaseStart(const TestCase& test_case) {
37 WriteLine("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
38 }
39
TestCaseEnd(const TestCase & test_case,TestResult result)40 void SimplePrintingEventHandler::TestCaseEnd(const TestCase& test_case,
41 TestResult result) {
42 // Use a switch with no default to detect changes in the test result enum.
43 switch (result) {
44 case TestResult::kSuccess:
45 WriteLine(
46 "[ OK ] %s.%s", test_case.suite_name, test_case.test_name);
47 break;
48 case TestResult::kFailure:
49 WriteLine(
50 "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name);
51 break;
52 }
53 }
54
TestCaseExpect(const TestCase & test_case,const TestExpectation & expectation)55 void SimplePrintingEventHandler::TestCaseExpect(
56 const TestCase& test_case, const TestExpectation& expectation) {
57 if (!verbose_ && expectation.success) {
58 return;
59 }
60
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);
64
65 write_(" Actual: ", false);
66 write_(expectation.evaluated_expression, true);
67 }
68
WriteLine(const char * format,...)69 void SimplePrintingEventHandler::WriteLine(const char* format, ...) {
70 va_list args;
71
72 va_start(args, format);
73 std::vsnprintf(buffer_, sizeof(buffer_), format, args);
74 va_end(args);
75
76 write_(buffer_, true);
77 }
78
TestCaseDisabled(const TestCase & test)79 void SimplePrintingEventHandler::TestCaseDisabled(const TestCase& test) {
80 if (verbose_) {
81 WriteLine("Skipping disabled test %s.%s", test.suite_name, test.test_name);
82 }
83 }
84
85 } // namespace pw::unit_test
86