rippled
Loading...
Searching...
No Matches
TestSuite.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_BASICS_TESTSUITE_H_INCLUDED
21#define RIPPLE_BASICS_TESTSUITE_H_INCLUDED
22
23#include <xrpl/beast/unit_test.h>
24
25#include <string>
26
27namespace ripple {
28
30{
31public:
32 template <class S, class T>
33 bool
34 expectEquals(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: " << expected;
43 fail(ss.str());
44 return false;
45 }
46 pass();
47 return true;
48 }
49
50 template <class S, class T>
51 bool
52 expectNotEquals(S actual, T expected, std::string const& message = "")
53 {
54 if (actual == expected)
55 {
57 if (!message.empty())
58 ss << message << "\n";
59 ss << "Actual: " << actual << "\n"
60 << "Expected anything but: " << expected;
61 fail(ss.str());
62 return false;
63 }
64 pass();
65 return true;
66 }
67
68 template <class Collection>
69 bool
71 Collection const& actual,
72 Collection const& expected,
73 std::string const& message = "")
74 {
75 auto msg = addPrefix(message);
76 bool success = expectEquals(
77 actual.size(), expected.size(), msg + "Sizes are different");
78 using std::begin;
79 using std::end;
80
81 auto i = begin(actual);
82 auto j = begin(expected);
83 auto k = 0;
84
85 for (; i != end(actual) && j != end(expected); ++i, ++j, ++k)
86 {
87 if (!expectEquals(
88 *i,
89 *j,
90 msg + "Elements at " + std::to_string(k) +
91 " are different."))
92 return false;
93 }
94
95 return success;
96 }
97
98 template <class Exception, class Functor>
99 bool
100 expectException(Functor f, std::string const& message = "")
101 {
102 bool success = false;
103 try
104 {
105 f();
106 }
107 catch (Exception const&)
108 {
109 success = true;
110 }
111 return expect(success, addPrefix(message) + "no exception thrown");
112 }
113
114 template <class Functor>
115 bool
116 expectException(Functor f, std::string const& message = "")
117 {
118 bool success = false;
119 try
120 {
121 f();
122 }
123 catch (std::exception const&)
124 {
125 success = true;
126 }
127 return expect(success, addPrefix(message) + "no exception thrown");
128 }
129
130private:
131 static std::string
132 addPrefix(std::string const& message)
133 {
134 std::string msg = message;
135 if (!msg.empty())
136 msg = ": " + msg;
137 return msg;
138 }
139};
140
141} // namespace ripple
142
143#endif
T begin(T... args)
A testsuite class.
Definition: suite.h:55
void pass()
Record a successful test condition.
Definition: suite.h:511
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition: suite.h:229
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition: suite.h:533
bool expectCollectionEquals(Collection const &actual, Collection const &expected, std::string const &message="")
Definition: TestSuite.h:70
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:116
bool expectEquals(S actual, T expected, std::string const &message="")
Definition: TestSuite.h:34
static std::string addPrefix(std::string const &message)
Definition: TestSuite.h:132
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:100
bool expectNotEquals(S actual, T 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:25
T str(T... args)
T to_string(T... args)