Keir Mierle | da0bccb | 2020-01-17 13:51:35 -0800 | [diff] [blame] | 1 | // Copyright 2020 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/logging_event_handler.h" |
| 16 | |
| 17 | //#include <cstdarg> |
| 18 | //#include <cstdio> |
| 19 | //#include <string_view> |
| 20 | #include <cstdint> |
| 21 | |
| 22 | #include "pw_log/log.h" |
| 23 | |
| 24 | namespace pw::unit_test { |
| 25 | |
| 26 | void LoggingEventHandler::RunAllTestsStart() { |
| 27 | PW_LOG_INFO("[==========] Running all tests."); |
| 28 | } |
| 29 | |
| 30 | void LoggingEventHandler::RunAllTestsEnd( |
| 31 | const RunTestsSummary& run_tests_summary) { |
| 32 | PW_LOG_INFO("[==========] Done running all tests."); |
| 33 | PW_LOG_INFO("[ PASSED ] %d test(s).", run_tests_summary.passed_tests); |
| 34 | if (run_tests_summary.failed_tests) { |
| 35 | PW_LOG_ERROR("[ FAILED ] %d test(s).", run_tests_summary.failed_tests); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | void LoggingEventHandler::TestCaseStart(const TestCase& test_case) { |
| 40 | PW_LOG_INFO("[ RUN ] %s.%s", test_case.suite_name, test_case.test_name); |
| 41 | } |
| 42 | |
| 43 | void LoggingEventHandler::TestCaseEnd(const TestCase& test_case, |
| 44 | TestResult result) { |
| 45 | // Use a switch with no default to detect changes in the test result enum. |
| 46 | switch (result) { |
| 47 | case TestResult::kSuccess: |
| 48 | PW_LOG_INFO( |
| 49 | "[ OK ] %s.%s", test_case.suite_name, test_case.test_name); |
| 50 | break; |
| 51 | case TestResult::kFailure: |
| 52 | PW_LOG_ERROR( |
| 53 | "[ FAILED ] %s.%s", test_case.suite_name, test_case.test_name); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void LoggingEventHandler::TestCaseExpect(const TestCase& test_case, |
| 59 | const TestExpectation& expectation) { |
| 60 | if (!verbose_ && expectation.success) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | const char* result = expectation.success ? "Success" : "Failure"; |
| 65 | uint32_t level = expectation.success ? PW_LOG_LEVEL_INFO : PW_LOG_LEVEL_ERROR; |
| 66 | PW_LOG(level, |
| 67 | PW_LOG_NO_FLAGS, |
| 68 | "%s:%d: %s", |
| 69 | test_case.file_name, |
| 70 | expectation.line_number, |
| 71 | result); |
| 72 | PW_LOG(level, PW_LOG_NO_FLAGS, " Expected: %s", expectation.expression); |
| 73 | PW_LOG(level, |
| 74 | PW_LOG_NO_FLAGS, |
| 75 | " Actual: %s", |
| 76 | expectation.evaluated_expression); |
| 77 | } |
| 78 | |
Wyatt Hepler | 1c0e65e | 2020-01-27 13:10:18 -0800 | [diff] [blame^] | 79 | void LoggingEventHandler::TestCaseDisabled(const TestCase& test) { |
| 80 | PW_LOG_DEBUG("Skipping disabled test %s.%s", test.suite_name, test.test_name); |
| 81 | } |
| 82 | |
Keir Mierle | da0bccb | 2020-01-17 13:51:35 -0800 | [diff] [blame] | 83 | } // namespace pw::unit_test |