2024-11-11 16:36:37 +01:00
|
|
|
#include "rei-json/json.h"
|
|
|
|
#include <catch2/catch_test_macros.hpp>
|
|
|
|
#include <print>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
TEST_CASE("Basic json") {
|
|
|
|
auto objectJson = rei::json::JsonObject{};
|
|
|
|
objectJson
|
|
|
|
.set("keyPositiveNumber", 12)
|
|
|
|
.set("keyNegativeNumber", -13)
|
|
|
|
.set("keyBooleanTrue", true)
|
|
|
|
.set("keyBooleanFalse", false)
|
|
|
|
.set("keyString", "YEP")
|
|
|
|
.set("keyEmptyString", "")
|
|
|
|
.setNull("keyNull");
|
|
|
|
|
|
|
|
rei::json::JsonObject obj{};
|
|
|
|
obj.set("keyNumberOnObject", 42);
|
|
|
|
|
|
|
|
rei::json::JsonArray array{};
|
|
|
|
array.push(42);
|
|
|
|
array.push("elemString");
|
|
|
|
array.push("");
|
|
|
|
array.push(true);
|
|
|
|
array.push(false);
|
|
|
|
array.pushNull();
|
|
|
|
|
2024-11-22 17:53:00 +01:00
|
|
|
objectJson.set("keyObject", std::move(obj));
|
|
|
|
objectJson.set("keyArray", std::move(array));
|
2024-11-11 16:36:37 +01:00
|
|
|
|
|
|
|
REQUIRE(objectJson.getNumber("keyPositiveNumber") == 12);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyPositiveNumber"].asNumber() == 12);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.getNumber("keyNegativeNumber") == -13);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyNegativeNumber"].asNumber() == -13);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.getBool("keyBooleanTrue") == true);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyBooleanTrue"].asBool() == true);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.getBool("keyBooleanFalse") == false);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyBooleanFalse"].asBool() == false);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.getString("keyString") == "YEP");
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyString"].asString() == "YEP");
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.getString("keyEmptyString") == "");
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyEmptyString"].asString() == "");
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.getObject("keyObject").getNumber("keyNumberOnObject") == 42);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyObject"].asObject()["keyNumberOnObject"].asNumber() == 42);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.hasNull("keyNull") == true);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyNull"].isNull() == true);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(objectJson.hasNull("keyPositiveNumber") == false);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(objectJson["keyPositiveNumber"].isNull() == false);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE_THROWS(objectJson.getNumber("nonExistantKey"));
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE_THROWS(objectJson["nonExistantKey"]);
|
2024-11-11 16:36:37 +01:00
|
|
|
|
|
|
|
auto& arrayJson = objectJson.getArray("keyArray");
|
2024-11-22 17:53:00 +01:00
|
|
|
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(arrayJson.getNumber(0) == 42);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(arrayJson[0].asNumber() == 42);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(arrayJson.getString(1) == "elemString");
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(arrayJson[1].asString() == "elemString");
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(arrayJson.getString(2) == "");
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(arrayJson[2].asString() == "");
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(arrayJson.getBool(3) == true);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(arrayJson[3].asBool() == true);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(arrayJson.getBool(4) == false);
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(arrayJson[4].asBool() == false);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE(arrayJson.hasNull(5));
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE(arrayJson[5].isNull() == true);
|
|
|
|
|
2024-11-11 16:36:37 +01:00
|
|
|
REQUIRE_THROWS(arrayJson.getNumber(99));
|
2024-11-22 17:53:00 +01:00
|
|
|
REQUIRE_THROWS(arrayJson[99].asNumber());
|
|
|
|
|
|
|
|
objectJson.remove("keyString");
|
|
|
|
REQUIRE_THROWS(objectJson.getString("keyString") == "YEP");
|
|
|
|
|
|
|
|
arrayJson.remove(0);
|
|
|
|
REQUIRE(arrayJson[0].isNumber() == false);
|
|
|
|
REQUIRE(arrayJson[0].isString() == true);
|
|
|
|
REQUIRE(arrayJson[0].asString() == "elemString");
|
2024-11-11 16:36:37 +01:00
|
|
|
}
|