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