1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include <Progmasoft/Catch3/Interfaces/IResultCapture.hpp>
#include <Progmasoft/Catch3/Internal/AssertionHandler.hpp>
#include <Progmasoft/Catch3/Internal/CompilerCapabilities.hpp>
#include <Progmasoft/Catch3/Internal/Context.hpp>
#include <Progmasoft/Catch3/Mock.hpp>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
namespace Progmasoft::Catch3::Detail
{
namespace
{
constexpr auto kMacro = "CATCH3_EXPECT_CALL";
/// Whether a test case is running that a report can belong to.
[[nodiscard]] bool
InTestCase()
{
const auto *capture = Catch::getCurrentContext().getResultCapture();
return capture != nullptr && !capture->getCurrentTestName().empty();
}
void
Report(const Catch::SourceLineInfo &line,
const std::string &mock,
Catch::ResultWas::OfType result,
std::string message,
Catch::ResultDisposition::Flags disposition)
{
Catch::AssertionHandler handler(kMacro, line, mock, disposition);
handler.handleMessage(result, std::move(message));
handler.complete();
}
void
WriteToStandardError(const Catch::SourceLineInfo &line,
const std::string &mock,
const std::string &message)
{
std::fprintf(stderr,
"%s:%zu: %s: %s\n",
line.file,
line.line,
mock.c_str(),
message.c_str());
}
} // namespace
void
MockSequence::Add(const std::shared_ptr<MockExpectationState> &step)
{
steps.push_back(step);
}
std::string
MockSequence::Enter(const MockExpectationState &step)
{
const auto position
= std::find_if(steps.begin() + static_cast<std::ptrdiff_t>(current),
steps.end(),
[&step](const auto &candidate) {
return candidate.lock().get() == &step;
});
std::string undone;
if (position == steps.end())
{
return undone;
}
const auto target = static_cast<std::size_t>(position - steps.begin());
for (; current < target; ++current)
{
const auto earlier = steps[current].lock();
if (earlier == nullptr)
{
continue;
}
if (!earlier->Satisfied())
{
undone += (undone.empty() ? "call out of order: " : "\n and ")
+ step.Description + " came before "
+ earlier->Description;
}
earlier->Retired = true;
}
return undone;
}
std::unique_lock<std::recursive_mutex>
LockMocks()
{
// Never destroyed: a mock of static lifetime can go after it would.
static auto *mutex = new std::recursive_mutex;
return std::unique_lock<std::recursive_mutex>{ *mutex };
}
void
ReportMockFailure(const Catch::SourceLineInfo &line,
const std::string &mock,
std::string message)
{
if (!InTestCase())
{
WriteToStandardError(line, mock, message);
return;
}
// An aborting run throws out of any failure. The run still stops, at
// its next assertion.
CATCH_TRY
{
Report(line,
mock,
Catch::ResultWas::ExplicitFailure,
std::move(message),
Catch::ResultDisposition::ContinueOnFailure);
}
CATCH_CATCH_ALL
{}
}
void
AbortMockCall(const Catch::SourceLineInfo &line,
const std::string &mock,
std::string message)
{
if (InTestCase())
{
Report(line,
mock,
Catch::ResultWas::ExplicitFailure,
std::move(message),
Catch::ResultDisposition::Normal);
}
else
{
WriteToStandardError(line, mock, message);
}
std::abort();
}
void
ReportMockSuccess(const Catch::SourceLineInfo &line,
const std::string &mock,
std::string message)
{
if (InTestCase())
{
Report(line,
mock,
Catch::ResultWas::Ok,
std::move(message),
Catch::ResultDisposition::ContinueOnFailure);
}
}
std::string
DescribeCalls(std::size_t minimum, std::size_t maximum)
{
const auto calls = [](std::size_t count) {
return std::to_string(count) + (count == 1 ? " call" : " calls");
};
if (minimum == 0 && maximum == kUnlimitedCalls)
{
return "any number of calls";
}
if (minimum == maximum)
{
return maximum == 0 ? "no calls" : "exactly " + calls(minimum);
}
if (maximum == kUnlimitedCalls)
{
return "at least " + calls(minimum);
}
if (minimum == 0)
{
return "at most " + calls(maximum);
}
return std::to_string(minimum) + " to " + calls(maximum);
}
std::string
DescribeCount(std::size_t calls)
{
switch (calls)
{
case 0:
return "never called";
case 1:
return "called once";
default:
return "called " + std::to_string(calls) + " times";
}
}
} // namespace Progmasoft::Catch3::Detail
|