blob: ab5cd74d2cafcf745ee286cfaa7633584f284997 [file] [log] [blame]
Alexei Frolovc10c8122019-11-01 16:31:19 -07001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Alexei Frolovc10c8122019-11-01 16:31:19 -07006//
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
Wyatt Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Alexei Frolovc10c8122019-11-01 16:31:19 -070014
15#include "pw_unit_test/simple_printing_event_handler.h"
16
17#include <cstdarg>
18#include <cstdio>
Armando Montanezd3ed0f42019-11-18 11:15:42 -080019#include <string_view>
Alexei Frolovc10c8122019-11-01 16:31:19 -070020
21namespace pw::unit_test {
22
Keir Mierle243e32a2019-11-05 10:29:26 -080023void SimplePrintingEventHandler::RunAllTestsStart() {
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080024 WriteLine("[==========] Running all tests.");
Keir Mierle243e32a2019-11-05 10:29:26 -080025}
26
27void SimplePrintingEventHandler::RunAllTestsEnd(
28 const RunTestsSummary& run_tests_summary) {
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080029 WriteLine("[==========] Done running all tests.");
30 WriteLine("[ PASSED ] %d test(s).", run_tests_summary.passed_tests);
Keir Mierle243e32a2019-11-05 10:29:26 -080031 if (run_tests_summary.failed_tests) {
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080032 WriteLine("[ FAILED ] %d test(s).", run_tests_summary.failed_tests);
Keir Mierle243e32a2019-11-05 10:29:26 -080033 }
34}
35
Alexei Frolovc10c8122019-11-01 16:31:19 -070036void SimplePrintingEventHandler::TestCaseStart(const TestCase& test_case) {
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080037 WriteLine("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
Alexei Frolovc10c8122019-11-01 16:31:19 -070038}
39
40void SimplePrintingEventHandler::TestCaseEnd(const TestCase& test_case,
41 TestResult result) {
Keir Mierle243e32a2019-11-05 10:29:26 -080042 // Use a switch with no default to detect changes in the test result enum.
43 switch (result) {
44 case TestResult::kSuccess:
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080045 WriteLine(
Armando Montanezd3ed0f42019-11-18 11:15:42 -080046 "[ OK ] %s.%s", test_case.suite_name, test_case.test_name);
Keir Mierle243e32a2019-11-05 10:29:26 -080047 break;
48 case TestResult::kFailure:
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080049 WriteLine(
Armando Montanezd3ed0f42019-11-18 11:15:42 -080050 "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name);
Keir Mierle243e32a2019-11-05 10:29:26 -080051 break;
52 }
Alexei Frolovc10c8122019-11-01 16:31:19 -070053}
54
55void SimplePrintingEventHandler::TestCaseExpect(
56 const TestCase& test_case, const TestExpectation& expectation) {
57 if (!verbose_ && expectation.success) {
58 return;
59 }
60
Keir Mierle243e32a2019-11-05 10:29:26 -080061 const char* result = expectation.success ? "Success" : "Failure";
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080062 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);
Alexei Frolovc10c8122019-11-01 16:31:19 -070067}
68
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080069void SimplePrintingEventHandler::WriteLine(const char* format, ...) {
Alexei Frolovc10c8122019-11-01 16:31:19 -070070 va_list args;
71
72 va_start(args, format);
73 std::vsnprintf(buffer_, sizeof(buffer_), format, args);
74 va_end(args);
75
Wyatt Hepler8663e9c2019-11-20 13:58:29 -080076 write_(buffer_, true);
Alexei Frolovc10c8122019-11-01 16:31:19 -070077}
78
Wyatt Hepler1c0e65e2020-01-27 13:10:18 -080079void SimplePrintingEventHandler::TestCaseDisabled(const TestCase& test) {
80 if (verbose_) {
81 WriteLine("Skipping disabled test %s.%s", test.suite_name, test.test_name);
82 }
83}
84
Alexei Frolovc10c8122019-11-01 16:31:19 -070085} // namespace pw::unit_test