diff --git a/src/ripple/basics/TestSuite.h b/src/ripple/basics/TestSuite.h new file mode 100644 index 000000000..02c4bc601 --- /dev/null +++ b/src/ripple/basics/TestSuite.h @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#ifndef RIPPLED_RIPPLE_BASICS_TESTSUITE_H +#define RIPPLED_RIPPLE_BASICS_TESTSUITE_H + +#include + +namespace ripple { + +class TestSuite : public beast::unit_test::suite +{ +public: + template + bool expectEquals (S actual, T expected, std::string const& message = "") + { + if (actual != expected) + { + std::stringstream ss; + if (!message.empty()) + ss << message << "\n"; + ss << "Actual: " << actual << "\n" + << "Expected: " << expected; + fail (ss.str()); + return false; + } + pass (); + return true; + + } + + template + bool expectCollectionEquals ( + Collection const& actual, Collection const& expected, + std::string const& message = "") + { + auto msg = addPrefix (message); + bool success = expectEquals (actual.size(), expected.size(), + msg + "Sizes are different"); + using std::begin; + using std::end; + + auto i = begin (actual); + auto j = begin (expected); + auto k = 0; + + for (; i != end (actual) && j != end (expected); ++i, ++j, ++k) + { + if (!expectEquals (*i, *j, msg + "Elements at " + + std::to_string(k) + " are different.")) + return false; + } + + return success; + } + + template + bool expectException (Functor f, std::string const& message = "") + { + bool success = false; + try + { + f(); + } catch (Exception const&) + { + success = true; + } + return expect (success, addPrefix (message) + "no exception thrown"); + } + + template + bool expectException (Functor f, std::string const& message = "") + { + bool success = false; + try + { + f(); + } catch (...) + { + success = true; + } + return expect (success, addPrefix (message) + "no exception thrown"); + } + +private: + static std::string addPrefix (std::string const& message) + { + std::string msg = message; + if (!msg.empty()) + msg = ": " + msg; + return msg; + } +}; + +} // ripple + +#endif diff --git a/src/ripple/basics/impl/TestSuite.test.cpp b/src/ripple/basics/impl/TestSuite.test.cpp new file mode 100644 index 000000000..e64287c6c --- /dev/null +++ b/src/ripple/basics/impl/TestSuite.test.cpp @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2012, 2013 Ripple Labs Inc. + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +*/ +//============================================================================== + +#include + +namespace ripple { + +struct TestSuite_test : TestSuite +{ + void run() override + { + expectEquals (2, 2, "this won't get printed."); + using Vec = std::vector; + Vec v1{"hello", "world"}; + Vec v2{"hello", "world"}; + expectCollectionEquals (v1, v2, "this won't get printed."); + + // TODO(tom): how to test failure? + } +}; + +} // ripple diff --git a/src/ripple/rpc/impl/Coroutine.test.cpp b/src/ripple/rpc/impl/Coroutine.test.cpp index d97706f8c..2bc048d2e 100644 --- a/src/ripple/rpc/impl/Coroutine.test.cpp +++ b/src/ripple/rpc/impl/Coroutine.test.cpp @@ -51,9 +51,7 @@ public: result.push_back (buffer); } - auto r = strJoin (result.begin(), result.end(), ", "); - auto e = strJoin (expected.begin(), expected.end(), ", "); - expectEquals (r, e); + expectCollectionEquals (result, expected); } void run() override diff --git a/src/ripple/rpc/impl/JsonObject.test.cpp b/src/ripple/rpc/impl/JsonObject.test.cpp index b18d40d36..99f5c6018 100644 --- a/src/ripple/rpc/impl/JsonObject.test.cpp +++ b/src/ripple/rpc/impl/JsonObject.test.cpp @@ -165,20 +165,6 @@ public: "\"obj2\":{\"h\":\"w\",\"f\":false}}"); } - template - void expectException (Functor f) - { - bool success = true; - try - { - f(); - success = false; - } catch (std::exception) - { - } - expect (success, "no exception thrown"); - } - void testFailureObject() { { diff --git a/src/ripple/rpc/impl/TestOutputSuite.h b/src/ripple/rpc/impl/TestOutputSuite.h index 0bcbe5d19..ee512d33e 100644 --- a/src/ripple/rpc/impl/TestOutputSuite.h +++ b/src/ripple/rpc/impl/TestOutputSuite.h @@ -22,12 +22,12 @@ #include #include -#include +#include namespace ripple { namespace RPC { -class TestOutputSuite : public beast::unit_test::suite +class TestOutputSuite : public TestSuite { protected: std::string output_; @@ -41,19 +41,12 @@ protected: } // Test the result and report values. - void expectResult (std::string const& expected) + void expectResult (std::string const& expected, + std::string const& message = "") { writer_.reset (); - expectEquals (output_, expected); - } - - // Test the result and report values. - void expectEquals (std::string const& result, std::string const& expected) - { - expect (result == expected, - "\n" "result: '" + result + "'" + - "\n" "expected: '" + expected + "'"); + expectEquals (output_, expected, message); } }; diff --git a/src/ripple/unity/basics.cpp b/src/ripple/unity/basics.cpp index 3b53d6ae2..86002fe06 100644 --- a/src/ripple/unity/basics.cpp +++ b/src/ripple/unity/basics.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include