test_json.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*************************************************************************/
  2. /* test_json.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. Variant result;
  42. String err_str;
  43. int err_line;
  44. json.parse("null", result, err_str, err_line);
  45. CHECK_MESSAGE(
  46. err_line == 0,
  47. "Parsing `null` as JSON should parse successfully.");
  48. CHECK_MESSAGE(
  49. result == Variant(),
  50. "Parsing a double quoted string as JSON should return the expected value.");
  51. json.parse("true", result, err_str, err_line);
  52. CHECK_MESSAGE(
  53. err_line == 0,
  54. "Parsing boolean `true` as JSON should parse successfully.");
  55. CHECK_MESSAGE(
  56. result,
  57. "Parsing boolean `true` as JSON should return the expected value.");
  58. json.parse("false", result, err_str, err_line);
  59. CHECK_MESSAGE(
  60. err_line == 0,
  61. "Parsing boolean `false` as JSON should parse successfully.");
  62. CHECK_MESSAGE(
  63. !result,
  64. "Parsing boolean `false` as JSON should return the expected value.");
  65. // JSON only has a floating-point number type, no integer type.
  66. // This is why we use `is_equal_approx()` for the comparison.
  67. json.parse("123456", result, err_str, err_line);
  68. CHECK_MESSAGE(
  69. err_line == 0,
  70. "Parsing an integer number as JSON should parse successfully.");
  71. CHECK_MESSAGE(
  72. Math::is_equal_approx(result, 123'456),
  73. "Parsing an integer number as JSON should return the expected value.");
  74. json.parse("0.123456", result, err_str, err_line);
  75. CHECK_MESSAGE(
  76. err_line == 0,
  77. "Parsing a floating-point number as JSON should parse successfully.");
  78. CHECK_MESSAGE(
  79. Math::is_equal_approx(result, 0.123456),
  80. "Parsing a floating-point number as JSON should return the expected value.");
  81. json.parse("\"hello\"", result, err_str, err_line);
  82. CHECK_MESSAGE(
  83. err_line == 0,
  84. "Parsing a double quoted string as JSON should parse successfully.");
  85. CHECK_MESSAGE(
  86. result == "hello",
  87. "Parsing a double quoted string as JSON should return the expected value.");
  88. }
  89. TEST_CASE("[JSON] Parsing arrays") {
  90. JSON json;
  91. Variant result;
  92. String err_str;
  93. int err_line;
  94. // JSON parsing fails if it's split over several lines (even if leading indentation is removed).
  95. json.parse(
  96. R"(["Hello", "world.", "This is",["a","json","array.",[]], "Empty arrays ahoy:", [[["Gotcha!"]]]])",
  97. result, err_str, err_line);
  98. const Array array = result;
  99. CHECK_MESSAGE(
  100. err_line == 0,
  101. "Parsing a JSON array should parse successfully.");
  102. CHECK_MESSAGE(
  103. array[0] == "Hello",
  104. "The parsed JSON should contain the expected values.");
  105. const Array sub_array = array[3];
  106. CHECK_MESSAGE(
  107. sub_array.size() == 4,
  108. "The parsed JSON should contain the expected values.");
  109. CHECK_MESSAGE(
  110. sub_array[1] == "json",
  111. "The parsed JSON should contain the expected values.");
  112. CHECK_MESSAGE(
  113. sub_array[3].hash() == Array().hash(),
  114. "The parsed JSON should contain the expected values.");
  115. const Array deep_array = Array(Array(array[5])[0])[0];
  116. CHECK_MESSAGE(
  117. deep_array[0] == "Gotcha!",
  118. "The parsed JSON should contain the expected values.");
  119. }
  120. TEST_CASE("[JSON] Parsing objects (dictionaries)") {
  121. JSON json;
  122. Variant result;
  123. String err_str;
  124. int err_line;
  125. json.parse(
  126. R"({"name": "Godot Engine", "is_free": true, "bugs": null, "apples": {"red": 500, "green": 0, "blue": -20}, "empty_object": {}})",
  127. result, err_str, err_line);
  128. const Dictionary dictionary = result;
  129. CHECK_MESSAGE(
  130. dictionary["name"] == "Godot Engine",
  131. "The parsed JSON should contain the expected values.");
  132. CHECK_MESSAGE(
  133. dictionary["is_free"],
  134. "The parsed JSON should contain the expected values.");
  135. CHECK_MESSAGE(
  136. dictionary["bugs"] == Variant(),
  137. "The parsed JSON should contain the expected values.");
  138. CHECK_MESSAGE(
  139. Math::is_equal_approx(Dictionary(dictionary["apples"])["blue"], -20),
  140. "The parsed JSON should contain the expected values.");
  141. CHECK_MESSAGE(
  142. dictionary["empty_object"].hash() == Dictionary().hash(),
  143. "The parsed JSON should contain the expected values.");
  144. }
  145. } // namespace TestJSON
  146. #endif // TEST_JSON_H