Add Exceptions indicating which line contains error when parsing JSON

This commit is contained in:
Vyn 2024-11-29 10:50:49 +01:00
parent fd034eff71
commit 2639dba60a
9 changed files with 307 additions and 177 deletions

83
tests/errors.cpp Normal file
View file

@ -0,0 +1,83 @@
#include "catch2/catch_test_macros.hpp"
#include "catch2/matchers/catch_matchers.hpp"
#include "rei-json/json.h"
#include <catch2/catch_all.hpp>
#include <exception>
#include <print>
#include <stdexcept>
#include <string>
#include <utility>
TEST_CASE("Parsing errors") {
SECTION("Root 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"
})";
REQUIRE_THROWS_WITH(rei::json::parse(jsonStr), "2:1: Expected JSON object or array");
}
SECTION("Missing comma") {
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"
})";
REQUIRE_THROWS_WITH(rei::json::parse(jsonStr), "12:1: Expected comma (',') or object ending ('}')");
}
SECTION("Missing object key quote") {
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"
})";
REQUIRE_THROWS_WITH(rei::json::parse(jsonStr), "11:1: Expected '\"'");
}
}