test_config_file.h 5.7 KB

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