test_config_file.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**************************************************************************/
  2. /* test_config_file.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_CONFIG_FILE_H
  31. #define TEST_CONFIG_FILE_H
  32. #include "core/io/config_file.h"
  33. #include "core/os/os.h"
  34. #include "tests/test_macros.h"
  35. namespace TestConfigFile {
  36. TEST_CASE("[ConfigFile] Parsing well-formatted files") {
  37. ConfigFile config_file;
  38. // Formatting is intentionally hand-edited to see how human-friendly the parser is.
  39. const Error error = config_file.parse(R"(
  40. [player]
  41. name = "Unnamed Player"
  42. tagline="Waiting
  43. for
  44. Godot"
  45. color =Color( 0, 0.5,1, 1) ; Inline comment
  46. position= Vector2(
  47. 3,
  48. 4
  49. )
  50. [graphics]
  51. antialiasing = true
  52. ; Testing comments and case-sensitivity...
  53. antiAliasing = false
  54. )");
  55. CHECK_MESSAGE(error == OK, "The configuration file should parse successfully.");
  56. CHECK_MESSAGE(
  57. String(config_file.get_value("player", "name")) == "Unnamed Player",
  58. "Reading `player/name` should return the expected value.");
  59. CHECK_MESSAGE(
  60. String(config_file.get_value("player", "tagline")) == "Waiting\nfor\nGodot",
  61. "Reading `player/tagline` should return the expected value.");
  62. CHECK_MESSAGE(
  63. Color(config_file.get_value("player", "color")).is_equal_approx(Color(0, 0.5, 1)),
  64. "Reading `player/color` should return the expected value.");
  65. CHECK_MESSAGE(
  66. Vector2(config_file.get_value("player", "position")).is_equal_approx(Vector2(3, 4)),
  67. "Reading `player/position` should return the expected value.");
  68. CHECK_MESSAGE(
  69. bool(config_file.get_value("graphics", "antialiasing")),
  70. "Reading `graphics/antialiasing` should return `true`.");
  71. CHECK_MESSAGE(
  72. bool(config_file.get_value("graphics", "antiAliasing")) == false,
  73. "Reading `graphics/antiAliasing` should return `false`.");
  74. // An empty ConfigFile is valid.
  75. const Error error_empty = config_file.parse("");
  76. CHECK_MESSAGE(error_empty == OK,
  77. "An empty configuration file should parse successfully.");
  78. }
  79. TEST_CASE("[ConfigFile] Parsing malformatted file") {
  80. ConfigFile config_file;
  81. ERR_PRINT_OFF;
  82. const Error error = config_file.parse(R"(
  83. [player]
  84. name = "Unnamed Player"" ; Extraneous closing quote.
  85. tagline = "Waiting\nfor\nGodot"
  86. color = Color(0, 0.5, 1) ; Missing 4th parameter.
  87. position = Vector2(
  88. 3,,
  89. 4
  90. ) ; Extraneous comma.
  91. [graphics]
  92. antialiasing = true
  93. antialiasing = false ; Duplicate key.
  94. )");
  95. ERR_PRINT_ON;
  96. CHECK_MESSAGE(error == ERR_PARSE_ERROR,
  97. "The configuration file shouldn't parse successfully.");
  98. }
  99. TEST_CASE("[ConfigFile] Saving file") {
  100. ConfigFile config_file;
  101. config_file.set_value("player", "name", "Unnamed Player");
  102. config_file.set_value("player", "tagline", "Waiting\nfor\nGodot");
  103. config_file.set_value("player", "color", Color(0, 0.5, 1));
  104. config_file.set_value("player", "position", Vector2(3, 4));
  105. config_file.set_value("graphics", "antialiasing", true);
  106. config_file.set_value("graphics", "antiAliasing", false);
  107. config_file.set_value("quoted", String::utf8("静音"), 42);
  108. config_file.set_value("quoted", "a=b", 7);
  109. #ifdef WINDOWS_ENABLED
  110. const String config_path = OS::get_singleton()->get_environment("TEMP").path_join("config.ini");
  111. #else
  112. const String config_path = "/tmp/config.ini";
  113. #endif
  114. config_file.save(config_path);
  115. // Expected contents of the saved ConfigFile.
  116. const String contents = String::utf8(R"([player]
  117. name="Unnamed Player"
  118. tagline="Waiting
  119. for
  120. Godot"
  121. color=Color(0, 0.5, 1, 1)
  122. position=Vector2(3, 4)
  123. [graphics]
  124. antialiasing=true
  125. antiAliasing=false
  126. [quoted]
  127. "静音"=42
  128. "a=b"=7
  129. )");
  130. Ref<FileAccess> file = FileAccess::open(config_path, FileAccess::READ);
  131. CHECK_MESSAGE(file->get_as_utf8_string() == contents,
  132. "The saved configuration file should match the expected format.");
  133. }
  134. } // namespace TestConfigFile
  135. #endif // TEST_CONFIG_FILE_H