-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathPropertyTests.cpp
More file actions
394 lines (335 loc) · 13.6 KB
/
Copy pathPropertyTests.cpp
File metadata and controls
394 lines (335 loc) · 13.6 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include "rapidcheck/detail/Property.h"
#include "util/Generators.h"
#include "util/Predictable.h"
#include "util/GenUtils.h"
#include "util/TemplateProps.h"
#include "util/ShrinkableUtils.h"
using namespace rc;
using namespace rc::test;
using namespace rc::detail;
TEST_CASE("CaseDescription") {
SECTION("operator==/operator!=") {
propConformsToEquals<CaseDescription>();
PROP_REPLACE_MEMBER_INEQUAL(CaseDescription, result);
PROP_REPLACE_MEMBER_INEQUAL(CaseDescription, tags);
prop("not equal if example not equal",
[](const CaseDescription &original) {
auto other(original);
const auto otherExample = *gen::distinctFrom(other.example());
other.example = [=] { return otherExample; };
RC_ASSERT(original != other);
RC_ASSERT(other != original);
RC_ASSERT(!(original == other));
RC_ASSERT(!(other == original));
});
SECTION("equal if neither has example") {
CaseDescription a;
CaseDescription b;
REQUIRE(a == b);
}
SECTION("inequal if one has example but not the other") {
CaseDescription a;
CaseDescription b;
b.example = [] { return Example(); };
REQUIRE_FALSE(a == b);
}
}
SECTION("operator<<") {
propConformsToOutputOperator<CaseDescription>();
SECTION("prints description without example") {
std::ostringstream os;
CaseDescription desc;
os << desc;
REQUIRE(os.str().find("example=") == std::string::npos);
}
}
}
namespace {
template <typename Callable>
PropertyAdapter<Decay<Callable>> makeAdapter(Callable &&callable) {
return PropertyAdapter<Decay<Callable>>(std::forward<Callable>(callable));
}
bool descriptionContains(const TaggedResult &result, const std::string &str) {
return result.result.description.find(str) != std::string::npos;
}
void reportAll(const std::vector<CaseResult> &results) {
for (const auto &result : results) {
ImplicitParam<param::CurrentPropertyContext>::value()->reportResult(result);
}
}
void logAll(const std::vector<std::string> &log) {
auto &logStream =
ImplicitParam<param::CurrentPropertyContext>::value()->logStream();
for (const auto &message : log) {
logStream << message;
}
}
Gen<CaseResult> genResultOfType(std::initializer_list<CaseResult::Type> types) {
return gen::build<CaseResult>(
gen::set(&CaseResult::type,
gen::elementOf<std::vector<CaseResult::Type>>(types)),
gen::set(&CaseResult::description));
}
} // namespace
TEST_CASE("PropertyAdapter") {
prop("Discard overrides any other reported result",
[] {
auto results =
*gen::container<std::vector<CaseResult>>(genResultOfType(
{CaseResult::Type::Success, CaseResult::Type::Failure}));
const auto discardResult =
*genResultOfType({CaseResult::Type::Discard});
const auto position = *gen::inRange<std::size_t>(0, results.size());
results.insert(begin(results) + position, discardResult);
const auto result = makeAdapter([=] { reportAll(results); })().result;
RC_ASSERT(result == discardResult);
});
prop("Failure overrides any reported successes",
[] {
auto results = *gen::container<std::vector<CaseResult>>(
genResultOfType({CaseResult::Type::Success}));
const auto failureResult =
*genResultOfType({CaseResult::Type::Failure});
const auto position = *gen::inRange<std::size_t>(0, results.size());
results.insert(begin(results) + position, failureResult);
const auto result = makeAdapter([=] { reportAll(results); })().result;
RC_ASSERT(result == failureResult);
});
prop("result description contains descriptions of all reported failures",
[] {
auto failureResults =
*gen::container<std::vector<CaseResult>>(
genResultOfType({CaseResult::Type::Failure}));
const auto result = makeAdapter([=] { reportAll(failureResults); })();
for (const auto &failureResult : failureResults) {
RC_ASSERT(descriptionContains(result, failureResult.description));
}
});
prop("result description contains all logged messages",
[](const std::vector<std::string> &messages) {
const auto result = makeAdapter([=] { logAll(messages); })();
for (const auto &message : messages) {
RC_ASSERT(descriptionContains(result, message));
}
});
SECTION("does not include log when nothing was logged") {
const auto result = makeAdapter([=] {})();
REQUIRE(!descriptionContains(result, "Log:"));
}
prop("returns CaseResult as is",
[](const CaseResult &result) {
RC_ASSERT(makeAdapter([=] { return result; })().result == result);
});
SECTION("returns success result for void callables") {
REQUIRE(makeAdapter([] {})().result.type == CaseResult::Type::Success);
}
SECTION("if callable returns a bool") {
SECTION("returns success result for true") {
REQUIRE(makeAdapter([] { return true; })().result.type ==
CaseResult::Type::Success);
}
SECTION("returns success result for false") {
REQUIRE(makeAdapter([] { return false; })().result.type ==
CaseResult::Type::Failure);
}
}
SECTION("returns success for empty strings") {
REQUIRE(makeAdapter([] { return std::string(); })().result.type ==
CaseResult::Type::Success);
}
prop("returns failure with the string as message for non-empty strings",
[] {
// TODO non-empty generator
const auto msg = *gen::nonEmpty<std::string>();
const auto result = makeAdapter([&] { return msg; })();
RC_ASSERT(result.result.type == CaseResult::Type::Failure);
RC_ASSERT(descriptionContains(result, msg));
});
prop("if a CaseResult is thrown, returns that case result",
[](const CaseResult &result) {
RC_ASSERT(makeAdapter([&] { throw result; })().result == result);
});
prop("returns a discard result if a GenerationFailure is thrown",
[](const std::string &msg) {
const auto result =
makeAdapter([&] { throw GenerationFailure(msg); })();
RC_ASSERT(result.result.type == CaseResult::Type::Discard);
RC_ASSERT(descriptionContains(result, msg));
});
prop(
"returns a failure result with what message if an std::exception is"
" thrown",
[](const std::string &msg) {
const auto result =
makeAdapter([&] { throw std::runtime_error(msg); })();
RC_ASSERT(result.result.type == CaseResult::Type::Failure);
RC_ASSERT(descriptionContains(result, msg));
});
prop(
"returns a failure result with the string as the message if a string"
" is thrown",
[](const std::string &msg) {
const auto result = makeAdapter([&] { throw msg; })();
RC_ASSERT(result.result.type == CaseResult::Type::Failure);
RC_ASSERT(descriptionContains(result, msg));
});
SECTION("returns a failure result if other values are thrown") {
const auto result = makeAdapter([&] { throw 1337; })();
RC_ASSERT(result.result.type == CaseResult::Type::Failure);
}
prop("forwards arguments to callable",
[](int a, const std::string &b, NonCopyable c) {
const auto expected = std::to_string(a) + b + std::to_string(c.extra);
const auto adapter =
makeAdapter([](int d, const std::string &e, NonCopyable f) {
return std::to_string(d) + e + std::to_string(f.extra);
});
const auto result = adapter(std::move(a), std::move(b), std::move(c));
RC_ASSERT(result.result.description == expected);
});
prop("returns any tags that were added",
[](const std::vector<std::string> &tags) {
const auto result = makeAdapter([&] {
for (const auto &tag : tags) {
ImplicitParam<param::CurrentPropertyContext>::value()->addTag(tag);
}
})();
RC_ASSERT(result.tags == tags);
});
}
namespace {
template <int N>
struct Fixed {};
} // namespace
namespace rc {
template <int N>
struct Arbitrary<Fixed<N>> {
static Gen<Fixed<N>> arbitrary() {
return [](const Random &random, int) {
const int n = Random(random).next() % 10;
return shrinkable::just(
Fixed<N>(), seq::take(n, seq::repeat(shrinkable::just(Fixed<N>()))));
};
}
};
} // namespace rc
TEST_CASE("toProperty") {
using ShrinkableResult = Shrinkable<CaseDescription>;
prop("counterexample contains arguments as tuple",
[](const GenParams ¶ms) {
const auto gen = toProperty([=](Fixed<1>, Fixed<2>, Fixed<3>) {});
const auto shrinkable = gen(params.random, params.size);
const auto expected =
toString(std::make_tuple(Fixed<1>(), Fixed<2>(), Fixed<3>()));
onAnyPath(shrinkable,
[&](const ShrinkableResult &value,
const ShrinkableResult &shrink) {
RC_ASSERT(value.value().example().front().second ==
expected);
});
});
prop("counterexample contains string versions of picked values",
[](const GenParams ¶ms) {
const auto n = *gen::inRange<std::size_t>(0, 10);
const auto gen = toProperty([=] {
for (std::size_t i = 0; i < n; i++) {
*gen::arbitrary<Fixed<1337>>();
}
});
const auto shrinkable = gen(params.random, params.size);
const auto expected = toString(Fixed<1337>());
onAnyPath(shrinkable,
[&](const ShrinkableResult &value,
const ShrinkableResult &shrink) {
for (const auto &desc : value.value().example()) {
RC_ASSERT(desc.second == expected);
}
});
});
prop("counterexample contains type of value generator has no name",
[](const GenParams ¶ms) {
const auto gen = toProperty([] { *gen::arbitrary<int>(); });
const auto shrinkable = gen(params.random, params.size);
const auto expected = typeToString<int>();
onAnyPath(shrinkable,
[&](const ShrinkableResult &value,
const ShrinkableResult &shrink) {
RC_ASSERT(value.value().example().front().first ==
expected);
});
});
prop("counterexample contains name of generator if it has one",
[](const GenParams ¶ms) {
const auto name = *gen::nonEmpty<std::string>();
const auto gen = gen::arbitrary<int>().as(name);
const auto property = toProperty([=] { *gen; });
const auto shrinkable = property(params.random, params.size);
onAnyPath(shrinkable,
[&](const ShrinkableResult &value,
const ShrinkableResult &shrink) {
RC_ASSERT(value.value().example().front().first == name);
});
});
prop(
"throws in counterexample is replaced with placeholders dscribing the"
" error",
[](const GenParams ¶ms, const std::string &msg) {
const auto n = *gen::inRange<std::size_t>(1, 10);
const auto throwIndex = *gen::inRange<std::size_t>(0, n);
const auto gen = toProperty([=] {
for (std::size_t i = 0; i < n; i++) {
if (i == throwIndex) {
// TODO maybe a "throws" generator?
try {
*Gen<int>([=](const Random &, int) -> Shrinkable<int> {
throw GenerationFailure(msg);
});
} catch (...) {
}
} else {
*gen::arbitrary<Fixed<1337>>();
}
}
});
const auto shrinkable = gen(params.random, params.size);
const std::pair<std::string, std::string> expected("Generation failed",
msg);
onAnyPath(
shrinkable,
[&](const ShrinkableResult &value, const ShrinkableResult &shrink) {
RC_ASSERT(value.value().example()[throwIndex] == expected);
});
});
prop("case result corresponds to counterexample",
[](const GenParams ¶ms) {
const auto gen =
toProperty([=] { return (*gen::arbitrary<int>() % 2) == 0; });
const auto shrinkable = gen(params.random, params.size);
onAnyPath(
shrinkable,
[](const ShrinkableResult &value, const ShrinkableResult &shrink) {
const auto desc = value.value();
RC_ASSERT((desc.result.type == CaseResult::Type::Success) ==
((std::stoi(desc.example().back().second) % 2) == 0));
});
});
prop("tags correspond to counterexample",
[](const GenParams ¶ms) {
const auto gen = toProperty([=] {
const auto tags =
*gen::scale(0.25, gen::arbitrary<std::vector<std::string>>());
for (const auto &tag : tags) {
ImplicitParam<param::CurrentPropertyContext>::value()->addTag(tag);
}
});
const auto shrinkable = gen(params.random, params.size);
onAnyPath(
shrinkable,
[](const ShrinkableResult &value, const ShrinkableResult &shrink) {
const auto desc = value.value();
RC_ASSERT(toString(desc.tags) == desc.example().back().second);
});
});
}