1 // Copyright 2019 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_unit_test/framework.h"
22 void RegisterEventHandler(EventHandler* event_handler) {
23 internal::Framework::Get().RegisterEventHandler(event_handler);
28 // Singleton instance of the unit test framework class.
29 Framework Framework::framework_;
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;
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) {
42 // Append the test case to the end of the test list.
43 TestInfo* info = tests_;
44 for (; info->next() != nullptr; info = info->next()) {
46 info->set_next(new_test);
49 int Framework::RunAllTests() {
50 run_tests_summary_.passed_tests = 0;
51 run_tests_summary_.failed_tests = 0;
53 if (event_handler_ != nullptr) {
54 event_handler_->RunAllTestsStart();
56 for (const TestInfo* test = tests_; test != nullptr; test = test->next()) {
57 if (test->enabled()) {
59 } else if (event_handler_ != nullptr) {
60 event_handler_->TestCaseDisabled(test->test_case());
63 if (event_handler_ != nullptr) {
64 event_handler_->RunAllTestsEnd(run_tests_summary_);
69 void Framework::StartTest(const TestInfo& test) {
70 current_test_ = &test;
71 current_result_ = TestResult::kSuccess;
73 if (event_handler_ != nullptr) {
74 event_handler_->TestCaseStart(test.test_case());
78 void Framework::EndCurrentTest() {
79 switch (current_result_) {
80 case TestResult::kSuccess:
81 run_tests_summary_.passed_tests++;
83 case TestResult::kFailure:
84 run_tests_summary_.failed_tests++;
88 if (event_handler_ != nullptr) {
89 event_handler_->TestCaseEnd(current_test_->test_case(), current_result_);
92 current_test_ = nullptr;
95 void Framework::ExpectationResult(const char* expression,
96 const char* evaluated_expression,
100 current_result_ = TestResult::kFailure;
104 if (event_handler_ == nullptr) {
108 TestExpectation expectation = {
109 .expression = expression,
110 .evaluated_expression = evaluated_expression,
115 event_handler_->TestCaseExpect(current_test_->test_case(), expectation);
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;
124 } // namespace internal
125 } // namespace unit_test