From e9ac727cc1534d541e1ed5dfd76b9941cfdc9c2d Mon Sep 17 00:00:00 2001 From: Arihant Kothari Date: Thu, 19 Feb 2026 15:51:36 +0900 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 64a168860..7bf21d0bb 100644 --- a/src/test/jtx/Env.h +++ b/src/test/jtx/Env.h @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -761,6 +762,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 4d112a19b..f6325c633 100644 --- a/src/test/rpc/AccountTx_test.cpp +++ b/src/test/rpc/AccountTx_test.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include #include @@ -852,12 +851,8 @@ public: 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 64e8caf28..23332d712 100644 --- a/src/test/rpc/LedgerRPC_test.cpp +++ b/src/test/rpc/LedgerRPC_test.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -2455,13 +2454,8 @@ public: 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)); } };