From 228d4e587006341518715aa905ee3c1dee3299c3 Mon Sep 17 00:00:00 2001 From: Arihant Kothari Date: Sat, 8 Jul 2023 20:36:24 -0400 Subject: [PATCH] test: add forAllApiVersions helper function (#4611) 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: #4552 --- src/test/jtx/Env.h | 35 +++++++++++++++++++++++++++++++++ src/test/rpc/AccountTx_test.cpp | 9 ++------- src/test/rpc/LedgerRPC_test.cpp | 10 ++-------- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/test/jtx/Env.h b/src/test/jtx/Env.h index 57e69ef79df..08e7b22681d 100644 --- a/src/test/jtx/Env.h +++ b/src/test/jtx/Env.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -715,6 +716,40 @@ Env::rpc(std::string const& cmd, Args&&... args) std::forward(args)...); } +/** + * The SingleVersionedTestCallable concept checks for a callable that takes + * an unsigned integer as its argument and returns void. + */ +template +concept SingleVersionedTestCallable = requires(T callable, unsigned int version) +{ + { + callable(version) + } + ->std::same_as; +}; + +/** + * 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 +concept VersionedTestCallable = (... && SingleVersionedTestCallable); +void +forAllApiVersions(VersionedTestCallable auto... testCallable) +{ + for (auto testVersion = RPC::apiMinimumSupportedVersion; + testVersion <= RPC::apiBetaVersion; + ++testVersion) + { + (..., testCallable(testVersion)); + } +} + } // namespace jtx } // namespace test } // namespace ripple diff --git a/src/test/rpc/AccountTx_test.cpp b/src/test/rpc/AccountTx_test.cpp index 2e09ad93b86..05baf869eee 100644 --- a/src/test/rpc/AccountTx_test.cpp +++ b/src/test/rpc/AccountTx_test.cpp @@ -20,7 +20,6 @@ #include #include #include -#include #include #include @@ -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(); } diff --git a/src/test/rpc/LedgerRPC_test.cpp b/src/test/rpc/LedgerRPC_test.cpp index 9c9a63005a7..443209ca726 100644 --- a/src/test/rpc/LedgerRPC_test.cpp +++ b/src/test/rpc/LedgerRPC_test.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include namespace ripple { @@ -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)); } };