Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 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 |
| 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 Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 19 | #include <string_view> |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 20 | |
| 21 | namespace pw::unit_test { |
| 22 | |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 23 | void SimplePrintingEventHandler::RunAllTestsStart() { |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 24 | WriteAndFlush("[==========] Running all tests."); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void SimplePrintingEventHandler::RunAllTestsEnd( |
| 28 | const RunTestsSummary& run_tests_summary) { |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 29 | WriteAndFlush("[==========] Done running all tests."); |
| 30 | WriteAndFlush("[ PASSED ] %d test(s).", run_tests_summary.passed_tests); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 31 | if (run_tests_summary.failed_tests) { |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 32 | WriteAndFlush("[ FAILED ] %d test(s).", run_tests_summary.failed_tests); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 36 | void SimplePrintingEventHandler::TestCaseStart(const TestCase& test_case) { |
| 37 | WriteAndFlush( |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 38 | "[ RUN ] %s.%s", test_case.suite_name, test_case.test_name); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void SimplePrintingEventHandler::TestCaseEnd(const TestCase& test_case, |
| 42 | TestResult result) { |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 43 | // Use a switch with no default to detect changes in the test result enum. |
| 44 | switch (result) { |
| 45 | case TestResult::kSuccess: |
| 46 | WriteAndFlush( |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 47 | "[ OK ] %s.%s", test_case.suite_name, test_case.test_name); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 48 | break; |
| 49 | case TestResult::kFailure: |
| 50 | WriteAndFlush( |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 51 | "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 52 | break; |
| 53 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SimplePrintingEventHandler::TestCaseExpect( |
| 57 | const TestCase& test_case, const TestExpectation& expectation) { |
| 58 | if (!verbose_ && expectation.success) { |
| 59 | return; |
| 60 | } |
| 61 | |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 62 | const char* result = expectation.success ? "Success" : "Failure"; |
| 63 | WriteAndFlush( |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame^] | 64 | "%s:%d: %s", test_case.file_name, expectation.line_number, result); |
| 65 | WriteAndFlush(" Expected: %s", expectation.expression); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int 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 |