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