rippled
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 <ripple/beast/unit_test.h>
24 #include <string>
25 
26 namespace ripple {
27 
28 class TestSuite : public beast::unit_test::suite
29 {
30 public:
31  template <class S, class T>
32  bool expectEquals (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: " << expected;
41  fail (ss.str());
42  return false;
43  }
44  pass ();
45  return true;
46 
47  }
48 
49  template <class S, class T>
50  bool expectNotEquals(S actual, T expected, std::string const& message = "")
51  {
52  if (actual == expected)
53  {
55  if (!message.empty())
56  ss << message << "\n";
57  ss << "Actual: " << actual << "\n"
58  << "Expected anything but: " << expected;
59  fail(ss.str());
60  return false;
61  }
62  pass();
63  return true;
64 
65  }
66 
67  template <class Collection>
69  Collection const& actual, Collection const& expected,
70  std::string const& message = "")
71  {
72  auto msg = addPrefix (message);
73  bool success = expectEquals (actual.size(), expected.size(),
74  msg + "Sizes are different");
75  using std::begin;
76  using std::end;
77 
78  auto i = begin (actual);
79  auto j = begin (expected);
80  auto k = 0;
81 
82  for (; i != end (actual) && j != end (expected); ++i, ++j, ++k)
83  {
84  if (!expectEquals (*i, *j, msg + "Elements at " +
85  std::to_string(k) + " are different."))
86  return false;
87  }
88 
89  return success;
90  }
91 
92  template <class Exception, class Functor>
93  bool expectException (Functor f, std::string const& message = "")
94  {
95  bool success = false;
96  try
97  {
98  f();
99  } catch (Exception const&)
100  {
101  success = true;
102  }
103  return expect (success, addPrefix (message) + "no exception thrown");
104  }
105 
106  template <class Functor>
107  bool expectException (Functor f, std::string const& message = "")
108  {
109  bool success = false;
110  try
111  {
112  f();
113  }
114  catch (std::exception const&)
115  {
116  success = true;
117  }
118  return expect (success, addPrefix (message) + "no exception thrown");
119  }
120 
121 private:
122  static std::string addPrefix (std::string const& message)
123  {
124  std::string msg = message;
125  if (!msg.empty())
126  msg = ": " + msg;
127  return msg;
128  }
129 };
130 
131 } // ripple
132 
133 #endif
ripple::TestSuite::expectException
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:107
std::string
STL class.
std::exception
STL class.
ripple::TestSuite::expectEquals
bool expectEquals(S actual, T expected, std::string const &message="")
Definition: TestSuite.h:32
ripple::TestSuite::addPrefix
static std::string addPrefix(std::string const &message)
Definition: TestSuite.h:122
std::stringstream
STL class.
ripple::TestSuite::expectCollectionEquals
bool expectCollectionEquals(Collection const &actual, Collection const &expected, std::string const &message="")
Definition: TestSuite.h:68
ripple::TestSuite::expectNotEquals
bool expectNotEquals(S actual, T expected, std::string const &message="")
Definition: TestSuite.h:50
std::to_string
T to_string(T... args)
ripple::TestSuite
Definition: TestSuite.h:28
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::begin
T begin(T... args)
std::string::empty
T empty(T... args)
std::stringstream::str
T str(T... args)
ripple::TestSuite::expectException
bool expectException(Functor f, std::string const &message="")
Definition: TestSuite.h:93
std::end
T end(T... args)
string