Tests.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2021, Itamar S. <itamar8910@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Tests.h"
  7. #include "../FileDB.h"
  8. #include "CppComprehensionEngine.h"
  9. #include <AK/LexicalPath.h>
  10. #include <LibCore/File.h>
  11. using namespace LanguageServers;
  12. using namespace LanguageServers::Cpp;
  13. static bool s_some_test_failed = false;
  14. #define I_TEST(name) \
  15. { \
  16. printf("Testing " #name "... "); \
  17. fflush(stdout); \
  18. }
  19. #define PASS \
  20. do { \
  21. printf("PASS\n"); \
  22. fflush(stdout); \
  23. return; \
  24. } while (0)
  25. #define FAIL(reason) \
  26. do { \
  27. printf("FAIL: " #reason "\n"); \
  28. s_some_test_failed = true; \
  29. return; \
  30. } while (0)
  31. constexpr char TESTS_ROOT_DIR[] = "/home/anon/cpp-tests/comprehension";
  32. static void test_complete_local_args();
  33. static void test_complete_local_vars();
  34. static void test_complete_type();
  35. static void test_find_variable_definition();
  36. static void test_complete_includes();
  37. static void test_parameters_hint();
  38. int run_tests()
  39. {
  40. test_complete_local_args();
  41. test_complete_local_vars();
  42. test_complete_type();
  43. test_find_variable_definition();
  44. test_complete_includes();
  45. test_parameters_hint();
  46. return s_some_test_failed ? 1 : 0;
  47. }
  48. static void add_file(FileDB& filedb, const String& name)
  49. {
  50. auto file = Core::File::open(LexicalPath::join(TESTS_ROOT_DIR, name).string(), Core::OpenMode::ReadOnly);
  51. VERIFY(!file.is_error());
  52. filedb.add(name, file.value()->fd());
  53. }
  54. void test_complete_local_args()
  55. {
  56. I_TEST(Complete Local Args)
  57. FileDB filedb;
  58. add_file(filedb, "complete_local_args.cpp");
  59. CppComprehensionEngine engine(filedb);
  60. auto suggestions = engine.get_suggestions("complete_local_args.cpp", { 2, 6 });
  61. if (suggestions.size() != 2)
  62. FAIL(bad size);
  63. if (suggestions[0].completion == "argc" && suggestions[1].completion == "argv")
  64. PASS;
  65. FAIL("wrong results");
  66. }
  67. void test_complete_local_vars()
  68. {
  69. I_TEST(Complete Local Vars)
  70. FileDB filedb;
  71. add_file(filedb, "complete_local_vars.cpp");
  72. CppComprehensionEngine autocomplete(filedb);
  73. auto suggestions = autocomplete.get_suggestions("complete_local_vars.cpp", { 3, 7 });
  74. if (suggestions.size() != 1)
  75. FAIL(bad size);
  76. if (suggestions[0].completion == "myvar1")
  77. PASS;
  78. FAIL("wrong results");
  79. }
  80. void test_complete_type()
  81. {
  82. I_TEST(Complete Type)
  83. FileDB filedb;
  84. add_file(filedb, "complete_type.cpp");
  85. CppComprehensionEngine autocomplete(filedb);
  86. auto suggestions = autocomplete.get_suggestions("complete_type.cpp", { 5, 7 });
  87. if (suggestions.size() != 1)
  88. FAIL(bad size);
  89. if (suggestions[0].completion == "MyStruct")
  90. PASS;
  91. FAIL("wrong results");
  92. }
  93. void test_find_variable_definition()
  94. {
  95. I_TEST(Find Variable Declaration)
  96. FileDB filedb;
  97. add_file(filedb, "find_variable_declaration.cpp");
  98. CppComprehensionEngine engine(filedb);
  99. auto position = engine.find_declaration_of("find_variable_declaration.cpp", { 2, 5 });
  100. if (!position.has_value())
  101. FAIL("declaration not found");
  102. if (position.value().file == "find_variable_declaration.cpp" && position.value().line == 0 && position.value().column >= 19)
  103. PASS;
  104. FAIL("wrong declaration location");
  105. }
  106. void test_complete_includes()
  107. {
  108. I_TEST(Complete Type)
  109. FileDB filedb;
  110. filedb.set_project_root(TESTS_ROOT_DIR);
  111. add_file(filedb, "complete_includes.cpp");
  112. add_file(filedb, "sample_header.h");
  113. CppComprehensionEngine autocomplete(filedb);
  114. auto suggestions = autocomplete.get_suggestions("complete_includes.cpp", { 0, 22 });
  115. if (suggestions.size() != 1)
  116. FAIL(project include - bad size);
  117. if (suggestions[0].completion != "sample_header.h")
  118. FAIL("project include - wrong results");
  119. suggestions = autocomplete.get_suggestions("complete_includes.cpp", { 1, 18 });
  120. if (suggestions.size() != 1)
  121. FAIL(global include - bad size);
  122. if (suggestions[0].completion != "cdefs.h")
  123. FAIL("global include - wrong results");
  124. PASS;
  125. }
  126. void test_parameters_hint()
  127. {
  128. I_TEST(Function Parameters hint)
  129. FileDB filedb;
  130. filedb.set_project_root(TESTS_ROOT_DIR);
  131. add_file(filedb, "parameters_hint1.cpp");
  132. CppComprehensionEngine engine(filedb);
  133. auto result = engine.get_function_params_hint("parameters_hint1.cpp", { 4, 9 });
  134. if (!result.has_value())
  135. FAIL("failed to get parameters hint (1)");
  136. if (result->params != Vector<String> { "int x", "char y" } || result->current_index != 0)
  137. FAIL("bad result (1)");
  138. result = engine.get_function_params_hint("parameters_hint1.cpp", { 5, 15 });
  139. if (!result.has_value())
  140. FAIL("failed to get parameters hint (2)");
  141. if (result->params != Vector<String> { "int x", "char y" } || result->current_index != 1)
  142. FAIL("bad result (2)");
  143. result = engine.get_function_params_hint("parameters_hint1.cpp", { 6, 8 });
  144. if (!result.has_value())
  145. FAIL("failed to get parameters hint (3)");
  146. if (result->params != Vector<String> { "int x", "char y" } || result->current_index != 0)
  147. FAIL("bad result (3)");
  148. PASS;
  149. }