rippled
Loading...
Searching...
No Matches
TestSuite.h
1#ifndef XRPL_BASICS_TESTSUITE_H_INCLUDED
2#define XRPL_BASICS_TESTSUITE_H_INCLUDED
3
4#include <xrpl/beast/unit_test.h>
5
6#include <string>
7
8namespace ripple {
9
11{
12public:
13 template <class S, class T>
14 bool
15 expectEquals(S actual, T expected, std::string const& message = "")
16 {
17 if (actual != expected)
18 {
20 if (!message.empty())
21 ss << message << "\n";
22 ss << "Actual: " << actual << "\n"
23 << "Expected: " << expected;
24 fail(ss.str());
25 return false;
26 }
27 pass();
28 return true;
29 }
30
31 template <class S, class T>
32 bool
33 expectNotEquals(S actual, T expected, std::string const& message = "")
34 {
35 if (actual == expected)
36 {
38 if (!message.empty())
39 ss << message << "\n";
40 ss << "Actual: " << actual << "\n"
41 << "Expected anything but: " << expected;
42 fail(ss.str());
43 return false;
44 }
45 pass();
46 return true;
47 }
48
49 template <class Collection>
50 bool
52 Collection const& actual,
53 Collection const& expected,
54 std::string const& message = "")
55 {
56 auto msg = addPrefix(message);
57 bool success = expectEquals(
58 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(
69 *i,
70 *j,
71 msg + "Elements at " + std::to_string(k) +
72 " are different."))
73 return false;
74 }
75
76 return success;
77 }
78
79 template <class Exception, class Functor>
80 bool
81 expectException(Functor f, std::string const& message = "")
82 {
83 bool success = false;
84 try
85 {
86 f();
87 }
88 catch (Exception const&)
89 {
90 success = true;
91 }
92 return expect(success, addPrefix(message) + "no exception thrown");
93 }
94
95 template <class Functor>
96 bool
97 expectException(Functor f, std::string const& message = "")
98 {
99 bool success = false;
100 try
101 {
102 f();
103 }
104 catch (std::exception const&)
105 {
106 success = true;
107 }
108 return expect(success, addPrefix(message) + "no exception thrown");
109 }
110
111private:
112 static std::string
113 addPrefix(std::string const& message)
114 {
115 std::string msg = message;
116 if (!msg.empty())
117 msg = ": " + msg;
118 return msg;
119 }
120};
121
122} // namespace ripple
123
124#endif
T begin(T... args)
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:508
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:226
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:530
bool expectCollectionEquals(Collection const &actual, Collection const &expected, std::string const &message="")
Definition TestSuite.h:51
bool expectException(Functor f, std::string const &message="")
Definition TestSuite.h:97
bool expectEquals(S actual, T expected, std::string const &message="")
Definition TestSuite.h:15
static std::string addPrefix(std::string const &message)
Definition TestSuite.h:113
bool expectException(Functor f, std::string const &message="")
Definition TestSuite.h:81
bool expectNotEquals(S actual, T expected, std::string const &message="")
Definition TestSuite.h:33
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:6
T str(T... args)
T to_string(T... args)