Skip to content

Commit 13525e0

Browse files
committed
rpc: add arg helper unit test
Compare the results of self.Arg with the request.params accessors to ensure they behave the same way.
1 parent dfc35c9 commit 13525e0

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/test/rpc_tests.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,4 +581,58 @@ BOOST_AUTO_TEST_CASE(help_example)
581581
BOOST_CHECK_NE(HelpExampleRpcNamed("foo", {{"arg", true}}), HelpExampleRpcNamed("foo", {{"arg", "true"}}));
582582
}
583583

584+
static void CheckRpc(const std::vector<RPCArg>& params, const UniValue& args, RPCHelpMan::RPCMethodImpl test_impl)
585+
{
586+
auto null_result{RPCResult{RPCResult::Type::NONE, "", "None"}};
587+
const RPCHelpMan rpc{"dummy", "dummy description", params, null_result, RPCExamples{""}, test_impl};
588+
JSONRPCRequest req;
589+
req.params = args;
590+
591+
rpc.HandleRequest(req);
592+
}
593+
594+
BOOST_AUTO_TEST_CASE(rpc_arg_helper)
595+
{
596+
constexpr bool DEFAULT_BOOL = true;
597+
constexpr auto DEFAULT_STRING = "default";
598+
constexpr uint64_t DEFAULT_UINT64_T = 3;
599+
600+
//! Parameters with which the RPCHelpMan is instantiated
601+
const std::vector<RPCArg> params{
602+
// Required arg
603+
{"req_int", RPCArg::Type::NUM, RPCArg::Optional::NO, ""},
604+
{"req_str", RPCArg::Type::STR, RPCArg::Optional::NO, ""},
605+
// Default arg
606+
{"def_uint64_t", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_UINT64_T}, ""},
607+
{"def_string", RPCArg::Type::STR, RPCArg::Default{DEFAULT_STRING}, ""},
608+
{"def_bool", RPCArg::Type::BOOL, RPCArg::Default{DEFAULT_BOOL}, ""},
609+
// Optional arg without default
610+
{"opt_double", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, ""},
611+
{"opt_string", RPCArg::Type::STR, RPCArg::Optional::OMITTED, ""}
612+
};
613+
614+
//! Check that `self.Arg` returns the same value as the `request.params` accessors
615+
RPCHelpMan::RPCMethodImpl check_positional = [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
616+
BOOST_CHECK_EQUAL(self.Arg<int>(0), request.params[0].getInt<int>());
617+
BOOST_CHECK_EQUAL(self.Arg<std::string>(1), request.params[1].get_str());
618+
BOOST_CHECK_EQUAL(self.Arg<uint64_t>(2), request.params[2].isNull() ? DEFAULT_UINT64_T : request.params[2].getInt<uint64_t>());
619+
BOOST_CHECK_EQUAL(self.Arg<std::string>(3), request.params[3].isNull() ? DEFAULT_STRING : request.params[3].get_str());
620+
BOOST_CHECK_EQUAL(self.Arg<bool>(4), request.params[4].isNull() ? DEFAULT_BOOL : request.params[4].get_bool());
621+
if (!request.params[5].isNull()) {
622+
BOOST_CHECK_EQUAL(self.MaybeArg<double>(5).value(), request.params[5].get_real());
623+
} else {
624+
BOOST_CHECK(!self.MaybeArg<double>(5));
625+
}
626+
if (!request.params[6].isNull()) {
627+
BOOST_CHECK(self.MaybeArg<std::string>(6));
628+
BOOST_CHECK_EQUAL(*self.MaybeArg<std::string>(6), request.params[6].get_str());
629+
} else {
630+
BOOST_CHECK(!self.MaybeArg<std::string>(6));
631+
}
632+
return UniValue{};
633+
};
634+
CheckRpc(params, UniValue{JSON(R"([5, "hello", null, null, null, null, null])")}, check_positional);
635+
CheckRpc(params, UniValue{JSON(R"([5, "hello", 4, "test", true, 1.23, "world"])")}, check_positional);
636+
}
637+
584638
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)