From 84589262c19fa12237bbbcadfca5f877e034e51b Mon Sep 17 00:00:00 2001 From: Ed Hennis Date: Mon, 29 Jun 2026 18:26:31 -0400 Subject: [PATCH] test: Add an RAII class to manage the env.parseFailureExpected flag --- src/test/app/Batch_test.cpp | 3 +-- src/test/app/Vault_test.cpp | 3 +-- src/test/jtx/Env.h | 32 ++++++++++++++++++++++++++++++++ src/test/jtx/impl/Env.cpp | 2 +- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/test/app/Batch_test.cpp b/src/test/app/Batch_test.cpp index 6aaae62155..eb104eb458 100644 --- a/src/test/app/Batch_test.cpp +++ b/src/test/app/Batch_test.cpp @@ -463,7 +463,7 @@ class Batch_test : public beast::unit_test::Suite auto const batchFee = batch::calcBatchFee(env, 0, 2); auto tx1 = batch::Inner(pay(alice, bob, XRP(1)), seq + 1); tx1[jss::Fee] = "1.5"; - env.setParseFailureExpected(true); + auto const g = env.getParseFailureGuard(true); try { env(batch::outer(alice, seq, batchFee, tfAllOrNothing), @@ -475,7 +475,6 @@ class Batch_test : public beast::unit_test::Suite { BEAST_EXPECT(true); } - env.setParseFailureExpected(false); } // temSEQ_AND_TICKET: Batch: inner txn cannot have both Sequence diff --git a/src/test/app/Vault_test.cpp b/src/test/app/Vault_test.cpp index 4643bc8555..c2051a5050 100644 --- a/src/test/app/Vault_test.cpp +++ b/src/test/app/Vault_test.cpp @@ -5811,9 +5811,9 @@ class Vault_test : public beast::unit_test::Suite env.close(); // 2. Mantissa larger than uint64 max - env.setParseFailureExpected(true); try { + auto const g = env.getParseFailureGuard(true); tx[sfAssetsMaximum] = "18446744073709551617e5"; // uint64 max + 1 env(tx); BEAST_EXPECTS(false, "Expected parse_error for mantissa larger than uint64 max"); @@ -5824,7 +5824,6 @@ class Vault_test : public beast::unit_test::Suite BEAST_EXPECT( e.what() == "invalidParamsField 'tx_json.AssetsMaximum' has invalid data."s); } - env.setParseFailureExpected(false); } } diff --git a/src/test/jtx/Env.h b/src/test/jtx/Env.h index 3d813d993c..07ce7db428 100644 --- a/src/test/jtx/Env.h +++ b/src/test/jtx/Env.h @@ -462,6 +462,38 @@ public: parseFailureExpected_ = b; } + /** RAII class to set and restore the parse failure flag (setParseFailureExpected). + * + * Can be created directly, or through the `getParseFailureGuard(bool)` function. + */ + class ParseFailureGuard final + { + Env& self_; + bool oldExpected = self_.parseFailureExpected_; + + public: + ParseFailureGuard(Env& self, bool b) : self_(self) + { + self_.setParseFailureExpected(b); + } + + ~ParseFailureGuard() + { + self_.setParseFailureExpected(oldExpected); + } + }; + + /** Gets an RAII guard to set and restore the parse failure flag + * + * Usage: + * auto const guard = env.getParseFailureGuard(true/false); + */ + ParseFailureGuard + getParseFailureGuard(bool b) + { + return ParseFailureGuard{*this, b}; + } + /** Turn off signature checks. */ void disableSigs() diff --git a/src/test/jtx/impl/Env.cpp b/src/test/jtx/impl/Env.cpp index db45deb6de..3049d2a736 100644 --- a/src/test/jtx/impl/Env.cpp +++ b/src/test/jtx/impl/Env.cpp @@ -602,7 +602,7 @@ Env::autofill(JTx& jt) catch (ParseError const&) { if (!parseFailureExpected_) - test.log << "parse failed:\n" << pretty(jv) << std::endl; + test.log << "parse failure:\n" << pretty(jv) << std::endl; rethrow(); } }