• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Pigweed Authors
2 //
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
5 // the License at
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
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include <algorithm>
16 #include <cstring>
17 
18 #include "light_public_overrides/pw_unit_test/framework_backend.h"
19 #include "pw_assert/check.h"
20 
21 namespace pw {
22 namespace unit_test {
23 
RegisterEventHandler(EventHandler * event_handler)24 void RegisterEventHandler(EventHandler* event_handler) {
25   internal::Framework::Get().RegisterEventHandler(event_handler);
26 }
27 
28 namespace internal {
29 
30 // Singleton instance of the unit test framework class.
31 Framework Framework::framework_;
32 
33 // Linked list of all test cases in the test executable. This is static as it is
34 // populated using static initialization.
35 TestInfo* Framework::tests_ = nullptr;
36 
RegisterTest(TestInfo * new_test) const37 void Framework::RegisterTest(TestInfo* new_test) const {
38   // If the test list is empty, set new_test as the first test.
39   if (tests_ == nullptr) {
40     tests_ = new_test;
41     return;
42   }
43 
44   // Find the right place in the test list to insert new test case.
45   TestInfo* info = tests_;
46   for (; info->next() != nullptr; info = info->next()) {
47     // Stop if this is the last test case from new test's suite.
48     if (strcmp(info->test_case().suite_name,
49                new_test->test_case().suite_name) == 0 &&
50         strcmp(info->next()->test_case().suite_name,
51                new_test->test_case().suite_name) != 0) {
52       break;
53     }
54   }
55 
56   new_test->set_next(info->next());
57   info->set_next(new_test);
58 }
59 
RunAllTests()60 int Framework::RunAllTests() {
61   exit_status_ = 0;
62   run_tests_summary_.passed_tests = 0;
63   run_tests_summary_.failed_tests = 0;
64   run_tests_summary_.skipped_tests = 0;
65   run_tests_summary_.disabled_tests = 0;
66 
67   if (event_handler_ != nullptr) {
68     event_handler_->RunAllTestsStart();
69   }
70   for (const TestInfo* test = tests_; test != nullptr; test = test->next()) {
71     if (ShouldRunTest(*test)) {
72       test->run();
73     } else if (!test->enabled()) {
74       run_tests_summary_.disabled_tests++;
75 
76       if (event_handler_ != nullptr) {
77         event_handler_->TestCaseDisabled(test->test_case());
78       }
79     } else {
80       run_tests_summary_.skipped_tests++;
81     }
82   }
83   if (event_handler_ != nullptr) {
84     event_handler_->RunAllTestsEnd(run_tests_summary_);
85   }
86   return exit_status_;
87 }
88 
SetUpTestSuiteIfNeeded(SetUpTestSuiteFunc set_up_ts) const89 void Framework::SetUpTestSuiteIfNeeded(SetUpTestSuiteFunc set_up_ts) const {
90   if (set_up_ts == Test::SetUpTestSuite) {
91     return;
92   }
93 
94   for (TestInfo* info = tests_; info != current_test_; info = info->next()) {
95     if (info->test_case().suite_name == current_test_->test_case().suite_name) {
96       return;
97     }
98   }
99 
100   set_up_ts();
101 }
102 
TearDownTestSuiteIfNeeded(TearDownTestSuiteFunc tear_down_ts) const103 void Framework::TearDownTestSuiteIfNeeded(
104     TearDownTestSuiteFunc tear_down_ts) const {
105   if (tear_down_ts == Test::TearDownTestSuite) {
106     return;
107   }
108 
109   for (TestInfo* info = current_test_->next(); info != nullptr;
110        info = info->next()) {
111     if (info->test_case().suite_name == current_test_->test_case().suite_name) {
112       return;
113     }
114   }
115 
116   tear_down_ts();
117 }
118 
StartTest(const TestInfo & test)119 void Framework::StartTest(const TestInfo& test) {
120   current_test_ = &test;
121   current_result_ = TestResult::kSuccess;
122 
123   if (event_handler_ != nullptr) {
124     event_handler_->TestCaseStart(test.test_case());
125   }
126 }
127 
EndCurrentTest()128 void Framework::EndCurrentTest() {
129   switch (current_result_) {
130     case TestResult::kSuccess:
131       run_tests_summary_.passed_tests++;
132       break;
133     case TestResult::kFailure:
134       run_tests_summary_.failed_tests++;
135       break;
136     case TestResult::kSkipped:
137       run_tests_summary_.skipped_tests++;
138       break;
139   }
140 
141   if (event_handler_ != nullptr) {
142     event_handler_->TestCaseEnd(current_test_->test_case(), current_result_);
143   }
144 
145   current_test_ = nullptr;
146 }
147 
CurrentTestSkip(int line)148 void Framework::CurrentTestSkip(int line) {
149   if (current_result_ == TestResult::kSuccess) {
150     current_result_ = TestResult::kSkipped;
151   }
152   return CurrentTestExpectSimple(
153       "(test skipped)", "(test skipped)", line, true);
154 }
155 
CurrentTestExpectSimple(const char * expression,const char * evaluated_expression,int line,bool success)156 void Framework::CurrentTestExpectSimple(const char* expression,
157                                         const char* evaluated_expression,
158                                         int line,
159                                         bool success) {
160   PW_CHECK_NOTNULL(
161       current_test_,
162       "EXPECT/ASSERT was called when no test was running! EXPECT/ASSERT cannot "
163       "be used from static constructors/destructors or before or after "
164       "RUN_ALL_TESTS().");
165 
166   if (!success) {
167     current_result_ = TestResult::kFailure;
168     exit_status_ = 1;
169   }
170 
171   if (event_handler_ == nullptr) {
172     return;
173   }
174 
175   TestExpectation expectation = {
176       .expression = expression,
177       .evaluated_expression = evaluated_expression,
178       .line_number = line,
179       .success = success,
180   };
181 
182   event_handler_->TestCaseExpect(current_test_->test_case(), expectation);
183 }
184 
ShouldRunTest(const TestInfo & test_info) const185 bool Framework::ShouldRunTest(const TestInfo& test_info) const {
186 #if PW_CXX_STANDARD_IS_SUPPORTED(17)
187   // Test suite filtering is only supported if using C++17.
188   if (!test_suites_to_run_.empty()) {
189     std::string_view test_suite(test_info.test_case().suite_name);
190 
191     bool suite_matches =
192         std::any_of(test_suites_to_run_.begin(),
193                     test_suites_to_run_.end(),
194                     [&](auto& name) { return test_suite == name; });
195 
196     if (!suite_matches) {
197       return false;
198     }
199   }
200 #endif  // PW_CXX_STANDARD_IS_SUPPORTED(17)
201 
202   return test_info.enabled();
203 }
204 
enabled() const205 bool TestInfo::enabled() const {
206   constexpr size_t kStringSize = sizeof("DISABLED_") - 1;
207   return std::strncmp("DISABLED_", test_case().test_name, kStringSize) != 0 &&
208          std::strncmp("DISABLED_", test_case().suite_name, kStringSize) != 0;
209 }
210 
211 }  // namespace internal
212 }  // namespace unit_test
213 }  // namespace pw
214