New ripple::TestSuite with method expectEquals().

This commit is contained in:
Tom Ritchford
2014-12-22 12:31:58 -05:00
committed by Nik Bougalis
parent eafa6f960f
commit 9650b1aa70
6 changed files with 158 additions and 29 deletions

View File

@@ -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 <beast/unit_test/suite.h>
namespace ripple {
class TestSuite : public beast::unit_test::suite
{
public:
template <class S, class T>
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 <class Collection>
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 <class Exception, class Functor>
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 <class Functor>
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

View File

@@ -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 <ripple/basics/TestSuite.h>
namespace ripple {
struct TestSuite_test : TestSuite
{
void run() override
{
expectEquals (2, 2, "this won't get printed.");
using Vec = std::vector<std::string>;
Vec v1{"hello", "world"};
Vec v2{"hello", "world"};
expectCollectionEquals (v1, v2, "this won't get printed.");
// TODO(tom): how to test failure?
}
};
} // ripple

View File

@@ -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

View File

@@ -165,20 +165,6 @@ public:
"\"obj2\":{\"h\":\"w\",\"f\":false}}");
}
template <typename Functor>
void expectException (Functor f)
{
bool success = true;
try
{
f();
success = false;
} catch (std::exception)
{
}
expect (success, "no exception thrown");
}
void testFailureObject()
{
{

View File

@@ -22,12 +22,12 @@
#include <ripple/rpc/Output.h>
#include <ripple/rpc/impl/JsonWriter.h>
#include <beast/unit_test/suite.h>
#include <ripple/basics/TestSuite.h>
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);
}
};

View File

@@ -30,6 +30,7 @@
#include <ripple/basics/impl/StringUtilities.cpp>
#include <ripple/basics/impl/RangeSet.cpp>
#include <ripple/basics/impl/Sustain.cpp>
#include <ripple/basics/impl/TestSuite.test.cpp>
#include <ripple/basics/impl/ThreadName.cpp>
#include <ripple/basics/impl/Time.cpp>
#include <ripple/basics/impl/UptimeTimer.cpp>