Hegel 0.18.3
Property-based testing for C++
Loading...
Searching...
No Matches
gtest.h
1#pragma once
2
31
32#include <gtest/gtest-spi.h>
33#include <gtest/gtest.h>
34
35#include "hegel.h"
36
37#include <exception>
38#include <functional>
39#include <optional>
40#include <stdexcept>
41#include <string>
42
43namespace hegel {
44
55 class GTestFailure : public std::runtime_error, public FailureOrigin {
56 public:
59 GTestFailure(std::string origin, const std::string& message)
60 : std::runtime_error(message), origin_(std::move(origin)) {}
61
63 std::string failure_origin() const override { return origin_; }
64
65 private:
66 std::string origin_;
67 };
68
70 namespace internal {
71 namespace gtest_hooks {
72
73 // "Suite.Name" of the GoogleTest test that runs now.
74 inline std::string current_test_name() {
75 const testing::TestInfo* info =
76 testing::UnitTest::GetInstance()->current_test_info();
77 if (info == nullptr) {
78 return {};
79 }
80 return std::string(info->test_suite_name()) + "." +
81 info->name();
82 }
83
84 // Where one failed assertion is written, or "<unknown>" for an
85 // assertion GoogleTest could not place.
86 inline std::string position(const testing::TestPartResult& part) {
87 if (part.file_name() == nullptr) {
88 return "<unknown>";
89 }
90 return std::string(part.file_name()) + ":" +
91 std::to_string(part.line_number());
92 }
93
94 struct Failures {
95 std::string message;
96 std::string origin;
97 };
98
99 inline void
100 raise_failures(const testing::TestPartResultArray& recorded) {
101 Failures out;
102 for (int i = 0; i < recorded.size(); i++) {
103 const testing::TestPartResult& part =
104 recorded.GetTestPartResult(i);
105 if (!part.failed()) {
106 continue;
107 }
108 if (!out.message.empty()) {
109 out.message += "\n";
110 out.origin += ", ";
111 }
112 out.message += position(part) + ": " + part.message();
113 out.origin += position(part);
114 }
115
116 if (!out.message.empty()) {
117 throw GTestFailure("hegel::GTestFailure at " + out.origin,
118 out.message);
119 }
120 }
121
122 // Thrown when an assertion fails fatally.
123 struct FatalFailure {};
124
125 // Records the assertions of one test case. Without this, ASSERT_*
126 // returns only from the function it is in and the test case
127 // continues.
128 class Reporter : public testing::ScopedFakeTestPartResultReporter {
129 public:
130 explicit Reporter(testing::TestPartResultArray* recorded)
131 : ScopedFakeTestPartResultReporter(
132 INTERCEPT_ONLY_CURRENT_THREAD, recorded) {}
133
134 void ReportTestPartResult(
135 // override makes ASSERT_* instantly throw
136 const testing::TestPartResult& result) override {
137 ScopedFakeTestPartResultReporter::ReportTestPartResult(
138 result);
139 if (result.fatally_failed()) {
140 throw FatalFailure{};
141 }
142 }
143 };
144
145 inline bool is_assertion_failure(const std::exception& e) {
146 return dynamic_cast<const GTestFailure*>(&e) != nullptr;
147 }
148
149 // Runs one test-case body and collects GoogleTest assertions, then
150 // raises them as one exception. A fatal failure stops the body
151 // immediately.
152 inline void run_case(const std::function<void()>& body) {
153 testing::TestPartResultArray recorded;
154 {
155 // Outside a GoogleTest test, no assertion is recorded.
156 std::optional<Reporter> reporter;
157 if (testing::UnitTest::GetInstance()->current_test_info() !=
158 nullptr) {
159 reporter.emplace(&recorded);
160 }
161 try {
162 body();
163 // NOLINTNEXTLINE(bugprone-empty-catch)
164 } catch (const FatalFailure&) {
165 // The failure is in recorded. Continue to
166 // raise_failures below.
167 }
168 }
169 raise_failures(recorded);
170 }
171
172 } // namespace gtest_hooks
173
174 [[maybe_unused]] static const bool hegel_gtest_hooks_installed =
175 install_framework_hooks({&gtest_hooks::current_test_name,
176 &gtest_hooks::run_case,
177 &gtest_hooks::is_assertion_failure});
178 } // namespace internal
180
181} // namespace hegel
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