test_json.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. #pragma once
  31. #include "core/io/json.h"
  32. #include "thirdparty/doctest/doctest.h"
  33. namespace TestJSON {
  34. // NOTE: The current JSON parser accepts many non-conformant strings such as
  35. // single-quoted strings, duplicate commas and trailing commas.
  36. // This is intentionally not tested as users shouldn't rely on this behavior.
  37. TEST_CASE("[JSON] Parsing single data types") {
  38. // Parsing a single data type as JSON is valid per the JSON specification.
  39. JSON json;
  40. json.parse("null");
  41. CHECK_MESSAGE(
  42. json.get_error_line() == 0,
  43. "Parsing `null` as JSON should parse successfully.");
  44. CHECK_MESSAGE(
  45. json.get_data() == Variant(),
  46. "Parsing a double quoted string as JSON should return the expected value.");
  47. json.parse("true");
  48. CHECK_MESSAGE(
  49. json.get_error_line() == 0,
  50. "Parsing boolean `true` as JSON should parse successfully.");
  51. CHECK_MESSAGE(
  52. json.get_data(),
  53. "Parsing boolean `true` as JSON should return the expected value.");
  54. json.parse("false");
  55. CHECK_MESSAGE(
  56. json.get_error_line() == 0,
  57. "Parsing boolean `false` as JSON should parse successfully.");
  58. CHECK_MESSAGE(
  59. !json.get_data(),
  60. "Parsing boolean `false` as JSON should return the expected value.");
  61. json.parse("123456");
  62. CHECK_MESSAGE(
  63. json.get_error_line() == 0,
  64. "Parsing an integer number as JSON should parse successfully.");
  65. CHECK_MESSAGE(
  66. (int)(json.get_data()) == 123456,
  67. "Parsing an integer number as JSON should return the expected value.");
  68. json.parse("0.123456");
  69. CHECK_MESSAGE(
  70. json.get_error_line() == 0,
  71. "Parsing a floating-point number as JSON should parse successfully.");
  72. CHECK_MESSAGE(
  73. double(json.get_data()) == doctest::Approx(0.123456),
  74. "Parsing a floating-point number as JSON should return the expected value.");
  75. json.parse("\"hello\"");
  76. CHECK_MESSAGE(
  77. json.get_error_line() == 0,
  78. "Parsing a double quoted string as JSON should parse successfully.");
  79. CHECK_MESSAGE(
  80. json.get_data() == "hello",
  81. "Parsing a double quoted string as JSON should return the expected value.");
  82. }
  83. TEST_CASE("[JSON] Parsing arrays") {
  84. JSON json;
  85. // JSON parsing fails if it's split over several lines (even if leading indentation is removed).
  86. json.parse(R"(["Hello", "world.", "This is",["a","json","array.",[]], "Empty arrays ahoy:", [[["Gotcha!"]]]])");
  87. const Array array = json.get_data();
  88. CHECK_MESSAGE(
  89. json.get_error_line() == 0,
  90. "Parsing a JSON array should parse successfully.");
  91. CHECK_MESSAGE(
  92. array[0] == "Hello",
  93. "The parsed JSON should contain the expected values.");
  94. const Array sub_array = array[3];
  95. CHECK_MESSAGE(
  96. sub_array.size() == 4,
  97. "The parsed JSON should contain the expected values.");
  98. CHECK_MESSAGE(
  99. sub_array[1] == "json",
  100. "The parsed JSON should contain the expected values.");
  101. CHECK_MESSAGE(
  102. sub_array[3].hash() == Array().hash(),
  103. "The parsed JSON should contain the expected values.");
  104. const Array deep_array = Array(Array(array[5])[0])[0];
  105. CHECK_MESSAGE(
  106. deep_array[0] == "Gotcha!",
  107. "The parsed JSON should contain the expected values.");
  108. }
  109. TEST_CASE("[JSON] Parsing objects (dictionaries)") {
  110. JSON json;
  111. json.parse(R"({"name": "Godot Engine", "is_free": true, "bugs": null, "apples": {"red": 500, "green": 0, "blue": -20}, "empty_object": {}})");
  112. const Dictionary dictionary = json.get_data();
  113. CHECK_MESSAGE(
  114. dictionary["name"] == "Godot Engine",
  115. "The parsed JSON should contain the expected values.");
  116. CHECK_MESSAGE(
  117. dictionary["is_free"],
  118. "The parsed JSON should contain the expected values.");
  119. CHECK_MESSAGE(
  120. dictionary["bugs"] == Variant(),
  121. "The parsed JSON should contain the expected values.");
  122. CHECK_MESSAGE(
  123. (int)Dictionary(dictionary["apples"])["blue"] == -20,
  124. "The parsed JSON should contain the expected values.");
  125. CHECK_MESSAGE(
  126. dictionary["empty_object"].hash() == Dictionary().hash(),
  127. "The parsed JSON should contain the expected values.");
  128. }
  129. TEST_CASE("[JSON] Parsing escape sequences") {
  130. // Only certain escape sequences are valid according to the JSON specification.
  131. // Others must result in a parsing error instead.
  132. JSON json;
  133. TypedArray<String> valid_escapes;
  134. valid_escapes.push_back("\";\"");
  135. valid_escapes.push_back("\\;\\");
  136. valid_escapes.push_back("/;/");
  137. valid_escapes.push_back("b;\b");
  138. valid_escapes.push_back("f;\f");
  139. valid_escapes.push_back("n;\n");
  140. valid_escapes.push_back("r;\r");
  141. valid_escapes.push_back("t;\t");
  142. SUBCASE("Basic valid escape sequences") {
  143. for (int i = 0; i < valid_escapes.size(); i++) {
  144. String valid_escape = valid_escapes[i];
  145. String valid_escape_string = valid_escape.get_slicec(';', 0);
  146. String valid_escape_value = valid_escape.get_slicec(';', 1);
  147. String json_string = "\"\\";
  148. json_string += valid_escape_string;
  149. json_string += "\"";
  150. json.parse(json_string);
  151. CHECK_MESSAGE(
  152. json.get_error_line() == 0,
  153. vformat("Parsing valid escape sequence `%s` as JSON should parse successfully.", valid_escape_string));
  154. String json_value = json.get_data();
  155. CHECK_MESSAGE(
  156. json_value == valid_escape_value,
  157. vformat("Parsing valid escape sequence `%s` as JSON should return the expected value.", valid_escape_string));
  158. }
  159. }
  160. SUBCASE("Valid unicode escape sequences") {
  161. String json_string = "\"\\u0020\"";
  162. json.parse(json_string);
  163. CHECK_MESSAGE(
  164. json.get_error_line() == 0,
  165. vformat("Parsing valid unicode escape sequence with value `0020` as JSON should parse successfully."));
  166. String json_value = json.get_data();
  167. CHECK_MESSAGE(
  168. json_value == " ",
  169. vformat("Parsing valid unicode escape sequence with value `0020` as JSON should return the expected value."));
  170. }
  171. SUBCASE("Invalid escape sequences") {
  172. ERR_PRINT_OFF
  173. for (char32_t i = 0; i < 128; i++) {
  174. bool skip = false;
  175. for (int j = 0; j < valid_escapes.size(); j++) {
  176. String valid_escape = valid_escapes[j];
  177. String valid_escape_string = valid_escape.get_slicec(';', 0);
  178. if (valid_escape_string[0] == i) {
  179. skip = true;
  180. break;
  181. }
  182. }
  183. if (skip) {
  184. continue;
  185. }
  186. String json_string = "\"\\";
  187. json_string += i;
  188. json_string += "\"";
  189. Error err = json.parse(json_string);
  190. // TODO: Line number is currently kept on 0, despite an error occurring. This should be fixed in the JSON parser.
  191. // CHECK_MESSAGE(
  192. // json.get_error_line() != 0,
  193. // vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse.", i));
  194. CHECK_MESSAGE(
  195. err == ERR_PARSE_ERROR,
  196. vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse with ERR_PARSE_ERROR.", i));
  197. }
  198. ERR_PRINT_ON
  199. }
  200. }
  201. TEST_CASE("[JSON] Serialization") {
  202. JSON json;
  203. struct FpTestCase {
  204. double number;
  205. String json;
  206. };
  207. struct IntTestCase {
  208. int64_t number;
  209. String json;
  210. };
  211. struct UIntTestCase {
  212. uint64_t number;
  213. String json;
  214. };
  215. static FpTestCase fp_tests_default_precision[] = {
  216. { 0.0, "0.0" },
  217. { 1000.1234567890123456789, "1000.12345678901" },
  218. { -1000.1234567890123456789, "-1000.12345678901" },
  219. { DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
  220. { DBL_MAX - 1, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
  221. { pow(2, 53), "9007199254740992.0" },
  222. { -pow(2, 53), "-9007199254740992.0" },
  223. { 0.00000000000000011, "0.00000000000000011" },
  224. { -0.00000000000000011, "-0.00000000000000011" },
  225. { 1.0 / 3.0, "0.333333333333333" },
  226. { 0.9999999999999999, "1.0" },
  227. { 1.0000000000000001, "1.0" },
  228. };
  229. static FpTestCase fp_tests_full_precision[] = {
  230. { 0.0, "0.0" },
  231. { 1000.1234567890123456789, "1000.12345678901238" },
  232. { -1000.1234567890123456789, "-1000.12345678901238" },
  233. { DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
  234. { DBL_MAX - 1, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
  235. { pow(2, 53), "9007199254740992.0" },
  236. { -pow(2, 53), "-9007199254740992.0" },
  237. { 0.00000000000000011, "0.00000000000000011" },
  238. { -0.00000000000000011, "-0.00000000000000011" },
  239. { 1.0 / 3.0, "0.333333333333333315" },
  240. { 0.9999999999999999, "0.999999999999999889" },
  241. { 1.0000000000000001, "1.0" },
  242. };
  243. static IntTestCase int_tests[] = {
  244. { 0, "0" },
  245. { INT64_MAX, "9223372036854775807" },
  246. { INT64_MIN, "-9223372036854775808" },
  247. };
  248. SUBCASE("Floating point default precision") {
  249. for (FpTestCase &test : fp_tests_default_precision) {
  250. String json_value = json.stringify(test.number, "", true, false);
  251. CHECK_MESSAGE(
  252. json_value == test.json,
  253. vformat("Serializing `%.20d` to JSON should return the expected value.", test.number));
  254. }
  255. }
  256. SUBCASE("Floating point full precision") {
  257. for (FpTestCase &test : fp_tests_full_precision) {
  258. String json_value = json.stringify(test.number, "", true, true);
  259. CHECK_MESSAGE(
  260. json_value == test.json,
  261. vformat("Serializing `%20f` to JSON should return the expected value.", test.number));
  262. }
  263. }
  264. SUBCASE("Signed integer") {
  265. for (IntTestCase &test : int_tests) {
  266. String json_value = json.stringify(test.number, "", true, true);
  267. CHECK_MESSAGE(
  268. json_value == test.json,
  269. vformat("Serializing `%d` to JSON should return the expected value.", test.number));
  270. }
  271. }
  272. }
  273. } // namespace TestJSON