-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathShowTests.cpp
More file actions
86 lines (67 loc) · 2.25 KB
/
Copy pathShowTests.cpp
File metadata and controls
86 lines (67 loc) · 2.25 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
#include <catch2/catch.hpp>
#include <rapidcheck/catch.h>
#include "util/Box.h"
using namespace rc;
using namespace rc::test;
namespace {
struct Showable {};
void showValue(Showable x, std::ostream &os) { os << "showValue(Showable)"; };
// This is intentonally not used, that's part of the test. We ensure below that
// showValue is selected over operator<<
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunneeded-internal-declaration"
#endif // __clang__
std::ostream &operator<<(std::ostream &os, Showable) {
os << "<< Showable";
return os;
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif // __clang__
struct Ostreamable {};
std::ostream &operator<<(std::ostream &os, Ostreamable) {
os << "<< Ostreamable";
return os;
}
struct NonShowable {};
} // namespace
TEST_CASE("toString<T>") {
std::ostringstream os;
SECTION("uses showValue(...) overload if available") {
REQUIRE(toString(Showable()) == "showValue(Showable)");
}
SECTION(
"tries to use operator<<(ostream...) if showValue(...) is not availble") {
REQUIRE(toString(Ostreamable()) == "<< Ostreamable");
}
SECTION(
"if neither operator<< or showValue(...) available, show as <\?\?\?>") {
REQUIRE(toString(NonShowable()) == "<\?\?\?>");
}
}
TEST_CASE("showCollection") {
prop("shows empty collection correctly",
[](const std::string &prefix, const std::string &suffix) {
std::ostringstream os;
showCollection(prefix, suffix, std::vector<Box>(), os);
RC_ASSERT(os.str() == (prefix + suffix));
});
prop("shows single element correctly",
[&](const std::string &prefix, const std::string &suffix, Box a) {
std::ostringstream os;
showCollection(prefix, suffix, std::vector<Box>{a}, os);
RC_ASSERT(os.str() == (prefix + a.str() + suffix));
});
prop("shows multiple elements correctly",
[&](const std::string &prefix,
const std::string &suffix,
Box a,
Box b,
Box c) {
std::ostringstream os;
showCollection(prefix, suffix, std::vector<Box>{a, b, c}, os);
RC_ASSERT(os.str() == (prefix + a.str() + ", " + b.str() + ", " +
c.str() + suffix));
});
}