mirror of
https://codeberg.org/vyn/rei-json.git
synced 2025-07-02 01:43:19 +00:00
first commit
This commit is contained in:
commit
eb5191a722
15 changed files with 1075 additions and 0 deletions
51
tests/usage.cpp
Normal file
51
tests/usage.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#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();
|
||||
|
||||
objectJson.addObject("keyObject", std::move(obj));
|
||||
objectJson.addArray("keyArray", std::move(array));
|
||||
|
||||
REQUIRE(objectJson.getNumber("keyPositiveNumber") == 12);
|
||||
REQUIRE(objectJson.getNumber("keyNegativeNumber") == -13);
|
||||
REQUIRE(objectJson.getBool("keyBooleanTrue") == true);
|
||||
REQUIRE(objectJson.getBool("keyBooleanFalse") == false);
|
||||
REQUIRE(objectJson.getString("keyString") == "YEP");
|
||||
REQUIRE(objectJson.getString("keyEmptyString") == "");
|
||||
REQUIRE(objectJson.getObject("keyObject").getNumber("keyNumberOnObject") == 42);
|
||||
REQUIRE(objectJson.hasNull("keyNull") == true);
|
||||
REQUIRE(objectJson.hasNull("keyPositiveNumber") == false);
|
||||
REQUIRE_THROWS(objectJson.getNumber("nonExistantKey"));
|
||||
|
||||
auto& arrayJson = objectJson.getArray("keyArray");
|
||||
REQUIRE(arrayJson.getNumber(0) == 42);
|
||||
REQUIRE(arrayJson.getString(1) == "elemString");
|
||||
REQUIRE(arrayJson.getString(2) == "");
|
||||
REQUIRE(arrayJson.getBool(3) == true);
|
||||
REQUIRE(arrayJson.getBool(4) == false);
|
||||
REQUIRE(arrayJson.hasNull(5));
|
||||
REQUIRE_THROWS(arrayJson.getNumber(99));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue