test: extend coverage of testJson
This commit is contained in:
1 parent
1d44575fcd
commit
9696853d5a
1 file changed
+73
@@ -303,3 +303,76 @@ VF_TEST(jsonPointerWalksIntoObjectsAndArrays) {
|
||||
CHECK_EQ(JsonPointer("/a~1b").text(), std::string("/a~1b"));
|
||||
CHECK_EQ(JsonPointer("/m~0n").text(), std::string("/m~0n"));
|
||||
|
||||
JsonValue* leaf = JsonPointer("/nested/list/0/leaf").resolve(root);
|
||||
CHECK(leaf != nullptr);
|
||||
*leaf = JsonValue::makeString("changed");
|
||||
CHECK_EQ(JsonPointer::find(root, "/nested/list/0/leaf")->stringValue(),
|
||||
std::string("changed"));
|
||||
}
|
||||
|
||||
VF_TEST(malformedPointersResolveToNothing) {
|
||||
JsonValue root = mustParse("{\"foo\":1}");
|
||||
CHECK(!JsonPointer("foo").valid());
|
||||
CHECK(!JsonPointer("/foo~").valid());
|
||||
CHECK(!JsonPointer("/foo~2").valid());
|
||||
CHECK(JsonPointer("foo").resolve(root) == nullptr);
|
||||
CHECK(JsonPointer("/foo~2").resolve(root) == nullptr);
|
||||
CHECK(JsonPointer("").valid());
|
||||
CHECK_EQ(JsonPointer("").text(), std::string(""));
|
||||
}
|
||||
|
||||
VF_TEST(typedReadsFallBackWhenTheFieldIsMissingOrOfTheWrongType) {
|
||||
JsonValue root = mustParse(
|
||||
"{\"count\":7,\"ratio\":0.5,\"label\":\"stone\",\"flag\":true,\"nothing\":null}");
|
||||
CHECK_EQ(root.intOr("count", -1), 7);
|
||||
CHECK_EQ(root.numberOr("ratio", 0.0), 0.5);
|
||||
CHECK_EQ(root.stringOr("label", "none"), std::string("stone"));
|
||||
CHECK(root.boolOr("flag", false));
|
||||
|
||||
CHECK_EQ(root.intOr("absent", -1), -1);
|
||||
CHECK_EQ(root.stringOr("count", "fallback"), std::string("fallback"));
|
||||
CHECK(root.boolOr("nothing", true));
|
||||
CHECK_EQ(root.numberOr("label", 2.5), 2.5);
|
||||
CHECK(!root.has("absent"));
|
||||
CHECK(root.has("nothing"));
|
||||
CHECK(root.find("nothing")->isNull());
|
||||
|
||||
CHECK_EQ(root.find("count")->intOr(0), 7);
|
||||
CHECK_EQ(root.find("label")->stringOr("x"), std::string("stone"));
|
||||
CHECK_EQ(root.at(0u), nullptr);
|
||||
CHECK_EQ(root.size(), static_cast<std::size_t>(5));
|
||||
CHECK_EQ(mustParse("[1,2,3]").size(), static_cast<std::size_t>(3));
|
||||
CHECK(mustParse("7").empty());
|
||||
}
|
||||
|
||||
VF_TEST(handBuiltDocumentsWriteAndReadBackTheSameWay) {
|
||||
JsonValue recipe = JsonValue::makeObject();
|
||||
recipe.set("type", JsonValue::makeString("craftingShaped"));
|
||||
JsonValue pattern = JsonValue::makeArray();
|
||||
pattern.push(JsonValue::makeString("XX"));
|
||||
pattern.push(JsonValue::makeString("XX"));
|
||||
recipe.set("pattern", std::move(pattern));
|
||||
JsonValue key = JsonValue::makeObject();
|
||||
key.set("X", JsonValue::makeString("voxelforge:planks"));
|
||||
recipe.set("key", std::move(key));
|
||||
JsonValue result = JsonValue::makeObject();
|
||||
result.set("item", JsonValue::makeString("voxelforge:crafting_table"));
|
||||
result.set("count", JsonValue::makeInt(1));
|
||||
recipe.set("result", std::move(result));
|
||||
recipe.set("mirror", JsonValue::makeBool(false));
|
||||
recipe.set("weightHint", JsonValue::makeNumber(0.25));
|
||||
|
||||
std::string text = JsonWriter::compact(recipe);
|
||||
CHECK_EQ(text,
|
||||
std::string("{\"type\":\"craftingShaped\",\"pattern\":[\"XX\",\"XX\"],"
|
||||
"\"key\":{\"X\":\"voxelforge:planks\"},"
|
||||
"\"result\":{\"item\":\"voxelforge:crafting_table\",\"count\":1},"
|
||||
"\"mirror\":false,\"weightHint\":0.25}"));
|
||||
JsonParseResult reread = parseJson(text);
|
||||
CHECK(reread.ok);
|
||||
CHECK(reread.root == recipe);
|
||||
|
||||
CHECK(recipe.erase("weightHint"));
|
||||
CHECK(!recipe.erase("weightHint"));
|
||||
CHECK(reread.root != recipe);
|
||||
}
|
||||
Reference in new issue
Block a user