Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_json.h
10278 views
1
/**************************************************************************/
2
/* test_json.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/json.h"
34
35
#include "thirdparty/doctest/doctest.h"
36
37
namespace TestJSON {
38
39
TEST_CASE("[JSON] Stringify single data types") {
40
CHECK(JSON::stringify(Variant()) == "null");
41
CHECK(JSON::stringify(false) == "false");
42
CHECK(JSON::stringify(true) == "true");
43
CHECK(JSON::stringify(0) == "0");
44
CHECK(JSON::stringify(12345) == "12345");
45
CHECK(JSON::stringify(0.75) == "0.75");
46
CHECK(JSON::stringify("test") == "\"test\"");
47
CHECK(JSON::stringify("\\\b\f\n\r\t\v\"") == "\"\\\\\\b\\f\\n\\r\\t\\v\\\"\"");
48
}
49
50
TEST_CASE("[JSON] Stringify arrays") {
51
CHECK(JSON::stringify(Array()) == "[]");
52
53
Array int_array;
54
for (int i = 0; i < 10; i++) {
55
int_array.push_back(i);
56
}
57
CHECK(JSON::stringify(int_array) == "[0,1,2,3,4,5,6,7,8,9]");
58
59
Array str_array;
60
str_array.push_back("Hello");
61
str_array.push_back("World");
62
str_array.push_back("!");
63
CHECK(JSON::stringify(str_array) == "[\"Hello\",\"World\",\"!\"]");
64
65
Array indented_array;
66
Array nested_array;
67
for (int i = 0; i < 5; i++) {
68
indented_array.push_back(i);
69
nested_array.push_back(i);
70
}
71
indented_array.push_back(nested_array);
72
CHECK(JSON::stringify(indented_array, "\t") == "[\n\t0,\n\t1,\n\t2,\n\t3,\n\t4,\n\t[\n\t\t0,\n\t\t1,\n\t\t2,\n\t\t3,\n\t\t4\n\t]\n]");
73
74
Array full_precision_array;
75
full_precision_array.push_back(0.123456789012345677);
76
CHECK(JSON::stringify(full_precision_array, "", true, true) == "[0.123456789012345677]");
77
78
ERR_PRINT_OFF
79
Array self_array;
80
self_array.push_back(self_array);
81
CHECK(JSON::stringify(self_array) == "[\"[...]\"]");
82
self_array.clear();
83
84
Array max_recursion_array;
85
for (int i = 0; i < Variant::MAX_RECURSION_DEPTH + 1; i++) {
86
Array next;
87
next.push_back(max_recursion_array);
88
max_recursion_array = next;
89
}
90
CHECK(JSON::stringify(max_recursion_array).contains("[...]"));
91
ERR_PRINT_ON
92
}
93
94
TEST_CASE("[JSON] Stringify dictionaries") {
95
CHECK(JSON::stringify(Dictionary()) == "{}");
96
97
Dictionary single_entry;
98
single_entry["key"] = "value";
99
CHECK(JSON::stringify(single_entry) == "{\"key\":\"value\"}");
100
101
Dictionary indented;
102
indented["key1"] = "value1";
103
indented["key2"] = 2;
104
CHECK(JSON::stringify(indented, "\t") == "{\n\t\"key1\": \"value1\",\n\t\"key2\": 2\n}");
105
106
Dictionary outer;
107
Dictionary inner;
108
inner["key"] = "value";
109
outer["inner"] = inner;
110
CHECK(JSON::stringify(outer) == "{\"inner\":{\"key\":\"value\"}}");
111
112
Dictionary full_precision_dictionary;
113
full_precision_dictionary["key"] = 0.123456789012345677;
114
CHECK(JSON::stringify(full_precision_dictionary, "", true, true) == "{\"key\":0.123456789012345677}");
115
116
ERR_PRINT_OFF
117
Dictionary self_dictionary;
118
self_dictionary["key"] = self_dictionary;
119
CHECK(JSON::stringify(self_dictionary) == "{\"key\":\"{...}\"}");
120
self_dictionary.clear();
121
122
Dictionary max_recursion_dictionary;
123
for (int i = 0; i < Variant::MAX_RECURSION_DEPTH + 1; i++) {
124
Dictionary next;
125
next["key"] = max_recursion_dictionary;
126
max_recursion_dictionary = next;
127
}
128
CHECK(JSON::stringify(max_recursion_dictionary).contains("{...:...}"));
129
ERR_PRINT_ON
130
}
131
132
// NOTE: The current JSON parser accepts many non-conformant strings such as
133
// single-quoted strings, duplicate commas and trailing commas.
134
// This is intentionally not tested as users shouldn't rely on this behavior.
135
136
TEST_CASE("[JSON] Parsing single data types") {
137
// Parsing a single data type as JSON is valid per the JSON specification.
138
139
JSON json;
140
141
json.parse("null");
142
CHECK_MESSAGE(
143
json.get_error_line() == 0,
144
"Parsing `null` as JSON should parse successfully.");
145
CHECK_MESSAGE(
146
json.get_data() == Variant(),
147
"Parsing a double quoted string as JSON should return the expected value.");
148
149
json.parse("true");
150
CHECK_MESSAGE(
151
json.get_error_line() == 0,
152
"Parsing boolean `true` as JSON should parse successfully.");
153
CHECK_MESSAGE(
154
json.get_data(),
155
"Parsing boolean `true` as JSON should return the expected value.");
156
157
json.parse("false");
158
CHECK_MESSAGE(
159
json.get_error_line() == 0,
160
"Parsing boolean `false` as JSON should parse successfully.");
161
CHECK_MESSAGE(
162
!json.get_data(),
163
"Parsing boolean `false` as JSON should return the expected value.");
164
165
json.parse("123456");
166
CHECK_MESSAGE(
167
json.get_error_line() == 0,
168
"Parsing an integer number as JSON should parse successfully.");
169
CHECK_MESSAGE(
170
(int)(json.get_data()) == 123456,
171
"Parsing an integer number as JSON should return the expected value.");
172
173
json.parse("0.123456");
174
CHECK_MESSAGE(
175
json.get_error_line() == 0,
176
"Parsing a floating-point number as JSON should parse successfully.");
177
CHECK_MESSAGE(
178
double(json.get_data()) == doctest::Approx(0.123456),
179
"Parsing a floating-point number as JSON should return the expected value.");
180
181
json.parse("\"hello\"");
182
CHECK_MESSAGE(
183
json.get_error_line() == 0,
184
"Parsing a double quoted string as JSON should parse successfully.");
185
CHECK_MESSAGE(
186
json.get_data() == "hello",
187
"Parsing a double quoted string as JSON should return the expected value.");
188
}
189
190
TEST_CASE("[JSON] Parsing arrays") {
191
JSON json;
192
193
// JSON parsing fails if it's split over several lines (even if leading indentation is removed).
194
json.parse(R"(["Hello", "world.", "This is",["a","json","array.",[]], "Empty arrays ahoy:", [[["Gotcha!"]]]])");
195
196
const Array array = json.get_data();
197
CHECK_MESSAGE(
198
json.get_error_line() == 0,
199
"Parsing a JSON array should parse successfully.");
200
CHECK_MESSAGE(
201
array[0] == "Hello",
202
"The parsed JSON should contain the expected values.");
203
const Array sub_array = array[3];
204
CHECK_MESSAGE(
205
sub_array.size() == 4,
206
"The parsed JSON should contain the expected values.");
207
CHECK_MESSAGE(
208
sub_array[1] == "json",
209
"The parsed JSON should contain the expected values.");
210
CHECK_MESSAGE(
211
sub_array[3].hash() == Array().hash(),
212
"The parsed JSON should contain the expected values.");
213
const Array deep_array = Array(Array(array[5])[0])[0];
214
CHECK_MESSAGE(
215
deep_array[0] == "Gotcha!",
216
"The parsed JSON should contain the expected values.");
217
}
218
219
TEST_CASE("[JSON] Parsing objects (dictionaries)") {
220
JSON json;
221
222
json.parse(R"({"name": "Godot Engine", "is_free": true, "bugs": null, "apples": {"red": 500, "green": 0, "blue": -20}, "empty_object": {}})");
223
224
const Dictionary dictionary = json.get_data();
225
CHECK_MESSAGE(
226
dictionary["name"] == "Godot Engine",
227
"The parsed JSON should contain the expected values.");
228
CHECK_MESSAGE(
229
dictionary["is_free"],
230
"The parsed JSON should contain the expected values.");
231
CHECK_MESSAGE(
232
dictionary["bugs"] == Variant(),
233
"The parsed JSON should contain the expected values.");
234
CHECK_MESSAGE(
235
(int)Dictionary(dictionary["apples"])["blue"] == -20,
236
"The parsed JSON should contain the expected values.");
237
CHECK_MESSAGE(
238
dictionary["empty_object"].hash() == Dictionary().hash(),
239
"The parsed JSON should contain the expected values.");
240
}
241
242
TEST_CASE("[JSON] Parsing escape sequences") {
243
// Only certain escape sequences are valid according to the JSON specification.
244
// Others must result in a parsing error instead.
245
246
JSON json;
247
248
TypedArray<String> valid_escapes = { "\";\"", "\\;\\", "/;/", "b;\b", "f;\f", "n;\n", "r;\r", "t;\t" };
249
250
SUBCASE("Basic valid escape sequences") {
251
for (int i = 0; i < valid_escapes.size(); i++) {
252
String valid_escape = valid_escapes[i];
253
String valid_escape_string = valid_escape.get_slicec(';', 0);
254
String valid_escape_value = valid_escape.get_slicec(';', 1);
255
256
String json_string = "\"\\";
257
json_string += valid_escape_string;
258
json_string += "\"";
259
json.parse(json_string);
260
261
CHECK_MESSAGE(
262
json.get_error_line() == 0,
263
vformat("Parsing valid escape sequence `%s` as JSON should parse successfully.", valid_escape_string));
264
265
String json_value = json.get_data();
266
CHECK_MESSAGE(
267
json_value == valid_escape_value,
268
vformat("Parsing valid escape sequence `%s` as JSON should return the expected value.", valid_escape_string));
269
}
270
}
271
272
SUBCASE("Valid unicode escape sequences") {
273
String json_string = "\"\\u0020\"";
274
json.parse(json_string);
275
276
CHECK_MESSAGE(
277
json.get_error_line() == 0,
278
vformat("Parsing valid unicode escape sequence with value `0020` as JSON should parse successfully."));
279
280
String json_value = json.get_data();
281
CHECK_MESSAGE(
282
json_value == " ",
283
vformat("Parsing valid unicode escape sequence with value `0020` as JSON should return the expected value."));
284
}
285
286
SUBCASE("Invalid escape sequences") {
287
ERR_PRINT_OFF
288
for (char32_t i = 0; i < 128; i++) {
289
bool skip = false;
290
for (int j = 0; j < valid_escapes.size(); j++) {
291
String valid_escape = valid_escapes[j];
292
String valid_escape_string = valid_escape.get_slicec(';', 0);
293
if (valid_escape_string[0] == i) {
294
skip = true;
295
break;
296
}
297
}
298
299
if (skip) {
300
continue;
301
}
302
303
String json_string = "\"\\";
304
json_string += i;
305
json_string += "\"";
306
Error err = json.parse(json_string);
307
308
// TODO: Line number is currently kept on 0, despite an error occurring. This should be fixed in the JSON parser.
309
// CHECK_MESSAGE(
310
// json.get_error_line() != 0,
311
// vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse.", i));
312
CHECK_MESSAGE(
313
err == ERR_PARSE_ERROR,
314
vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse with ERR_PARSE_ERROR.", i));
315
}
316
ERR_PRINT_ON
317
}
318
}
319
320
TEST_CASE("[JSON] Serialization") {
321
JSON json;
322
323
struct FpTestCase {
324
double number;
325
String json;
326
};
327
328
struct IntTestCase {
329
int64_t number;
330
String json;
331
};
332
333
struct UIntTestCase {
334
uint64_t number;
335
String json;
336
};
337
338
static FpTestCase fp_tests_default_precision[] = {
339
{ 0.0, "0.0" },
340
{ 1000.1234567890123456789, "1000.12345678901" },
341
{ -1000.1234567890123456789, "-1000.12345678901" },
342
{ DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
343
{ DBL_MAX - 1, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
344
{ std::pow(2, 53), "9007199254740992.0" },
345
{ -std::pow(2, 53), "-9007199254740992.0" },
346
{ 0.00000000000000011, "0.00000000000000011" },
347
{ -0.00000000000000011, "-0.00000000000000011" },
348
{ 1.0 / 3.0, "0.333333333333333" },
349
{ 0.9999999999999999, "1.0" },
350
{ 1.0000000000000001, "1.0" },
351
};
352
353
static FpTestCase fp_tests_full_precision[] = {
354
{ 0.0, "0.0" },
355
{ 1000.1234567890123456789, "1000.12345678901238" },
356
{ -1000.1234567890123456789, "-1000.12345678901238" },
357
{ DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
358
{ DBL_MAX - 1, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
359
{ std::pow(2, 53), "9007199254740992.0" },
360
{ -std::pow(2, 53), "-9007199254740992.0" },
361
{ 0.00000000000000011, "0.00000000000000011" },
362
{ -0.00000000000000011, "-0.00000000000000011" },
363
{ 1.0 / 3.0, "0.333333333333333315" },
364
{ 0.9999999999999999, "0.999999999999999889" },
365
{ 1.0000000000000001, "1.0" },
366
};
367
368
static IntTestCase int_tests[] = {
369
{ 0, "0" },
370
{ INT64_MAX, "9223372036854775807" },
371
{ INT64_MIN, "-9223372036854775808" },
372
};
373
374
SUBCASE("Floating point default precision") {
375
for (FpTestCase &test : fp_tests_default_precision) {
376
String json_value = json.stringify(test.number, "", true, false);
377
378
CHECK_MESSAGE(
379
json_value == test.json,
380
vformat("Serializing `%.20d` to JSON should return the expected value.", test.number));
381
}
382
}
383
384
SUBCASE("Floating point full precision") {
385
for (FpTestCase &test : fp_tests_full_precision) {
386
String json_value = json.stringify(test.number, "", true, true);
387
388
CHECK_MESSAGE(
389
json_value == test.json,
390
vformat("Serializing `%20f` to JSON should return the expected value.", test.number));
391
}
392
}
393
394
SUBCASE("Signed integer") {
395
for (IntTestCase &test : int_tests) {
396
String json_value = json.stringify(test.number, "", true, true);
397
398
CHECK_MESSAGE(
399
json_value == test.json,
400
vformat("Serializing `%d` to JSON should return the expected value.", test.number));
401
}
402
}
403
}
404
} // namespace TestJSON
405
406