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#include <string>
25
26namespace ripple {
27
29{
30public:
31 template <class S, class T>
32 bool
33 expectEquals(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: " << expected;
42 fail(ss.str());
43 return false;
44 }
45 pass();
46 return true;
47 }
48
49 template <class S, class T>
50 bool
51 expectNotEquals(S actual, T expected, std::string const& message = "")
52 {
53 if (actual == expected)
54 {
56 if (!message.empty())
57 ss << message << "\n";
58 ss << "Actual: " << actual << "\n"
59 << "Expected anything but: " << expected;
60 fail(ss.str());
61 return false;
62 }
63 pass();
64 return true;
65 }
66
67 template <class Collection>
68 bool
70 Collection const& actual,
71 Collection const& expected,
72 std::string const& message = "")
73 {
74 auto msg = addPrefix(message);
75 bool success = expectEquals(
76 actual.size(), expected.size(), msg + "Sizes are different");
77 using std::begin;
78 using std::end;
79
80 auto i = begin(actual);
81 auto j = begin(expected);
82 auto k = 0;
83
84 for (; i != end(actual) && j != end(expected); ++i, ++j, ++k)
85 {
86 if (!expectEquals(
87 *i,
88 *j,
89 msg + "Elements at " + std::to_string(k) +
90 " are different."))
91 return false;
92 }
93
94 return success;
95 }
96
97 template <class Exception, class Functor>
98 bool
99 expectException(Functor f, std::string const& message = "")
100 {
101 bool success = false;
102 try
103 {
104 f();
105 }
106 catch (Exception const&)
107 {
108 success = true;
109 }
110 return expect(success, addPrefix(message) + "no exception thrown");
111 }
112
113 template <class Functor>
114 bool
115 expectException(Functor f, std::string const& message = "")
116 {
117 bool success = false;
118 try
119 {
120 f();
121 }
122 catch (std::exception const&)
123 {
124 success = true;
125 }
126 return expect(success, addPrefix(message) + "no exception thrown");
127 }
128
129private:
130 static std::string
131 addPrefix(std::string const& message)
132 {
133 std::string msg = message;
134 if (!msg.empty())
135 msg = ": " + msg;
136 return msg;
137 }
138};
139
140} // namespace ripple
141
142#endif
T begin(T... args)
A testsuite class.
Definition: suite.h:53
void pass()
Record a successful test condition.
Definition: suite.h:509
bool expect(Condition const &shouldBeTrue)
Evaluate a test condition.
Definition: suite.h:227
void fail(String const &reason, char const *file, int line)
Record a failure.
Definition: suite.h:531
bool expectCollectionEquals(Collection const &actual, Collection const &expected, std::string const &message="")
Definition: TestSuite.h:69
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:115
bool expectEquals(S actual, T expected, std::string const &message="")
Definition: TestSuite.h:33
static std::string addPrefix(std::string const &message)
Definition: TestSuite.h:131
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:99
bool expectNotEquals(S actual, T expected, std::string const &message="")
Definition: TestSuite.h:51
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:26
T str(T... args)
T to_string(T... args)