test_json.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. #ifndef TEST_JSON_H
  31. #define TEST_JSON_H
  32. #include "core/io/json.h"
  33. #include "thirdparty/doctest/doctest.h"
  34. namespace TestJSON {
  35. // NOTE: The current JSON parser accepts many non-conformant strings such as
  36. // single-quoted strings, duplicate commas and trailing commas.
  37. // This is intentionally not tested as users shouldn't rely on this behavior.
  38. TEST_CASE("[JSON] Parsing single data types") {
  39. // Parsing a single data type as JSON is valid per the JSON specification.
  40. JSON json;
  41. json.parse("null");
  42. CHECK_MESSAGE(
  43. json.get_error_line() == 0,
  44. "Parsing `null` as JSON should parse successfully.");
  45. CHECK_MESSAGE(
  46. json.get_data() == Variant(),
  47. "Parsing a double quoted string as JSON should return the expected value.");
  48. json.parse("true");
  49. CHECK_MESSAGE(
  50. json.get_error_line() == 0,
  51. "Parsing boolean `true` as JSON should parse successfully.");
  52. CHECK_MESSAGE(
  53. json.get_data(),
  54. "Parsing boolean `true` as JSON should return the expected value.");
  55. json.parse("false");
  56. CHECK_MESSAGE(
  57. json.get_error_line() == 0,
  58. "Parsing boolean `false` as JSON should parse successfully.");
  59. CHECK_MESSAGE(
  60. !json.get_data(),
  61. "Parsing boolean `false` as JSON should return the expected value.");
  62. json.parse("123456");
  63. CHECK_MESSAGE(
  64. json.get_error_line() == 0,
  65. "Parsing an integer number as JSON should parse successfully.");
  66. CHECK_MESSAGE(
  67. (int)(json.get_data()) == 123456,
  68. "Parsing an integer number as JSON should return the expected value.");
  69. json.parse("0.123456");
  70. CHECK_MESSAGE(
  71. json.get_error_line() == 0,
  72. "Parsing a floating-point number as JSON should parse successfully.");
  73. CHECK_MESSAGE(
  74. double(json.get_data()) == doctest::Approx(0.123456),
  75. "Parsing a floating-point number as JSON should return the expected value.");
  76. json.parse("\"hello\"");
  77. CHECK_MESSAGE(
  78. json.get_error_line() == 0,
  79. "Parsing a double quoted string as JSON should parse successfully.");
  80. CHECK_MESSAGE(
  81. json.get_data() == "hello",
  82. "Parsing a double quoted string as JSON should return the expected value.");
  83. }
  84. TEST_CASE("[JSON] Parsing arrays") {
  85. JSON json;
  86. // JSON parsing fails if it's split over several lines (even if leading indentation is removed).
  87. json.parse(R"(["Hello", "world.", "This is",["a","json","array.",[]], "Empty arrays ahoy:", [[["Gotcha!"]]]])");
  88. const Array array = json.get_data();
  89. CHECK_MESSAGE(
  90. json.get_error_line() == 0,
  91. "Parsing a JSON array should parse successfully.");
  92. CHECK_MESSAGE(
  93. array[0] == "Hello",
  94. "The parsed JSON should contain the expected values.");
  95. const Array sub_array = array[3];
  96. CHECK_MESSAGE(
  97. sub_array.size() == 4,
  98. "The parsed JSON should contain the expected values.");
  99. CHECK_MESSAGE(
  100. sub_array[1] == "json",
  101. "The parsed JSON should contain the expected values.");
  102. CHECK_MESSAGE(
  103. sub_array[3].hash() == Array().hash(),
  104. "The parsed JSON should contain the expected values.");
  105. const Array deep_array = Array(Array(array[5])[0])[0];
  106. CHECK_MESSAGE(
  107. deep_array[0] == "Gotcha!",
  108. "The parsed JSON should contain the expected values.");
  109. }
  110. TEST_CASE("[JSON] Parsing objects (dictionaries)") {
  111. JSON json;
  112. json.parse(R"({"name": "Godot Engine", "is_free": true, "bugs": null, "apples": {"red": 500, "green": 0, "blue": -20}, "empty_object": {}})");
  113. const Dictionary dictionary = json.get_data();
  114. CHECK_MESSAGE(
  115. dictionary["name"] == "Godot Engine",
  116. "The parsed JSON should contain the expected values.");
  117. CHECK_MESSAGE(
  118. dictionary["is_free"],
  119. "The parsed JSON should contain the expected values.");
  120. CHECK_MESSAGE(
  121. dictionary["bugs"] == Variant(),
  122. "The parsed JSON should contain the expected values.");
  123. CHECK_MESSAGE(
  124. (int)Dictionary(dictionary["apples"])["blue"] == -20,
  125. "The parsed JSON should contain the expected values.");
  126. CHECK_MESSAGE(
  127. dictionary["empty_object"].hash() == Dictionary().hash(),
  128. "The parsed JSON should contain the expected values.");
  129. }
  130. TEST_CASE("[JSON] Parsing escape sequences") {
  131. // Only certain escape sequences are valid according to the JSON specification.
  132. // Others must result in a parsing error instead.
  133. JSON json;
  134. TypedArray<String> valid_escapes;
  135. valid_escapes.push_back("\";\"");
  136. valid_escapes.push_back("\\;\\");
  137. valid_escapes.push_back("/;/");
  138. valid_escapes.push_back("b;\b");
  139. valid_escapes.push_back("f;\f");
  140. valid_escapes.push_back("n;\n");
  141. valid_escapes.push_back("r;\r");
  142. valid_escapes.push_back("t;\t");
  143. SUBCASE("Basic valid escape sequences") {
  144. for (int i = 0; i < valid_escapes.size(); i++) {
  145. String valid_escape = valid_escapes[i];
  146. String valid_escape_string = valid_escape.get_slice(";", 0);
  147. String valid_escape_value = valid_escape.get_slice(";", 1);
  148. String json_string = "\"\\";
  149. json_string += valid_escape_string;
  150. json_string += "\"";
  151. json.parse(json_string);
  152. CHECK_MESSAGE(
  153. json.get_error_line() == 0,
  154. vformat("Parsing valid escape sequence `%s` as JSON should parse successfully.", valid_escape_string));
  155. String json_value = json.get_data();
  156. CHECK_MESSAGE(
  157. json_value == valid_escape_value,
  158. vformat("Parsing valid escape sequence `%s` as JSON should return the expected value.", valid_escape_string));
  159. }
  160. }
  161. SUBCASE("Valid unicode escape sequences") {
  162. String json_string = "\"\\u0020\"";
  163. json.parse(json_string);
  164. CHECK_MESSAGE(
  165. json.get_error_line() == 0,
  166. vformat("Parsing valid unicode escape sequence with value `0020` as JSON should parse successfully."));
  167. String json_value = json.get_data();
  168. CHECK_MESSAGE(
  169. json_value == " ",
  170. vformat("Parsing valid unicode escape sequence with value `0020` as JSON should return the expected value."));
  171. }
  172. SUBCASE("Invalid escape sequences") {
  173. ERR_PRINT_OFF
  174. for (char32_t i = 0; i < 128; i++) {
  175. bool skip = false;
  176. for (int j = 0; j < valid_escapes.size(); j++) {
  177. String valid_escape = valid_escapes[j];
  178. String valid_escape_string = valid_escape.get_slice(";", 0);
  179. if (valid_escape_string[0] == i) {
  180. skip = true;
  181. break;
  182. }
  183. }
  184. if (skip) {
  185. continue;
  186. }
  187. String json_string = "\"\\";
  188. json_string += i;
  189. json_string += "\"";
  190. Error err = json.parse(json_string);
  191. // TODO: Line number is currently kept on 0, despite an error occurring. This should be fixed in the JSON parser.
  192. // CHECK_MESSAGE(
  193. // json.get_error_line() != 0,
  194. // vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse.", i));
  195. CHECK_MESSAGE(
  196. err == ERR_PARSE_ERROR,
  197. vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse with ERR_PARSE_ERROR.", i));
  198. }
  199. ERR_PRINT_ON
  200. }
  201. }
  202. } // namespace TestJSON
  203. #endif // TEST_JSON_H