Skip to content

Commit 5039e0e

Browse files
committed
test: HelpExampleCliNamed and HelpExampleRpcNamed
1 parent 591735e commit 5039e0e

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/test/rpc_tests.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,39 @@ BOOST_AUTO_TEST_CASE(rpc_getblockstats_calculate_percentiles_by_weight)
421421
}
422422
}
423423

424+
BOOST_AUTO_TEST_CASE(help_example)
425+
{
426+
// test different argument types
427+
const RPCArgList& args = {{"foo", "bar"}, {"b", true}, {"n", 1}};
428+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", args), "> bitcoin-cli -named test foo=bar b=true n=1\n");
429+
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", args), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"foo\":\"bar\",\"b\":true,\"n\":1}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
430+
431+
// test shell escape
432+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b'ar"}}), "> bitcoin-cli -named test foo='b'''ar'\n");
433+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b\"ar"}}), "> bitcoin-cli -named test foo='b\"ar'\n");
434+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"foo", "b ar"}}), "> bitcoin-cli -named test foo='b ar'\n");
435+
436+
// test object params
437+
UniValue obj_value(UniValue::VOBJ);
438+
obj_value.pushKV("foo", "bar");
439+
obj_value.pushKV("b", false);
440+
obj_value.pushKV("n", 1);
441+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", obj_value}}), "> bitcoin-cli -named test name='{\"foo\":\"bar\",\"b\":false,\"n\":1}'\n");
442+
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", obj_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":{\"foo\":\"bar\",\"b\":false,\"n\":1}}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
443+
444+
// test array params
445+
UniValue arr_value(UniValue::VARR);
446+
arr_value.push_back("bar");
447+
arr_value.push_back(false);
448+
arr_value.push_back(1);
449+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("test", {{"name", arr_value}}), "> bitcoin-cli -named test name='[\"bar\",false,1]'\n");
450+
BOOST_CHECK_EQUAL(HelpExampleRpcNamed("test", {{"name", arr_value}}), "> curl --user myusername --data-binary '{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"test\", \"params\": {\"name\":[\"bar\",false,1]}}' -H 'content-type: text/plain;' http://127.0.0.1:8332/\n");
451+
452+
// test types don't matter for shell
453+
BOOST_CHECK_EQUAL(HelpExampleCliNamed("foo", {{"arg", true}}), HelpExampleCliNamed("foo", {{"arg", "true"}}));
454+
455+
// test types matter for Rpc
456+
BOOST_CHECK_NE(HelpExampleRpcNamed("foo", {{"arg", true}}), HelpExampleRpcNamed("foo", {{"arg", "true"}}));
457+
}
458+
424459
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)