test_json.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. } // namespace TestJSON
  131. #endif // TEST_JSON_H