32#include <gtest/gtest-spi.h>
33#include <gtest/gtest.h>
54 class GTestFailure :
public std::runtime_error,
public FailureOrigin {
59 : std::runtime_error(message), origin_(std::move(origin)) {}
70 namespace gtest_hooks {
73 inline std::string current_test_name() {
74 const testing::TestInfo* info =
75 testing::UnitTest::GetInstance()->current_test_info();
76 if (info ==
nullptr) {
79 return std::string(info->test_suite_name()) +
"." +
85 inline std::string position(
const testing::TestPartResult& part) {
86 if (part.file_name() ==
nullptr) {
89 return std::string(part.file_name()) +
":" +
90 std::to_string(part.line_number());
99 raise_failures(
const testing::TestPartResultArray& recorded) {
101 for (
int i = 0; i < recorded.size(); i++) {
102 const testing::TestPartResult& part =
103 recorded.GetTestPartResult(i);
104 if (!part.failed()) {
107 if (!out.message.empty()) {
111 out.message += position(part) +
": " + part.message();
112 out.origin += position(part);
115 if (!out.message.empty()) {
116 throw GTestFailure(
"hegel::GTestFailure at " + out.origin,
122 struct FatalFailure {};
127 class Reporter :
public testing::ScopedFakeTestPartResultReporter {
129 explicit Reporter(testing::TestPartResultArray* recorded)
130 : ScopedFakeTestPartResultReporter(
131 INTERCEPT_ONLY_CURRENT_THREAD, recorded) {}
133 void ReportTestPartResult(
135 const testing::TestPartResult& result)
override {
136 ScopedFakeTestPartResultReporter::ReportTestPartResult(
138 if (result.fatally_failed()) {
139 throw FatalFailure{};
147 inline void run_case(
const std::function<
void()>& body) {
148 testing::TestPartResultArray recorded;
151 std::optional<Reporter> reporter;
152 if (testing::UnitTest::GetInstance()->current_test_info() !=
154 reporter.emplace(&recorded);
159 }
catch (
const FatalFailure&) {
164 raise_failures(recorded);
169 [[maybe_unused]]
static const bool hegel_gtest_hooks_installed =
170 install_framework_hooks(
171 {>est_hooks::current_test_name, >est_hooks::run_case});
GTestFailure(std::string origin, const std::string &message)
Definition gtest.h:58
std::string failure_origin() const override
Definition gtest.h:62
Main namespace.
Definition config.h:29