xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
jtx
TestSuite.h
1
#pragma once
2
3
#include <xrpl/beast/unit_test/suite.h>
4
5
#include <
exception
>
6
#include <
sstream
>
7
#include <
string
>
8
9
namespace
xrpl
{
10
11
class
TestSuite
:
public
beast::unit_test::Suite
12
{
13
public
:
14
template
<
class
S,
class
T>
15
bool
16
expectEquals
(S actual, T expected,
std::string
const
& message =
""
)
17
{
18
if
(actual != expected)
19
{
20
std::stringstream
ss;
21
if
(!message.empty())
22
ss << message <<
"\n"
;
23
ss <<
"Actual: "
<< actual <<
"\n"
24
<<
"Expected: "
<< expected;
25
fail
(ss.
str
());
26
return
false
;
27
}
28
pass
();
29
return
true
;
30
}
31
32
template
<
class
S,
class
T>
33
bool
34
expectNotEquals
(S actual, T expected,
std::string
const
& message =
""
)
35
{
36
if
(actual == expected)
37
{
38
std::stringstream
ss;
39
if
(!message.empty())
40
ss << message <<
"\n"
;
41
ss <<
"Actual: "
<< actual <<
"\n"
42
<<
"Expected anything but: "
<< expected;
43
fail
(ss.
str
());
44
return
false
;
45
}
46
pass
();
47
return
true
;
48
}
49
50
template
<
class
Collection>
51
bool
52
expectCollectionEquals
(
53
Collection
const
& actual,
54
Collection
const
& expected,
55
std::string
const
& message =
""
)
56
{
57
auto
msg =
addPrefix
(message);
58
bool
success =
expectEquals
(actual.size(), expected.size(), msg +
"Sizes are different"
);
59
using
std::begin
;
60
using
std::end
;
61
62
auto
i = begin(actual);
63
auto
j = begin(expected);
64
auto
k = 0;
65
66
for
(; i != end(actual) && j != end(expected); ++i, ++j, ++k)
67
{
68
if
(!
expectEquals
(*i, *j, msg +
"Elements at "
+
std::to_string
(k) +
" are different."
))
69
return
false
;
70
}
71
72
return
success;
73
}
74
75
template
<
class
Exception,
class
Functor>
76
bool
77
expectException
(Functor f,
std::string
const
& message =
""
)
78
{
79
bool
success =
false
;
80
try
81
{
82
f();
83
}
84
catch
(Exception
const
&)
85
{
86
success =
true
;
87
}
88
return
expect
(success,
addPrefix
(message) +
"no exception thrown"
);
89
}
90
91
template
<
class
Functor>
92
bool
93
expectException
(Functor f,
std::string
const
& message =
""
)
94
{
95
bool
success =
false
;
96
try
97
{
98
f();
99
}
100
catch
(
std::exception
const
&)
101
{
102
success =
true
;
103
}
104
return
expect
(success,
addPrefix
(message) +
"no exception thrown"
);
105
}
106
107
private
:
108
static
std::string
109
addPrefix
(
std::string
const
& message)
110
{
111
std::string
msg = message;
112
if
(!msg.
empty
())
113
msg =
": "
+ msg;
114
return
msg;
115
}
116
};
117
118
}
// namespace xrpl
std::string
std::stringstream
std::begin
T begin(T... args)
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
beast::unit_test::Suite::pass
void pass()
Record a successful test condition.
Definition
suite.h:532
beast::unit_test::Suite::expect
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition
suite.h:235
beast::unit_test::Suite::fail
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition
suite.h:554
xrpl::TestSuite
Definition
TestSuite.h:12
xrpl::TestSuite::expectEquals
bool expectEquals(S actual, T expected, std::string const &message="")
Definition
TestSuite.h:16
xrpl::TestSuite::expectException
bool expectException(Functor f, std::string const &message="")
Definition
TestSuite.h:77
xrpl::TestSuite::expectException
bool expectException(Functor f, std::string const &message="")
Definition
TestSuite.h:93
xrpl::TestSuite::addPrefix
static std::string addPrefix(std::string const &message)
Definition
TestSuite.h:109
xrpl::TestSuite::expectNotEquals
bool expectNotEquals(S actual, T expected, std::string const &message="")
Definition
TestSuite.h:34
xrpl::TestSuite::expectCollectionEquals
bool expectCollectionEquals(Collection const &actual, Collection const &expected, std::string const &message="")
Definition
TestSuite.h:52
std::string::empty
T empty(T... args)
std::end
T end(T... args)
exception
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
sstream
std::stringstream::str
T str(T... args)
string
std::to_string
T to_string(T... args)
Generated by
1.17.0