third_party.pigweed.src/pw_unit_test/framework.cc
Alexei Frolov c10c81201d Add preprocessor and unit_test modules
This change adds two Pigweed modules: pw_preprocessor and pw_unit_test.
The preprocessor module contains header files providing helpful macros
for the C preprocessor. The unit test module contains a starter
implementation of a unit testing framework for Pigweed.

Change-Id: I46e1a4cae1fd8ce36d7840a2e92f8013fb489cde
2019-11-04 16:07:34 -08:00

113 lines
2.9 KiB
C++

// Copyright 2019 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
#include "pw_unit_test/framework.h"
namespace pw::unit_test {
void RegisterEventHandler(EventHandler* event_handler) {
internal::Framework::Get().RegisterEventHandler(event_handler);
}
namespace internal {
// Singleton instance of the unit test framework class.
Framework Framework::framework_;
// Linked list of all test cases in the test executable. This is static as it is
// populated using static initialization.
TestInfo* Framework::tests_ = nullptr;
void Framework::RegisterTest(TestInfo* test) {
// Append the test case to the end of the test list.
TestInfo** pos = &tests_;
for (; *pos != nullptr; pos = &(*pos)->next) {
}
*pos = test;
}
int Framework::RunAllTests() {
for (TestInfo* test = tests_; test != nullptr; test = test->next) {
test->run();
}
return exit_status_;
}
void Framework::StartTest(Test* test) {
current_test_ = test;
current_result_ = TestResult::kSuccess;
if (event_handler_ == nullptr) {
return;
}
const TestInfo* info = test->pigweed_test_info_;
TestCase test_case = {
.suite_name = info->test_suite_name,
.test_name = info->test_name,
.file_name = info->file_name,
};
event_handler_->TestCaseStart(test_case);
}
void Framework::EndTest(Test* test) {
current_test_ = nullptr;
if (event_handler_ == nullptr) {
return;
}
const TestInfo* info = test->pigweed_test_info_;
TestCase test_case = {
.suite_name = info->test_suite_name,
.test_name = info->test_name,
.file_name = info->file_name,
};
event_handler_->TestCaseEnd(test_case, current_result_);
}
void Framework::ExpectationResult(const char* expression,
int line,
bool success) {
if (!success) {
current_result_ = TestResult::kFailure;
exit_status_ = 1;
}
if (event_handler_ == nullptr) {
return;
}
const TestInfo* info = current_test_->pigweed_test_info_;
TestCase test_case = {
.suite_name = info->test_suite_name,
.test_name = info->test_name,
.file_name = info->file_name,
};
TestExpectation expectation = {
.expression = expression,
.line_number = line,
.success = success,
};
event_handler_->TestCaseExpect(test_case, expectation);
}
} // namespace internal
} // namespace pw::unit_test