blob: def635454478747a062aa27beb9d0241845973b7 [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
4// use this file except in compliance with the License. You may obtain a copy
5// of 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
13// under the License.
14
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() {
Armando Montanezd3ed0f42019-11-18 11:15:42 -080024 WriteAndFlush("[==========] Running all tests.");
Keir Mierle243e32a2019-11-05 10:29:26 -080025}
26
27void SimplePrintingEventHandler::RunAllTestsEnd(
28 const RunTestsSummary& run_tests_summary) {
Armando Montanezd3ed0f42019-11-18 11:15:42 -080029 WriteAndFlush("[==========] Done running all tests.");
30 WriteAndFlush("[ PASSED ] %d test(s).", run_tests_summary.passed_tests);
Keir Mierle243e32a2019-11-05 10:29:26 -080031 if (run_tests_summary.failed_tests) {
Armando Montanezd3ed0f42019-11-18 11:15:42 -080032 WriteAndFlush("[ 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) {
37 WriteAndFlush(
Armando Montanezd3ed0f42019-11-18 11:15:42 -080038 "[ RUN ] %s.%s", test_case.suite_name, test_case.test_name);
Alexei Frolovc10c8122019-11-01 16:31:19 -070039}
40
41void SimplePrintingEventHandler::TestCaseEnd(const TestCase& test_case,
42 TestResult result) {
Keir Mierle243e32a2019-11-05 10:29:26 -080043 // Use a switch with no default to detect changes in the test result enum.
44 switch (result) {
45 case TestResult::kSuccess:
46 WriteAndFlush(
Armando Montanezd3ed0f42019-11-18 11:15:42 -080047 "[ OK ] %s.%s", test_case.suite_name, test_case.test_name);
Keir Mierle243e32a2019-11-05 10:29:26 -080048 break;
49 case TestResult::kFailure:
50 WriteAndFlush(
Armando Montanezd3ed0f42019-11-18 11:15:42 -080051 "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name);
Keir Mierle243e32a2019-11-05 10:29:26 -080052 break;
53 }
Alexei Frolovc10c8122019-11-01 16:31:19 -070054}
55
56void SimplePrintingEventHandler::TestCaseExpect(
57 const TestCase& test_case, const TestExpectation& expectation) {
58 if (!verbose_ && expectation.success) {
59 return;
60 }
61
Keir Mierle243e32a2019-11-05 10:29:26 -080062 const char* result = expectation.success ? "Success" : "Failure";
63 WriteAndFlush(
Armando Montanezd3ed0f42019-11-18 11:15:42 -080064 "%s:%d: %s", test_case.file_name, expectation.line_number, result);
65 WriteAndFlush(" Expected: %s", expectation.expression);
Alexei Frolovc10c8122019-11-01 16:31:19 -070066}
67
68int SimplePrintingEventHandler::WriteAndFlush(const char* format, ...) {
69 va_list args;
70
71 va_start(args, format);
72 std::vsnprintf(buffer_, sizeof(buffer_), format, args);
73 va_end(args);
74
75 return write_(buffer_);
76}
77
78} // namespace pw::unit_test