Skip to content

Commit

Permalink
Merge bitcoin#10: Add pushKV(key, boolean) function (replaces bitcoin#5)
Browse files Browse the repository at this point in the history
129bad9 [tests] test pushKV for boolean values (John Newbery)
b3c44c9 Pushing boolean value to univalue correctly (Karel Bilek)

Pull request description:

  Adds the new pushKV(key, boolean) function from bitcoin#5, with test cases.

  Removal of srd::pair wrappers is in a future PR.

Tree-SHA512: d13b377365181723c06d4e4d7efa88f3cdbc6ca06ec2effe2ddf834bdfc7e602072c957296370323cb4e921a6934d55b040ebbdacca1dbff66e75d32073e6ac8
  • Loading branch information
laanwj committed Jan 13, 2018
2 parents 07947ff + 129bad9 commit 51d3ab3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/univalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ class UniValue {
UniValue tmpVal(val_);
return pushKV(key, tmpVal);
}
bool pushKV(const std::string& key, bool val_) {
UniValue tmpVal((bool)val_);
return pushKV(key, tmpVal);
}
bool pushKV(const std::string& key, int val_) {
UniValue tmpVal((int64_t)val_);
return pushKV(key, tmpVal);
Expand Down
14 changes: 13 additions & 1 deletion test/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,20 @@ BOOST_AUTO_TEST_CASE(univalue_object)
strKey = "temperature";
BOOST_CHECK(obj.pushKV(strKey, (double) 90.012));

strKey = "moon";
BOOST_CHECK(obj.pushKV(strKey, true));

strKey = "spoon";
BOOST_CHECK(obj.pushKV(strKey, false));

UniValue obj2(UniValue::VOBJ);
BOOST_CHECK(obj2.pushKV("cat1", 9000));
BOOST_CHECK(obj2.pushKV("cat2", 12345));

BOOST_CHECK(obj.pushKVs(obj2));

BOOST_CHECK_EQUAL(obj.empty(), false);
BOOST_CHECK_EQUAL(obj.size(), 9);
BOOST_CHECK_EQUAL(obj.size(), 11);

BOOST_CHECK_EQUAL(obj["age"].getValStr(), "100");
BOOST_CHECK_EQUAL(obj["first"].getValStr(), "John");
Expand All @@ -277,6 +283,8 @@ BOOST_AUTO_TEST_CASE(univalue_object)
BOOST_CHECK_EQUAL(obj["time"].getValStr(), "3600");
BOOST_CHECK_EQUAL(obj["calories"].getValStr(), "12");
BOOST_CHECK_EQUAL(obj["temperature"].getValStr(), "90.012");
BOOST_CHECK_EQUAL(obj["moon"].getValStr(), "1");
BOOST_CHECK_EQUAL(obj["spoon"].getValStr(), "");
BOOST_CHECK_EQUAL(obj["cat1"].getValStr(), "9000");
BOOST_CHECK_EQUAL(obj["cat2"].getValStr(), "12345");

Expand All @@ -289,6 +297,8 @@ BOOST_AUTO_TEST_CASE(univalue_object)
BOOST_CHECK(obj.exists("time"));
BOOST_CHECK(obj.exists("calories"));
BOOST_CHECK(obj.exists("temperature"));
BOOST_CHECK(obj.exists("moon"));
BOOST_CHECK(obj.exists("spoon"));
BOOST_CHECK(obj.exists("cat1"));
BOOST_CHECK(obj.exists("cat2"));

Expand All @@ -302,6 +312,8 @@ BOOST_AUTO_TEST_CASE(univalue_object)
objTypes["time"] = UniValue::VNUM;
objTypes["calories"] = UniValue::VNUM;
objTypes["temperature"] = UniValue::VNUM;
objTypes["moon"] = UniValue::VBOOL;
objTypes["spoon"] = UniValue::VBOOL;
objTypes["cat1"] = UniValue::VNUM;
objTypes["cat2"] = UniValue::VNUM;
BOOST_CHECK(obj.checkObject(objTypes));
Expand Down

0 comments on commit 51d3ab3

Please sign in to comment.