xrpld
Loading...
Searching...
No Matches
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
9namespace xrpl {
10
12{
13public:
14 template <class S, class T>
15 bool
16 expectEquals(S actual, T expected, std::string const& message = "")
17 {
18 if (actual != expected)
19 {
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 {
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
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
107private:
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
T begin(T... args)
A testsuite class.
Definition suite.h:52
void pass()
Record a successful test condition.
Definition suite.h:532
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition suite.h:235
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition suite.h:554
bool expectEquals(S actual, T expected, std::string const &message="")
Definition TestSuite.h:16
bool expectException(Functor f, std::string const &message="")
Definition TestSuite.h:77
bool expectException(Functor f, std::string const &message="")
Definition TestSuite.h:93
static std::string addPrefix(std::string const &message)
Definition TestSuite.h:109
bool expectNotEquals(S actual, T expected, std::string const &message="")
Definition TestSuite.h:34
bool expectCollectionEquals(Collection const &actual, Collection const &expected, std::string const &message="")
Definition TestSuite.h:52
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)