32#include <gtest/gtest-spi.h>
33#include <gtest/gtest.h>
55 class GTestFailure :
public std::runtime_error,
public FailureOrigin {
60 : std::runtime_error(message), origin_(std::move(origin)) {}
71 namespace gtest_hooks {
74 inline std::string current_test_name() {
75 const testing::TestInfo* info =
76 testing::UnitTest::GetInstance()->current_test_info();
77 if (info ==
nullptr) {
80 return std::string(
info->test_suite_name()) +
"." +
86 inline std::string position(
const testing::TestPartResult& part) {
87 if (part.file_name() ==
nullptr) {
90 return std::string(part.file_name()) +
":" +
91 std::to_string(part.line_number());
100 raise_failures(
const testing::TestPartResultArray& recorded) {
102 for (
int i = 0; i < recorded.size(); i++) {
103 const testing::TestPartResult& part =
104 recorded.GetTestPartResult(i);
105 if (!part.failed()) {
108 if (!out.message.empty()) {
112 out.message += position(part) +
": " + part.message();
113 out.origin += position(part);
116 if (!out.message.empty()) {
117 throw GTestFailure(
"hegel::GTestFailure at " + out.origin,
123 struct FatalFailure {};
128 class Reporter :
public testing::ScopedFakeTestPartResultReporter {
130 explicit Reporter(testing::TestPartResultArray* recorded)
131 : ScopedFakeTestPartResultReporter(
132 INTERCEPT_ONLY_CURRENT_THREAD, recorded) {}
134 void ReportTestPartResult(
136 const testing::TestPartResult& result)
override {
137 ScopedFakeTestPartResultReporter::ReportTestPartResult(
139 if (result.fatally_failed()) {
140 throw FatalFailure{};
145 inline bool is_assertion_failure(
const std::exception& e) {
146 return dynamic_cast<const GTestFailure*
>(&e) !=
nullptr;
152 inline void run_case(
const std::function<
void()>& body) {
153 testing::TestPartResultArray recorded;
156 std::optional<Reporter> reporter;
157 if (testing::UnitTest::GetInstance()->current_test_info() !=
159 reporter.emplace(&recorded);
164 }
catch (
const FatalFailure&) {
169 raise_failures(recorded);
174 [[maybe_unused]]
static const bool hegel_gtest_hooks_installed =
175 install_framework_hooks({>est_hooks::current_test_name,
176 >est_hooks::run_case,
177 >est_hooks::is_assertion_failure});
GTestFailure(std::string origin, const std::string &message)
Definition gtest.h:59
std::string failure_origin() const override
Definition gtest.h:63
std::optional< ZoneInfo > info(std::string_view name)
What the default zone source knows about name, matched exactly as the source spells it,...
Main namespace.
Definition config.h:29