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 |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 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 |
Wyatt Hepler | 1a96094 | 2019-11-26 14:13:38 -0800 | [diff] [blame] | 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 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() { |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 24 | WriteLine("[==========] 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) { |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 29 | WriteLine("[==========] Done running all tests."); |
| 30 | WriteLine("[ 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) { |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 32 | WriteLine("[ 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) { |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 37 | WriteLine("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void SimplePrintingEventHandler::TestCaseEnd(const TestCase& test_case, |
| 41 | TestResult result) { |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 42 | // Use a switch with no default to detect changes in the test result enum. |
| 43 | switch (result) { |
| 44 | case TestResult::kSuccess: |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 45 | WriteLine( |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame] | 46 | "[ OK ] %s.%s", test_case.suite_name, test_case.test_name); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 47 | break; |
| 48 | case TestResult::kFailure: |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 49 | WriteLine( |
Armando Montanez | d3ed0f4 | 2019-11-18 11:15:42 -0800 | [diff] [blame] | 50 | "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name); |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 51 | break; |
| 52 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void SimplePrintingEventHandler::TestCaseExpect( |
| 56 | const TestCase& test_case, const TestExpectation& expectation) { |
| 57 | if (!verbose_ && expectation.success) { |
| 58 | return; |
| 59 | } |
| 60 | |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 61 | const char* result = expectation.success ? "Success" : "Failure"; |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 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); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 69 | void SimplePrintingEventHandler::WriteLine(const char* format, ...) { |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 70 | va_list args; |
| 71 | |
| 72 | va_start(args, format); |
| 73 | std::vsnprintf(buffer_, sizeof(buffer_), format, args); |
| 74 | va_end(args); |
| 75 | |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 76 | write_(buffer_, true); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Wyatt Hepler | 1c0e65e | 2020-01-27 13:10:18 -0800 | [diff] [blame^] | 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 | |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 85 | } // namespace pw::unit_test |