test_project_settings.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* test_project_settings.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_PROJECT_SETTINGS_H
  31. #define TEST_PROJECT_SETTINGS_H
  32. #include "core/config/project_settings.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/variant/variant.h"
  35. #include "tests/test_macros.h"
  36. class TestProjectSettingsInternalsAccessor {
  37. public:
  38. static String &resource_path() {
  39. return ProjectSettings::get_singleton()->resource_path;
  40. };
  41. };
  42. namespace TestProjectSettings {
  43. // TODO: Handle some cases failing on release builds. See: https://github.com/godotengine/godot/pull/88452
  44. #ifdef TOOLS_ENABLED
  45. TEST_CASE("[ProjectSettings] Get existing setting") {
  46. CHECK(ProjectSettings::get_singleton()->has_setting("application/config/name"));
  47. Variant variant = ProjectSettings::get_singleton()->get_setting("application/config/name");
  48. CHECK_EQ(variant.get_type(), Variant::STRING);
  49. String name = variant;
  50. CHECK_EQ(name, "GDScript Integration Test Suite");
  51. }
  52. TEST_CASE("[ProjectSettings] Default value is ignored if setting exists") {
  53. CHECK(ProjectSettings::get_singleton()->has_setting("application/config/name"));
  54. Variant variant = ProjectSettings::get_singleton()->get_setting("application/config/name", "SomeDefaultValue");
  55. CHECK_EQ(variant.get_type(), Variant::STRING);
  56. String name = variant;
  57. CHECK_EQ(name, "GDScript Integration Test Suite");
  58. }
  59. #endif // TOOLS_ENABLED
  60. TEST_CASE("[ProjectSettings] Non existing setting is null") {
  61. CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting"));
  62. Variant variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting");
  63. CHECK_EQ(variant.get_type(), Variant::NIL);
  64. }
  65. TEST_CASE("[ProjectSettings] Non existing setting should return default value") {
  66. CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting"));
  67. Variant variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting");
  68. CHECK_EQ(variant.get_type(), Variant::NIL);
  69. variant = ProjectSettings::get_singleton()->get_setting("not_existing_setting", "my_nice_default_value");
  70. CHECK_EQ(variant.get_type(), Variant::STRING);
  71. String name = variant;
  72. CHECK_EQ(name, "my_nice_default_value");
  73. CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("not_existing_setting"));
  74. }
  75. TEST_CASE("[ProjectSettings] Set value should be returned when retrieved") {
  76. CHECK_FALSE(ProjectSettings::get_singleton()->has_setting("my_custom_setting"));
  77. Variant variant = ProjectSettings::get_singleton()->get_setting("my_custom_setting");
  78. CHECK_EQ(variant.get_type(), Variant::NIL);
  79. ProjectSettings::get_singleton()->set_setting("my_custom_setting", true);
  80. CHECK(ProjectSettings::get_singleton()->has_setting("my_custom_setting"));
  81. variant = ProjectSettings::get_singleton()->get_setting("my_custom_setting");
  82. CHECK_EQ(variant.get_type(), Variant::BOOL);
  83. bool value = variant;
  84. CHECK_EQ(true, value);
  85. CHECK(ProjectSettings::get_singleton()->has_setting("my_custom_setting"));
  86. }
  87. TEST_CASE("[ProjectSettings] localize_path") {
  88. String old_resource_path = TestProjectSettingsInternalsAccessor::resource_path();
  89. TestProjectSettingsInternalsAccessor::resource_path() = DirAccess::create(DirAccess::ACCESS_FILESYSTEM)->get_current_dir();
  90. String root_path = ProjectSettings::get_singleton()->get_resource_path();
  91. #ifdef WINDOWS_ENABLED
  92. String root_path_win = ProjectSettings::get_singleton()->get_resource_path().replace("/", "\\");
  93. #endif
  94. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("filename"), "res://filename");
  95. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path/filename"), "res://path/filename");
  96. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path/something/../filename"), "res://path/filename");
  97. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path/./filename"), "res://path/filename");
  98. #ifdef WINDOWS_ENABLED
  99. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path\\filename"), "res://path/filename");
  100. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path\\something\\..\\filename"), "res://path/filename");
  101. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("path\\.\\filename"), "res://path/filename");
  102. #endif
  103. // FIXME?: These checks pass, but that doesn't seems correct
  104. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("../filename"), "res://filename");
  105. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("../path/filename"), "res://path/filename");
  106. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("..\\path\\filename"), "res://path/filename");
  107. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/filename"), "/testroot/filename");
  108. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/path/filename"), "/testroot/path/filename");
  109. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/path/something/../filename"), "/testroot/path/filename");
  110. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("/testroot/path/./filename"), "/testroot/path/filename");
  111. #ifdef WINDOWS_ENABLED
  112. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/filename"), "C:/testroot/filename");
  113. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/path/filename"), "C:/testroot/path/filename");
  114. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/path/something/../filename"), "C:/testroot/path/filename");
  115. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:/testroot/path/./filename"), "C:/testroot/path/filename");
  116. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\filename"), "C:/testroot/filename");
  117. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\path\\filename"), "C:/testroot/path/filename");
  118. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\path\\something\\..\\filename"), "C:/testroot/path/filename");
  119. CHECK_EQ(ProjectSettings::get_singleton()->localize_path("C:\\testroot\\path\\.\\filename"), "C:/testroot/path/filename");
  120. #endif
  121. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/filename"), "res://filename");
  122. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/path/filename"), "res://path/filename");
  123. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/path/something/../filename"), "res://path/filename");
  124. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path + "/path/./filename"), "res://path/filename");
  125. #ifdef WINDOWS_ENABLED
  126. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\filename"), "res://filename");
  127. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\path\\filename"), "res://path/filename");
  128. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\path\\something\\..\\filename"), "res://path/filename");
  129. CHECK_EQ(ProjectSettings::get_singleton()->localize_path(root_path_win + "\\path\\.\\filename"), "res://path/filename");
  130. #endif
  131. TestProjectSettingsInternalsAccessor::resource_path() = old_resource_path;
  132. }
  133. } // namespace TestProjectSettings
  134. #endif // TEST_PROJECT_SETTINGS_H