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
51 Collection const& actual,
52 Collection const& expected,
53 std::string const& message = "")
54 {
55 auto msg = addPrefix(message);
56 bool success = expectEquals(actual.size(), expected.size(), msg + "Sizes are different");
57 using std::begin;
58 using std::end;
59
60 auto i = begin(actual);
61 auto j = begin(expected);
62 auto k = 0;
63
64 for (; i != end(actual) && j != end(expected); ++i, ++j, ++k)
65 {
66 if (!expectEquals(*i, *j, msg + "Elements at " + std::to_string(k) + " are different."))
67 return false;
68 }
69
70 return success;
71 }
72
73 template <class Exception, class Functor>
74 bool
75 expectException(Functor f, std::string const& message = "")
76 {
77 bool success = false;
78 try
79 {
80 f();
81 }
82 catch (Exception const&)
83 {
84 success = true;
85 }
86 return expect(success, addPrefix(message) + "no exception thrown");
87 }
88
89 template <class Functor>
90 bool
91 expectException(Functor f, std::string const& message = "")
92 {
93 bool success = false;
94 try
95 {
96 f();
97 }
98 catch (std::exception const&)
99 {
100 success = true;
101 }
102 return expect(success, addPrefix(message) + "no exception thrown");
103 }
104
105private:
106 static std::string
107 addPrefix(std::string const& message)
108 {
109 std::string msg = message;
110 if (!msg.empty())
111 msg = ": " + msg;
112 return msg;
113 }
114};
115
116} // namespace xrpl
T begin(T... args)
A testsuite class.
Definition suite.h:51
void pass()
Record a successful test condition.
Definition suite.h:497
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:224
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:519
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:75
bool expectException(Functor f, std::string const &message="")
Definition TestSuite.h:91
static std::string addPrefix(std::string const &message)
Definition TestSuite.h:107
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)