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/framework.h" |
| 16 | |
Wyatt Hepler | 1c0e65e | 2020-01-27 13:10:18 -0800 | [diff] [blame] | 17 | #include <cstring> |
| 18 | |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 19 | namespace pw::unit_test { |
| 20 | |
| 21 | void RegisterEventHandler(EventHandler* event_handler) { |
| 22 | internal::Framework::Get().RegisterEventHandler(event_handler); |
| 23 | } |
| 24 | |
| 25 | namespace internal { |
| 26 | |
| 27 | // Singleton instance of the unit test framework class. |
| 28 | Framework Framework::framework_; |
| 29 | |
| 30 | // Linked list of all test cases in the test executable. This is static as it is |
| 31 | // populated using static initialization. |
| 32 | TestInfo* Framework::tests_ = nullptr; |
| 33 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 34 | void Framework::RegisterTest(TestInfo* new_test) { |
| 35 | // If the test list is empty, set new_test as the first test. |
| 36 | if (tests_ == nullptr) { |
| 37 | tests_ = new_test; |
| 38 | return; |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 39 | } |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 40 | |
| 41 | // Append the test case to the end of the test list. |
| 42 | TestInfo* info = tests_; |
| 43 | for (; info->next() != nullptr; info = info->next()) { |
| 44 | } |
| 45 | info->set_next(new_test); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | int Framework::RunAllTests() { |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 49 | run_tests_summary_.passed_tests = 0; |
| 50 | run_tests_summary_.failed_tests = 0; |
| 51 | |
| 52 | if (event_handler_ != nullptr) { |
| 53 | event_handler_->RunAllTestsStart(); |
| 54 | } |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 55 | for (const TestInfo* test = tests_; test != nullptr; test = test->next()) { |
| 56 | if (test->enabled()) { |
Wyatt Hepler | 1c0e65e | 2020-01-27 13:10:18 -0800 | [diff] [blame] | 57 | test->run(); |
| 58 | } else { |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 59 | event_handler_->TestCaseDisabled(test->test_case()); |
Wyatt Hepler | 1c0e65e | 2020-01-27 13:10:18 -0800 | [diff] [blame] | 60 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 61 | } |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 62 | if (event_handler_ != nullptr) { |
| 63 | event_handler_->RunAllTestsEnd(run_tests_summary_); |
| 64 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 65 | return exit_status_; |
| 66 | } |
| 67 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 68 | void Framework::StartTest(const TestInfo& test) { |
| 69 | current_test_ = &test; |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 70 | current_result_ = TestResult::kSuccess; |
| 71 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 72 | if (event_handler_ != nullptr) { |
| 73 | event_handler_->TestCaseStart(test.test_case()); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 74 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 77 | void Framework::EndCurrentTest() { |
Keir Mierle | 243e32a | 2019-11-05 10:29:26 -0800 | [diff] [blame] | 78 | switch (current_result_) { |
| 79 | case TestResult::kSuccess: |
| 80 | run_tests_summary_.passed_tests++; |
| 81 | break; |
| 82 | case TestResult::kFailure: |
| 83 | run_tests_summary_.failed_tests++; |
| 84 | break; |
| 85 | } |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 86 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 87 | if (event_handler_ != nullptr) { |
| 88 | event_handler_->TestCaseEnd(current_test_->test_case(), current_result_); |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 91 | current_test_ = nullptr; |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void Framework::ExpectationResult(const char* expression, |
Wyatt Hepler | a55d4c7 | 2020-01-16 10:26:04 -0800 | [diff] [blame] | 95 | const char* evaluated_expression, |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 96 | int line, |
| 97 | bool success) { |
| 98 | if (!success) { |
| 99 | current_result_ = TestResult::kFailure; |
| 100 | exit_status_ = 1; |
| 101 | } |
| 102 | |
| 103 | if (event_handler_ == nullptr) { |
| 104 | return; |
| 105 | } |
| 106 | |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 107 | TestExpectation expectation = { |
| 108 | .expression = expression, |
Wyatt Hepler | 8663e9c | 2019-11-20 13:58:29 -0800 | [diff] [blame] | 109 | .evaluated_expression = evaluated_expression, |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 110 | .line_number = line, |
| 111 | .success = success, |
| 112 | }; |
| 113 | |
Wyatt Hepler | cad29b4 | 2020-02-03 17:21:48 -0800 | [diff] [blame] | 114 | event_handler_->TestCaseExpect(current_test_->test_case(), expectation); |
| 115 | } |
| 116 | |
| 117 | bool TestInfo::enabled() const { |
| 118 | constexpr size_t kStringSize = sizeof("DISABLED_") - 1; |
| 119 | return std::strncmp("DISABLED_", test_case().test_name, kStringSize) != 0 && |
| 120 | std::strncmp("DISABLED_", test_case().suite_name, kStringSize) != 0; |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace internal |
Alexei Frolov | c10c812 | 2019-11-01 16:31:19 -0700 | [diff] [blame] | 124 | } // namespace pw::unit_test |