test_completion.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**************************************************************************/
  2. /* test_completion.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_COMPLETION_H
  31. #define TEST_COMPLETION_H
  32. #ifdef TOOLS_ENABLED
  33. #include "tests/test_macros.h"
  34. #include "../gdscript.h"
  35. #include "gdscript_test_runner.h"
  36. #include "core/config/project_settings.h"
  37. #include "core/io/config_file.h"
  38. #include "core/io/dir_access.h"
  39. #include "core/io/file_access.h"
  40. #include "core/object/script_language.h"
  41. #include "core/variant/dictionary.h"
  42. #include "core/variant/variant.h"
  43. #include "editor/editor_settings.h"
  44. #include "scene/resources/packed_scene.h"
  45. #include "scene/theme/theme_db.h"
  46. #include "modules/modules_enabled.gen.h" // For mono.
  47. namespace GDScriptTests {
  48. static bool match_option(const Dictionary p_expected, const ScriptLanguage::CodeCompletionOption p_got) {
  49. if (p_expected.get("display", p_got.display) != p_got.display) {
  50. return false;
  51. }
  52. if (p_expected.get("insert_text", p_got.insert_text) != p_got.insert_text) {
  53. return false;
  54. }
  55. if (p_expected.get("kind", p_got.kind) != Variant(p_got.kind)) {
  56. return false;
  57. }
  58. if (p_expected.get("location", p_got.location) != Variant(p_got.location)) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. static void to_dict_list(Variant p_variant, List<Dictionary> &p_list) {
  64. ERR_FAIL_COND(!p_variant.is_array());
  65. Array arr = p_variant;
  66. for (int i = 0; i < arr.size(); i++) {
  67. if (arr[i].get_type() == Variant::DICTIONARY) {
  68. p_list.push_back(arr[i]);
  69. }
  70. }
  71. }
  72. static void test_directory(const String &p_dir) {
  73. Error err = OK;
  74. Ref<DirAccess> dir = DirAccess::open(p_dir, &err);
  75. if (err != OK) {
  76. FAIL("Invalid test directory.");
  77. return;
  78. }
  79. String path = dir->get_current_dir();
  80. dir->list_dir_begin();
  81. String next = dir->get_next();
  82. while (!next.is_empty()) {
  83. if (dir->current_is_dir()) {
  84. if (next == "." || next == "..") {
  85. next = dir->get_next();
  86. continue;
  87. }
  88. test_directory(path.path_join(next));
  89. } else if (next.ends_with(".gd") && !next.ends_with(".notest.gd")) {
  90. Ref<FileAccess> acc = FileAccess::open(path.path_join(next), FileAccess::READ, &err);
  91. if (err != OK) {
  92. next = dir->get_next();
  93. continue;
  94. }
  95. String code = acc->get_as_utf8_string();
  96. // For ease of reading ➡ (0x27A1) acts as sentinel char instead of 0xFFFF in the files.
  97. code = code.replace_first(String::chr(0x27A1), String::chr(0xFFFF));
  98. // Require pointer sentinel char in scripts.
  99. int location = code.find_char(0xFFFF);
  100. CHECK(location != -1);
  101. String res_path = ProjectSettings::get_singleton()->localize_path(path.path_join(next));
  102. ConfigFile conf;
  103. if (conf.load(path.path_join(next.get_basename() + ".cfg")) != OK) {
  104. FAIL("No config file found.");
  105. }
  106. #ifndef MODULE_MONO_ENABLED
  107. if (conf.get_value("input", "cs", false)) {
  108. next = dir->get_next();
  109. continue;
  110. }
  111. #endif
  112. EditorSettings::get_singleton()->set_setting("text_editor/completion/use_single_quotes", conf.get_value("input", "use_single_quotes", false));
  113. EditorSettings::get_singleton()->set_setting("text_editor/completion/add_node_path_literals", conf.get_value("input", "add_node_path_literals", false));
  114. EditorSettings::get_singleton()->set_setting("text_editor/completion/add_string_name_literals", conf.get_value("input", "add_string_name_literals", false));
  115. List<Dictionary> include;
  116. to_dict_list(conf.get_value("output", "include", Array()), include);
  117. List<Dictionary> exclude;
  118. to_dict_list(conf.get_value("output", "exclude", Array()), exclude);
  119. List<ScriptLanguage::CodeCompletionOption> options;
  120. String call_hint;
  121. bool forced;
  122. Node *scene = nullptr;
  123. if (conf.has_section_key("input", "scene")) {
  124. Ref<PackedScene> packed_scene = ResourceLoader::load(conf.get_value("input", "scene"), "PackedScene", ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP);
  125. if (packed_scene.is_valid()) {
  126. scene = packed_scene->instantiate();
  127. }
  128. } else if (dir->file_exists(next.get_basename() + ".tscn")) {
  129. Ref<PackedScene> packed_scene = ResourceLoader::load(path.path_join(next.get_basename() + ".tscn"), "PackedScene");
  130. if (packed_scene.is_valid()) {
  131. scene = packed_scene->instantiate();
  132. }
  133. }
  134. Node *owner = nullptr;
  135. if (scene != nullptr) {
  136. owner = scene->get_node(conf.get_value("input", "node_path", "."));
  137. }
  138. if (owner != nullptr) {
  139. // Remove the line which contains the sentinel char, to get a valid script.
  140. Ref<GDScript> scr;
  141. scr.instantiate();
  142. int start = location;
  143. int end = location;
  144. for (; start >= 0; --start) {
  145. if (code.get(start) == '\n') {
  146. break;
  147. }
  148. }
  149. for (; end < code.size(); ++end) {
  150. if (code.get(end) == '\n') {
  151. break;
  152. }
  153. }
  154. scr->set_source_code(code.erase(start, end - start));
  155. scr->reload();
  156. scr->set_path(res_path);
  157. owner->set_script(scr);
  158. }
  159. GDScriptLanguage::get_singleton()->complete_code(code, res_path, owner, &options, forced, call_hint);
  160. String contains_excluded;
  161. for (ScriptLanguage::CodeCompletionOption &option : options) {
  162. for (const Dictionary &E : exclude) {
  163. if (match_option(E, option)) {
  164. contains_excluded = option.display;
  165. break;
  166. }
  167. }
  168. if (!contains_excluded.is_empty()) {
  169. break;
  170. }
  171. for (const Dictionary &E : include) {
  172. if (match_option(E, option)) {
  173. include.erase(E);
  174. break;
  175. }
  176. }
  177. }
  178. CHECK_MESSAGE(contains_excluded.is_empty(), "Autocompletion suggests illegal option '", contains_excluded, "' for '", path.path_join(next), "'.");
  179. CHECK(include.is_empty());
  180. String expected_call_hint = conf.get_value("output", "call_hint", call_hint);
  181. bool expected_forced = conf.get_value("output", "forced", forced);
  182. CHECK(expected_call_hint == call_hint);
  183. CHECK(expected_forced == forced);
  184. if (scene) {
  185. memdelete(scene);
  186. }
  187. }
  188. next = dir->get_next();
  189. }
  190. }
  191. TEST_SUITE("[Modules][GDScript][Completion]") {
  192. TEST_CASE("[Editor] Check suggestion list") {
  193. // Set all editor settings that code completion relies on.
  194. EditorSettings::get_singleton()->set_setting("text_editor/completion/use_single_quotes", false);
  195. init_language("modules/gdscript/tests/scripts");
  196. test_directory("modules/gdscript/tests/scripts/completion");
  197. }
  198. }
  199. } // namespace GDScriptTests
  200. #endif
  201. #endif // TEST_COMPLETION_H