Skip to content

Commit

Permalink
test: add forAllApiVersions helper function (XRPLF#4611)
Browse files Browse the repository at this point in the history
Introduce a new variadic template helper function, `forAllApiVersions`,
that accepts callables to execute a set of functions over a range of
versions - from RPC::apiMinimumSupportedVersion to RPC::apiBetaVersion.
This avoids the duplication of code.

Context: XRPLF#4552
  • Loading branch information
arihantkothari authored and manojsdoshi committed Aug 18, 2023
1 parent df65548 commit ed15056
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
35 changes: 35 additions & 0 deletions src/test/jtx/Env.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <ripple/protocol/STAmount.h>
#include <ripple/protocol/STObject.h>
#include <ripple/protocol/STTx.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <functional>
#include <string>
#include <test/jtx/AbstractClient.h>
Expand Down Expand Up @@ -703,6 +704,40 @@ Env::rpc(std::string const& cmd, Args&&... args)
std::forward<Args>(args)...);
}

/**
* The SingleVersionedTestCallable concept checks for a callable that takes
* an unsigned integer as its argument and returns void.
*/
template <class T>
concept SingleVersionedTestCallable = requires(T callable, unsigned int version)
{
{
callable(version)
}
->std::same_as<void>;
};

/**
* The VersionedTestCallable concept checks if a set of callables all satisfy
* the SingleVersionedTestCallable concept. This allows forAllApiVersions to
* accept any number of functions. It executes a set of provided functions over
* a range of versions from RPC::apiMinimumSupportedVersion to
* RPC::apiBetaVersion. This is useful for running a series of tests or
* operations that need to be performed on multiple versions of an API.
*/
template <class... T>
concept VersionedTestCallable = (... && SingleVersionedTestCallable<T>);
void
forAllApiVersions(VersionedTestCallable auto... testCallable)
{
for (auto testVersion = RPC::apiMinimumSupportedVersion;
testVersion <= RPC::apiBetaVersion;
++testVersion)
{
(..., testCallable(testVersion));
}
}

} // namespace jtx
} // namespace test
} // namespace ripple
Expand Down
9 changes: 2 additions & 7 deletions src/test/rpc/AccountTx_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <ripple/beast/unit_test.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/jss.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <test/jtx.h>

#include <boost/container/flat_set.hpp>
Expand Down Expand Up @@ -667,12 +666,8 @@ class AccountTx_test : public beast::unit_test::suite
void
run() override
{
for (auto testVersion = RPC::apiMinimumSupportedVersion;
testVersion <= RPC::apiBetaVersion;
++testVersion)
{
testParameters(testVersion);
}
test::jtx::forAllApiVersions(
std::bind_front(&AccountTx_test::testParameters, this));
testContents();
testAccountDelete();
}
Expand Down
10 changes: 2 additions & 8 deletions src/test/rpc/LedgerRPC_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <ripple/beast/unit_test.h>
#include <ripple/protocol/ErrorCodes.h>
#include <ripple/protocol/jss.h>
#include <ripple/rpc/impl/RPCHelpers.h>
#include <test/jtx.h>

namespace ripple {
Expand Down Expand Up @@ -1737,13 +1736,8 @@ class LedgerRPC_test : public beast::unit_test::suite
testQueue();
testLedgerAccountsOption();

// version specific tests
for (auto testVersion = RPC::apiMinimumSupportedVersion;
testVersion <= RPC::apiBetaVersion;
++testVersion)
{
testLedgerEntryInvalidParams(testVersion);
}
test::jtx::forAllApiVersions(std::bind_front(
&LedgerRPC_test::testLedgerEntryInvalidParams, this));
}
};

Expand Down

0 comments on commit ed15056

Please sign in to comment.