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