-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtestjson5objects.cpp
More file actions
94 lines (84 loc) · 3.04 KB
/
Copy pathtestjson5objects.cpp
File metadata and controls
94 lines (84 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Licensed to Florent Guelfucci under one or more agreements.
// Florent Guelfucci licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#include <gtest/gtest.h>
#include "../src/TinyJSON.h"
#include "testshelper.h"
using namespace TinyJSON;
// These tests are inspired by the official JSON5 test suite:
// https://github.com/json5/json5-tests/tree/master/objects
TEST(TJJSON5Objects, UnquotedKeys)
{
const TJCHAR* json = TJCHARPREFIX("{hello: \"world\", _: 1, $: 2, a1: 3}");
parse_options options;
options.specification = parse_options::json5_1_0_0;
auto* tj = TJ::parse(json, options);
ASSERT_NE(nullptr, tj);
auto* jobj = dynamic_cast<TJValueObject*>(tj);
ASSERT_NE(nullptr, jobj);
EXPECT_STREQ(TJCHARPREFIX("world"), jobj->get_string(TJCHARPREFIX("hello")));
EXPECT_EQ(1, jobj->get_number<int>(TJCHARPREFIX("_")));
EXPECT_EQ(2, jobj->get_number<int>(TJCHARPREFIX("$")));
EXPECT_EQ(3, jobj->get_number<int>(TJCHARPREFIX("a1")));
delete tj;
}
TEST(TJJSON5Objects, ReservedWordKeys)
{
const TJCHAR* json = TJCHARPREFIX("{while: true, function: 1}");
parse_options options;
options.specification = parse_options::json5_1_0_0;
auto* tj = TJ::parse(json, options);
ASSERT_NE(nullptr, tj);
auto* jobj = dynamic_cast<TJValueObject*>(tj);
ASSERT_NE(nullptr, jobj);
EXPECT_TRUE(jobj->get_boolean(TJCHARPREFIX("while")));
EXPECT_EQ(1, jobj->get_number<int>(TJCHARPREFIX("function")));
delete tj;
}
TEST(TJJSON5Objects, SingleQuotedKeys)
{
const TJCHAR* json = TJCHARPREFIX("{'key': \"value\"}");
parse_options options;
options.specification = parse_options::json5_1_0_0;
auto* tj = TJ::parse(json, options);
ASSERT_NE(nullptr, tj);
auto* jobj = dynamic_cast<TJValueObject*>(tj);
ASSERT_NE(nullptr, jobj);
EXPECT_STREQ(TJCHARPREFIX("value"), jobj->get_string(TJCHARPREFIX("key")));
delete tj;
}
TEST(TJJSON5Objects, TrailingComma)
{
const TJCHAR* json = TJCHARPREFIX("{\"a\": 1,}");
parse_options options;
options.specification = parse_options::json5_1_0_0;
auto* tj = TJ::parse(json, options);
ASSERT_NE(nullptr, tj);
auto* jobj = dynamic_cast<TJValueObject*>(tj);
ASSERT_NE(nullptr, jobj);
ASSERT_EQ(1u, jobj->get_number_of_items());
delete tj;
}
TEST(TJJSON5Objects, IllegalUnquotedKeyStartRejected)
{
const TJCHAR* json = TJCHARPREFIX("{1a: 1}");
parse_options options;
options.specification = parse_options::json5_1_0_0;
auto* tj = TJ::parse(json, options);
ASSERT_EQ(nullptr, tj);
}
TEST(TJJSON5Objects, OperatorBracketJSON5Features)
{
const TJCHAR* json = TJCHARPREFIX("{unquoted: 1, 'single': 2, while: true, $: 3, _: 4}");
parse_options options;
options.specification = parse_options::json5_1_0_0;
auto* tj = TJ::parse(json, options);
ASSERT_NE(nullptr, tj);
// Test operator[] with JSON5 specific key styles
ASSERT_EQ(1, (*tj)["unquoted"].as<int>());
ASSERT_EQ(2, (*tj)["single"].as<int>());
ASSERT_TRUE((*tj)["while"].as<bool>());
ASSERT_EQ(3, (*tj)["$"].as<int>());
ASSERT_EQ(4, (*tj)["_"].as<int>());
delete tj;
}