test_lsp.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /**************************************************************************/
  2. /* test_lsp.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. #ifdef TOOLS_ENABLED
  32. #include "modules/modules_enabled.gen.h" // For jsonrpc.
  33. #ifdef MODULE_JSONRPC_ENABLED
  34. #include "tests/test_macros.h"
  35. #include "../language_server/gdscript_extend_parser.h"
  36. #include "../language_server/gdscript_language_protocol.h"
  37. #include "../language_server/gdscript_workspace.h"
  38. #include "../language_server/godot_lsp.h"
  39. #include "core/io/dir_access.h"
  40. #include "core/io/file_access_pack.h"
  41. #include "core/os/os.h"
  42. #include "editor/doc/editor_help.h"
  43. #include "editor/editor_node.h"
  44. #include "modules/gdscript/gdscript_analyzer.h"
  45. #include "modules/regex/regex.h"
  46. #include "thirdparty/doctest/doctest.h"
  47. class TestGDScriptLanguageProtocolInitializer {
  48. public:
  49. static void setup_client() {
  50. GDScriptLanguageProtocol *proto = GDScriptLanguageProtocol::get_singleton();
  51. Ref<GDScriptLanguageProtocol::LSPeer> peer = memnew(GDScriptLanguageProtocol::LSPeer);
  52. proto->clients.insert(proto->next_client_id, peer);
  53. proto->latest_client_id = proto->next_client_id;
  54. proto->next_client_id++;
  55. }
  56. };
  57. template <>
  58. struct doctest::StringMaker<LSP::Position> {
  59. static doctest::String convert(const LSP::Position &p_val) {
  60. return p_val.to_string().utf8().get_data();
  61. }
  62. };
  63. template <>
  64. struct doctest::StringMaker<LSP::Range> {
  65. static doctest::String convert(const LSP::Range &p_val) {
  66. return p_val.to_string().utf8().get_data();
  67. }
  68. };
  69. template <>
  70. struct doctest::StringMaker<GodotPosition> {
  71. static doctest::String convert(const GodotPosition &p_val) {
  72. return p_val.to_string().utf8().get_data();
  73. }
  74. };
  75. namespace GDScriptTests {
  76. // LSP GDScript test scripts are located inside project of other GDScript tests:
  77. // Cannot reset `ProjectSettings` (singleton) -> Cannot load another workspace and resources in there.
  78. // -> Reuse GDScript test project. LSP specific scripts are then placed inside `lsp` folder.
  79. // Access via `res://lsp/my_script.gd`.
  80. const String root = "modules/gdscript/tests/scripts/";
  81. /*
  82. * After use:
  83. * * `memdelete` returned `GDScriptLanguageProtocol`.
  84. * * Call `GDScriptTests::::finish_language`.
  85. */
  86. GDScriptLanguageProtocol *initialize(const String &p_root) {
  87. Error err = OK;
  88. Ref<DirAccess> dir(DirAccess::open(p_root, &err));
  89. REQUIRE_MESSAGE(err == OK, "Could not open specified root directory");
  90. String absolute_root = dir->get_current_dir();
  91. init_language(absolute_root);
  92. GDScriptLanguageProtocol *proto = memnew(GDScriptLanguageProtocol);
  93. TestGDScriptLanguageProtocolInitializer::setup_client();
  94. Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
  95. workspace->root = absolute_root;
  96. // On windows: `C:/...` -> `C%3A/...`.
  97. workspace->root_uri = "file:///" + absolute_root.lstrip("/").replace_first(":", "%3A");
  98. return proto;
  99. }
  100. LSP::Position pos(const int p_line, const int p_character) {
  101. LSP::Position p;
  102. p.line = p_line;
  103. p.character = p_character;
  104. return p;
  105. }
  106. LSP::Range range(const LSP::Position p_start, const LSP::Position p_end) {
  107. LSP::Range r;
  108. r.start = p_start;
  109. r.end = p_end;
  110. return r;
  111. }
  112. LSP::TextDocumentPositionParams pos_in(const LSP::DocumentUri &p_uri, const LSP::Position p_pos) {
  113. LSP::TextDocumentPositionParams params;
  114. params.textDocument.uri = p_uri;
  115. params.position = p_pos;
  116. return params;
  117. }
  118. const LSP::DocumentSymbol *test_resolve_symbol_at(const String &p_uri, const LSP::Position p_pos, const String &p_expected_uri, const String &p_expected_name, const LSP::Range &p_expected_range) {
  119. Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
  120. LSP::TextDocumentPositionParams params = pos_in(p_uri, p_pos);
  121. const LSP::DocumentSymbol *symbol = workspace->resolve_symbol(params);
  122. CHECK(symbol);
  123. if (symbol) {
  124. CHECK_EQ(symbol->uri, p_expected_uri);
  125. CHECK_EQ(symbol->name, p_expected_name);
  126. CHECK_EQ(symbol->selectionRange, p_expected_range);
  127. }
  128. return symbol;
  129. }
  130. struct InlineTestData {
  131. LSP::Range range;
  132. String text;
  133. String name;
  134. String ref;
  135. static bool try_parse(const Vector<String> &p_lines, const int p_line_number, InlineTestData &r_data) {
  136. String line = p_lines[p_line_number];
  137. RegEx regex = RegEx("^\\t*#[ |]*(?<range>(?<left><)?\\^+)(\\s+(?<name>(?!->)\\S+))?(\\s+->\\s+(?<ref>\\S+))?");
  138. Ref<RegExMatch> match = regex.search(line);
  139. if (match.is_null()) {
  140. return false;
  141. }
  142. // Find first line without leading comment above current line.
  143. int target_line = p_line_number;
  144. while (target_line >= 0) {
  145. String dedented = p_lines[target_line].lstrip("\t");
  146. if (!dedented.begins_with("#")) {
  147. break;
  148. }
  149. target_line--;
  150. }
  151. if (target_line < 0) {
  152. return false;
  153. }
  154. r_data.range.start.line = r_data.range.end.line = target_line;
  155. String marker = match->get_string("range");
  156. int i = line.find(marker);
  157. REQUIRE(i >= 0);
  158. r_data.range.start.character = i;
  159. if (!match->get_string("left").is_empty()) {
  160. // Include `#` (comment char) in range.
  161. r_data.range.start.character--;
  162. }
  163. r_data.range.end.character = i + marker.length();
  164. String target = p_lines[target_line];
  165. r_data.text = target.substr(r_data.range.start.character, r_data.range.end.character - r_data.range.start.character);
  166. r_data.name = match->get_string("name");
  167. r_data.ref = match->get_string("ref");
  168. return true;
  169. }
  170. };
  171. Vector<InlineTestData> read_tests(const String &p_path) {
  172. Error err;
  173. String source = FileAccess::get_file_as_string(p_path, &err);
  174. REQUIRE_MESSAGE(err == OK, vformat("Cannot read '%s'", p_path));
  175. // Format:
  176. // ```gdscript
  177. // var foo = bar + baz
  178. // # | | | | ^^^ name -> ref
  179. // # | | ^^^ -> ref
  180. // # ^^^ name
  181. //
  182. // func my_func():
  183. // # ^^^^^^^ name
  184. // var value = foo + 42
  185. // # ^^^^^ name
  186. // print(value)
  187. // # ^^^^^ -> ref
  188. // ```
  189. //
  190. // * `^`: Range marker.
  191. // * `name`: Unique name. Can contain any characters except whitespace chars.
  192. // * `ref`: Reference to unique name.
  193. //
  194. // Notes:
  195. // * If range should include first content-char (which is occupied by `#`): use `<` for next marker.
  196. // -> Range expands 1 to left (-> includes `#`).
  197. // * Note: Means: Range cannot be single char directly marked by `#`, but must be at least two chars (marked with `#<`).
  198. // * Comment must start at same ident as line its marked (-> because of tab alignment...).
  199. // * Use spaces to align after `#`! -> for correct alignment
  200. // * Between `#` and `^` can be spaces or `|` (to better visualize what's marked below).
  201. PackedStringArray lines = source.split("\n");
  202. PackedStringArray names;
  203. Vector<InlineTestData> data;
  204. for (int i = 0; i < lines.size(); i++) {
  205. InlineTestData d;
  206. if (InlineTestData::try_parse(lines, i, d)) {
  207. if (!d.name.is_empty()) {
  208. // Safety check: names must be unique.
  209. if (names.has(d.name)) {
  210. FAIL(vformat("Duplicated name '%s' in '%s'. Names must be unique!", d.name, p_path));
  211. }
  212. names.append(d.name);
  213. }
  214. data.append(d);
  215. }
  216. }
  217. return data;
  218. }
  219. void test_resolve_symbol(const String &p_uri, const InlineTestData &p_test_data, const Vector<InlineTestData> &p_all_data) {
  220. if (p_test_data.ref.is_empty()) {
  221. return;
  222. }
  223. SUBCASE(vformat("Can resolve symbol '%s' at %s to '%s'", p_test_data.text, p_test_data.range.to_string(), p_test_data.ref).utf8().get_data()) {
  224. const InlineTestData *target = nullptr;
  225. for (int i = 0; i < p_all_data.size(); i++) {
  226. if (p_all_data[i].name == p_test_data.ref) {
  227. target = &p_all_data[i];
  228. break;
  229. }
  230. }
  231. REQUIRE_MESSAGE(target, vformat("No target for ref '%s'", p_test_data.ref));
  232. Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
  233. LSP::Position pos = p_test_data.range.start;
  234. SUBCASE("start of identifier") {
  235. pos.character = p_test_data.range.start.character;
  236. test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
  237. }
  238. SUBCASE("inside identifier") {
  239. pos.character = (p_test_data.range.end.character + p_test_data.range.start.character) / 2;
  240. test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
  241. }
  242. SUBCASE("end of identifier") {
  243. pos.character = p_test_data.range.end.character;
  244. test_resolve_symbol_at(p_uri, pos, p_uri, target->text, target->range);
  245. }
  246. }
  247. }
  248. Vector<InlineTestData> filter_ref_towards(const Vector<InlineTestData> &p_data, const String &p_name) {
  249. Vector<InlineTestData> res;
  250. for (const InlineTestData &d : p_data) {
  251. if (d.ref == p_name) {
  252. res.append(d);
  253. }
  254. }
  255. return res;
  256. }
  257. void test_resolve_symbols(const String &p_uri, const Vector<InlineTestData> &p_test_data, const Vector<InlineTestData> &p_all_data) {
  258. for (const InlineTestData &d : p_test_data) {
  259. test_resolve_symbol(p_uri, d, p_all_data);
  260. }
  261. }
  262. void assert_no_errors_in(const String &p_path) {
  263. Error err;
  264. String source = FileAccess::get_file_as_string(p_path, &err);
  265. REQUIRE_MESSAGE(err == OK, vformat("Cannot read '%s'", p_path));
  266. GDScriptParser parser;
  267. err = parser.parse(source, p_path, true);
  268. REQUIRE_MESSAGE(err == OK, vformat("Errors while parsing '%s'", p_path));
  269. GDScriptAnalyzer analyzer(&parser);
  270. err = analyzer.analyze();
  271. REQUIRE_MESSAGE(err == OK, vformat("Errors while analyzing '%s'", p_path));
  272. }
  273. inline LSP::Position lsp_pos(int line, int character) {
  274. LSP::Position p;
  275. p.line = line;
  276. p.character = character;
  277. return p;
  278. }
  279. void test_position_roundtrip(LSP::Position p_lsp, GodotPosition p_gd, const PackedStringArray &p_lines) {
  280. GodotPosition actual_gd = GodotPosition::from_lsp(p_lsp, p_lines);
  281. CHECK_EQ(p_gd, actual_gd);
  282. LSP::Position actual_lsp = p_gd.to_lsp(p_lines);
  283. CHECK_EQ(p_lsp, actual_lsp);
  284. }
  285. // Note:
  286. // * Cursor is BETWEEN chars
  287. // * `va|r` -> cursor between `a`&`r`
  288. // * `var`
  289. // ^
  290. // -> Character on `r` -> cursor between `a`&`r`s for tests:
  291. // * Line & Char:
  292. // * LSP: both 0-based
  293. // * Godot: both 1-based
  294. TEST_SUITE("[Modules][GDScript][LSP][Editor]") {
  295. TEST_CASE("Can convert positions to and from Godot") {
  296. String code = R"(extends Node
  297. var member := 42
  298. func f():
  299. var value := 42
  300. return value + member)";
  301. PackedStringArray lines = code.split("\n");
  302. SUBCASE("line after end") {
  303. LSP::Position lsp = lsp_pos(7, 0);
  304. GodotPosition gd(8, 1);
  305. test_position_roundtrip(lsp, gd, lines);
  306. }
  307. SUBCASE("first char in first line") {
  308. LSP::Position lsp = lsp_pos(0, 0);
  309. GodotPosition gd(1, 1);
  310. test_position_roundtrip(lsp, gd, lines);
  311. }
  312. SUBCASE("with tabs") {
  313. // On `v` in `value` in `var value := ...`.
  314. LSP::Position lsp = lsp_pos(5, 6);
  315. GodotPosition gd(6, 13);
  316. test_position_roundtrip(lsp, gd, lines);
  317. }
  318. SUBCASE("doesn't fail with column outside of character length") {
  319. LSP::Position lsp = lsp_pos(2, 100);
  320. GodotPosition::from_lsp(lsp, lines);
  321. GodotPosition gd(3, 100);
  322. gd.to_lsp(lines);
  323. }
  324. SUBCASE("doesn't fail with line outside of line length") {
  325. LSP::Position lsp = lsp_pos(200, 100);
  326. GodotPosition::from_lsp(lsp, lines);
  327. GodotPosition gd(300, 100);
  328. gd.to_lsp(lines);
  329. }
  330. SUBCASE("special case: zero column for root class") {
  331. GodotPosition gd(1, 0);
  332. LSP::Position expected = lsp_pos(0, 0);
  333. LSP::Position actual = gd.to_lsp(lines);
  334. CHECK_EQ(actual, expected);
  335. }
  336. SUBCASE("special case: zero line and column for root class") {
  337. GodotPosition gd(0, 0);
  338. LSP::Position expected = lsp_pos(0, 0);
  339. LSP::Position actual = gd.to_lsp(lines);
  340. CHECK_EQ(actual, expected);
  341. }
  342. SUBCASE("special case: negative line for root class") {
  343. GodotPosition gd(-1, 0);
  344. LSP::Position expected = lsp_pos(0, 0);
  345. LSP::Position actual = gd.to_lsp(lines);
  346. CHECK_EQ(actual, expected);
  347. }
  348. SUBCASE("special case: lines.length() + 1 for root class") {
  349. GodotPosition gd(lines.size() + 1, 0);
  350. LSP::Position expected = lsp_pos(lines.size(), 0);
  351. LSP::Position actual = gd.to_lsp(lines);
  352. CHECK_EQ(actual, expected);
  353. }
  354. }
  355. TEST_CASE("[workspace][resolve_symbol]") {
  356. EditorFileSystem *efs = memnew(EditorFileSystem);
  357. GDScriptLanguageProtocol *proto = initialize(root);
  358. REQUIRE(proto);
  359. Ref<GDScriptWorkspace> workspace = GDScriptLanguageProtocol::get_singleton()->get_workspace();
  360. {
  361. String path = "res://lsp/local_variables.gd";
  362. assert_no_errors_in(path);
  363. String uri = workspace->get_file_uri(path);
  364. Vector<InlineTestData> all_test_data = read_tests(path);
  365. SUBCASE("Can get correct ranges for public variables") {
  366. Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "member");
  367. test_resolve_symbols(uri, test_data, all_test_data);
  368. }
  369. SUBCASE("Can get correct ranges for local variables") {
  370. Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "test");
  371. test_resolve_symbols(uri, test_data, all_test_data);
  372. }
  373. SUBCASE("Can get correct ranges for local parameters") {
  374. Vector<InlineTestData> test_data = filter_ref_towards(all_test_data, "arg");
  375. test_resolve_symbols(uri, test_data, all_test_data);
  376. }
  377. }
  378. SUBCASE("Can get correct ranges for indented variables") {
  379. String path = "res://lsp/indentation.gd";
  380. assert_no_errors_in(path);
  381. String uri = workspace->get_file_uri(path);
  382. Vector<InlineTestData> all_test_data = read_tests(path);
  383. test_resolve_symbols(uri, all_test_data, all_test_data);
  384. }
  385. SUBCASE("Can get correct ranges for scopes") {
  386. String path = "res://lsp/scopes.gd";
  387. assert_no_errors_in(path);
  388. String uri = workspace->get_file_uri(path);
  389. Vector<InlineTestData> all_test_data = read_tests(path);
  390. test_resolve_symbols(uri, all_test_data, all_test_data);
  391. }
  392. SUBCASE("Can get correct ranges for lambda") {
  393. String path = "res://lsp/lambdas.gd";
  394. assert_no_errors_in(path);
  395. String uri = workspace->get_file_uri(path);
  396. Vector<InlineTestData> all_test_data = read_tests(path);
  397. test_resolve_symbols(uri, all_test_data, all_test_data);
  398. }
  399. SUBCASE("Can get correct ranges for inner class") {
  400. String path = "res://lsp/class.gd";
  401. assert_no_errors_in(path);
  402. String uri = workspace->get_file_uri(path);
  403. Vector<InlineTestData> all_test_data = read_tests(path);
  404. test_resolve_symbols(uri, all_test_data, all_test_data);
  405. }
  406. SUBCASE("Can get correct ranges for inner class") {
  407. String path = "res://lsp/enums.gd";
  408. assert_no_errors_in(path);
  409. String uri = workspace->get_file_uri(path);
  410. Vector<InlineTestData> all_test_data = read_tests(path);
  411. test_resolve_symbols(uri, all_test_data, all_test_data);
  412. }
  413. SUBCASE("Can get correct ranges for shadowing & shadowed variables") {
  414. String path = "res://lsp/shadowing_initializer.gd";
  415. assert_no_errors_in(path);
  416. String uri = workspace->get_file_uri(path);
  417. Vector<InlineTestData> all_test_data = read_tests(path);
  418. test_resolve_symbols(uri, all_test_data, all_test_data);
  419. }
  420. SUBCASE("Can get correct ranges for properties and getter/setter") {
  421. String path = "res://lsp/properties.gd";
  422. assert_no_errors_in(path);
  423. String uri = workspace->get_file_uri(path);
  424. Vector<InlineTestData> all_test_data = read_tests(path);
  425. test_resolve_symbols(uri, all_test_data, all_test_data);
  426. }
  427. memdelete(proto);
  428. memdelete(efs);
  429. finish_language();
  430. }
  431. TEST_CASE("[workspace][document_symbol]") {
  432. EditorFileSystem *efs = memnew(EditorFileSystem);
  433. GDScriptLanguageProtocol *proto = initialize(root);
  434. REQUIRE(proto);
  435. SUBCASE("selectionRange of root class must be inside range") {
  436. LocalVector<String> paths = {
  437. "res://lsp/first_line_comment.gd", // Comment on first line
  438. "res://lsp/first_line_class_name.gd", // class_name (and thus selection range) before extends
  439. };
  440. for (const String &path : paths) {
  441. assert_no_errors_in(path);
  442. ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(path);
  443. REQUIRE(parser);
  444. LSP::DocumentSymbol cls = parser->get_symbols();
  445. REQUIRE(((cls.range.start.line == cls.selectionRange.start.line && cls.range.start.character <= cls.selectionRange.start.character) || (cls.range.start.line < cls.selectionRange.start.line)));
  446. REQUIRE(((cls.range.end.line == cls.selectionRange.end.line && cls.range.end.character >= cls.selectionRange.end.character) || (cls.range.end.line > cls.selectionRange.end.line)));
  447. }
  448. }
  449. SUBCASE("Documentation is correctly set") {
  450. String path = "res://lsp/doc_comments.gd";
  451. assert_no_errors_in(path);
  452. ExtendGDScriptParser *parser = GDScriptLanguageProtocol::get_singleton()->get_parse_result(path);
  453. REQUIRE(parser);
  454. LSP::DocumentSymbol cls = parser->get_symbols();
  455. REQUIRE(cls.documentation.contains("brief"));
  456. REQUIRE(cls.documentation.contains("description"));
  457. REQUIRE(cls.documentation.contains("t1"));
  458. REQUIRE(cls.documentation.contains("t2"));
  459. REQUIRE(cls.documentation.contains("t3"));
  460. }
  461. memdelete(proto);
  462. memdelete(efs);
  463. finish_language();
  464. }
  465. TEST_CASE("BBCode to markdown conversion") {
  466. // This tests the conversion from BBCode docstrings to the markdown markup sent to
  467. // the LSP client on documentation requests
  468. // Basic formatting
  469. CHECK_EQ(LSP::marked_documentation("[b]bold[/b]"), "**bold**");
  470. CHECK_EQ(LSP::marked_documentation("[i]italic[/i]"), "*italic*");
  471. CHECK_EQ(LSP::marked_documentation("[u]underline[/u]"), "__underline__");
  472. CHECK_EQ(LSP::marked_documentation("[s]strikethrough[/s]"), "~~strikethrough~~");
  473. CHECK_EQ(LSP::marked_documentation("[code]code[/code]"), "`code`");
  474. CHECK_EQ(LSP::marked_documentation("[kbd]Ctrl + S[/kbd]"), "`Ctrl + S`");
  475. // Line breaks. We insert paragraphs for [br] because the BBCode to
  476. // markdown conversion function simply makes the conversion line-wise and
  477. // we don't distinguish markdown inline elements and blocks.
  478. CHECK_EQ(LSP::marked_documentation("Line1[br]Line2"), "Line1\n\nLine2");
  479. // These tags (center, color, font) aren't supported in markdown and should be stripped.
  480. CHECK_EQ(LSP::marked_documentation("[center]Centered text[/center]"), "Centered text");
  481. CHECK_EQ(LSP::marked_documentation("[color=red]red text[/color]"), "red text");
  482. CHECK_EQ(LSP::marked_documentation("[font=Arial]Arial text[/font]"), "Arial text");
  483. // The following tests are for all the link patterns specific to Godot's built-in docs that we render as inline code.
  484. CHECK_EQ(LSP::marked_documentation("Class link: [Node2D], [Sprite2D]"), "Class link: `Node2D`, `Sprite2D`");
  485. CHECK_EQ(LSP::marked_documentation("Single class [RigidBody2D]"), "Single class `RigidBody2D`");
  486. CHECK_EQ(LSP::marked_documentation("[method Node2D.set_position]"), "`Node2D.set_position`");
  487. CHECK_EQ(LSP::marked_documentation("[member Node2D.position]"), "`Node2D.position`");
  488. CHECK_EQ(LSP::marked_documentation("[signal Node.ready]"), "`Node.ready`");
  489. CHECK_EQ(LSP::marked_documentation("[constant Color.RED]"), "`Color.RED`");
  490. CHECK_EQ(LSP::marked_documentation("[enum Node.ProcessMode]"), "`Node.ProcessMode`");
  491. CHECK_EQ(LSP::marked_documentation("[annotation @GDScript.@export]"), "`@GDScript.@export`");
  492. CHECK_EQ(LSP::marked_documentation("[constructor Vector2.Vector2]"), "`Vector2.Vector2`");
  493. CHECK_EQ(LSP::marked_documentation("[operator Vector2.operator +]"), "`Vector2.operator +`");
  494. CHECK_EQ(LSP::marked_documentation("[theme_item Button.font]"), "`Button.font`");
  495. CHECK_EQ(LSP::marked_documentation("[param delta]"), "`delta`");
  496. // Markdown links
  497. CHECK_EQ(LSP::marked_documentation("[url=https://godotengine.org]link to Godot Engine[/url]"),
  498. "[link to Godot Engine](https://godotengine.org)");
  499. CHECK_EQ(LSP::marked_documentation("[url]https://godotengine.org/[/url]"),
  500. "[https://godotengine.org/](https://godotengine.org/)");
  501. // Code listings
  502. CHECK_EQ(LSP::marked_documentation("[codeblock]\nfunc test():\n print(\"Hello, Godot!\")\n[/codeblock]"),
  503. "```gdscript\nfunc test():\n print(\"Hello, Godot!\")\n```");
  504. CHECK_EQ(LSP::marked_documentation("[codeblock lang=csharp]\npublic void Test()\n{\n GD.Print(\"Hello, Godot!\");\n}\n[/codeblock]"),
  505. "```csharp\npublic void Test()\n{\n GD.Print(\"Hello, Godot!\");\n}\n```");
  506. // Code listings with multiple languages (the codeblocks tag is used in the built-in reference)
  507. // When [codeblocks] is used, we only convert the [gdscript] tag to a code block like the built-in editor.
  508. // NOTE: There is always a GDScript code listing in the built-in class reference.
  509. CHECK_EQ(LSP::marked_documentation("[codeblocks]\n[gdscript]\nprint(hash(\"a\")) # Prints 177670\n[/gdscript]\n[csharp]\nGD.Print(GD.Hash(\"a\")); // Prints 177670\n[/csharp]\n[/codeblocks]"),
  510. "```gdscript\nprint(hash(\"a\")) # Prints 177670\n```\n");
  511. // lb and rb are used to insert literal square brackets in markdown.
  512. CHECK_EQ(LSP::marked_documentation("[lb]literal brackets[rb]"), "\\[literal brackets\\]");
  513. CHECK_EQ(LSP::marked_documentation("[lb]literal[rb] with [ClassName]"), "\\[literal\\] with `ClassName`");
  514. // We have to be careful that different patterns don't conflict with each
  515. // other, especially with urls that use brackets in markdown.
  516. CHECK_EQ(LSP::marked_documentation("Class [Sprite2D] with [url=https://godotengine.org]link[/url]"),
  517. "Class `Sprite2D` with [link](https://godotengine.org)");
  518. }
  519. }
  520. } // namespace GDScriptTests
  521. #endif // MODULE_JSONRPC_ENABLED
  522. #endif // TOOLS_ENABLED