blob: 70b36d341a3de8746b413be8fab17fcdc4c587d6 [file] [log] [blame]
Alexei Frolovc10c8122019-11-01 16:31:19 -07001// Copyright 2019 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
Wyatt Hepler1a960942019-11-26 14:13:38 -08004// use this file except in compliance with the License. You may obtain a copy of
5// the License at
Alexei Frolovc10c8122019-11-01 16:31:19 -07006//
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 Hepler1a960942019-11-26 14:13:38 -080012// License for the specific language governing permissions and limitations under
13// the License.
Alexei Frolovc10c8122019-11-01 16:31:19 -070014
15#include "pw_unit_test/framework.h"
16
Alexei Frolov47a43042021-04-06 14:19:55 -070017#include <algorithm>
Wyatt Hepler1c0e65e2020-01-27 13:10:18 -080018#include <cstring>
19
karthik bharadwaj4116dbd2020-08-10 11:03:06 -070020namespace pw {
21namespace unit_test {
Alexei Frolovc10c8122019-11-01 16:31:19 -070022
23void RegisterEventHandler(EventHandler* event_handler) {
24 internal::Framework::Get().RegisterEventHandler(event_handler);
25}
26
27namespace internal {
28
29// Singleton instance of the unit test framework class.
30Framework 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.
34TestInfo* Framework::tests_ = nullptr;
35
Wyatt Heplercad29b42020-02-03 17:21:48 -080036void 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 Frolovc10c8122019-11-01 16:31:19 -070041 }
Wyatt Heplercad29b42020-02-03 17:21:48 -080042
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 Frolovc10c8122019-11-01 16:31:19 -070048}
49
50int Framework::RunAllTests() {
Keir Mierle243e32a2019-11-05 10:29:26 -080051 run_tests_summary_.passed_tests = 0;
52 run_tests_summary_.failed_tests = 0;
Alexei Frolov47a43042021-04-06 14:19:55 -070053 run_tests_summary_.skipped_tests = 0;
54 run_tests_summary_.disabled_tests = 0;
Keir Mierle243e32a2019-11-05 10:29:26 -080055
56 if (event_handler_ != nullptr) {
57 event_handler_->RunAllTestsStart();
58 }
Wyatt Heplercad29b42020-02-03 17:21:48 -080059 for (const TestInfo* test = tests_; test != nullptr; test = test->next()) {
Alexei Frolov47a43042021-04-06 14:19:55 -070060 if (ShouldRunTest(*test)) {
Wyatt Hepler1c0e65e2020-01-27 13:10:18 -080061 test->run();
Alexei Frolov47a43042021-04-06 14:19:55 -070062 } 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 Hepler1c0e65e2020-01-27 13:10:18 -080070 }
Alexei Frolovc10c8122019-11-01 16:31:19 -070071 }
Keir Mierle243e32a2019-11-05 10:29:26 -080072 if (event_handler_ != nullptr) {
73 event_handler_->RunAllTestsEnd(run_tests_summary_);
74 }
Alexei Frolovc10c8122019-11-01 16:31:19 -070075 return exit_status_;
76}
77
Wyatt Heplercad29b42020-02-03 17:21:48 -080078void Framework::StartTest(const TestInfo& test) {
79 current_test_ = &test;
Alexei Frolovc10c8122019-11-01 16:31:19 -070080 current_result_ = TestResult::kSuccess;
81
Wyatt Heplercad29b42020-02-03 17:21:48 -080082 if (event_handler_ != nullptr) {
83 event_handler_->TestCaseStart(test.test_case());
Alexei Frolovc10c8122019-11-01 16:31:19 -070084 }
Alexei Frolovc10c8122019-11-01 16:31:19 -070085}
86
Wyatt Heplercad29b42020-02-03 17:21:48 -080087void Framework::EndCurrentTest() {
Keir Mierle243e32a2019-11-05 10:29:26 -080088 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 Frolovc10c8122019-11-01 16:31:19 -070096
Wyatt Heplercad29b42020-02-03 17:21:48 -080097 if (event_handler_ != nullptr) {
98 event_handler_->TestCaseEnd(current_test_->test_case(), current_result_);
Alexei Frolovc10c8122019-11-01 16:31:19 -070099 }
100
Wyatt Heplercad29b42020-02-03 17:21:48 -0800101 current_test_ = nullptr;
Alexei Frolovc10c8122019-11-01 16:31:19 -0700102}
103
104void Framework::ExpectationResult(const char* expression,
Wyatt Heplera55d4c72020-01-16 10:26:04 -0800105 const char* evaluated_expression,
Alexei Frolovc10c8122019-11-01 16:31:19 -0700106 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 Frolovc10c8122019-11-01 16:31:19 -0700117 TestExpectation expectation = {
118 .expression = expression,
Wyatt Hepler8663e9c2019-11-20 13:58:29 -0800119 .evaluated_expression = evaluated_expression,
Alexei Frolovc10c8122019-11-01 16:31:19 -0700120 .line_number = line,
121 .success = success,
122 };
123
Wyatt Heplercad29b42020-02-03 17:21:48 -0800124 event_handler_->TestCaseExpect(current_test_->test_case(), expectation);
125}
126
Alexei Frolov47a43042021-04-06 14:19:55 -0700127bool 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 Heplercad29b42020-02-03 17:21:48 -0800147bool 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 Frolovc10c8122019-11-01 16:31:19 -0700151}
152
153} // namespace internal
karthik bharadwaj4116dbd2020-08-10 11:03:06 -0700154} // namespace unit_test
155} // namespace pw