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
50
tests/parsing.cpp
Normal file
50
tests/parsing.cpp
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include "rei-json/Object.h"
|
||||
#include "rei-json/json.h"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <string>
|
||||
|
||||
TEST_CASE("Parsing json object") {
|
||||
std::string jsonStr = R"({
|
||||
"keyArray": [
|
||||
42,
|
||||
"elemString",
|
||||
"",
|
||||
true,
|
||||
false,
|
||||
null
|
||||
],
|
||||
"keyBooleanFalse": false,
|
||||
"keyBooleanTrue": true,
|
||||
"keyEmptyString": "",
|
||||
"keyNegativeNumber": -13,
|
||||
"keyNull": null,
|
||||
"keyObject": {
|
||||
"keyNumberOnObject": 42
|
||||
},
|
||||
"keyPositiveNumber": 12,
|
||||
"keyString": "YEP"
|
||||
})";
|
||||
auto json = rei::json::parse(jsonStr);
|
||||
auto& objectJson = std::get<rei::json::JsonObject>(json);
|
||||
|
||||
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