Tests.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. int run_tests()
  38. {
  39. test_complete_local_args();
  40. test_complete_local_vars();
  41. test_complete_type();
  42. test_find_variable_definition();
  43. test_complete_includes();
  44. return s_some_test_failed ? 1 : 0;
  45. }
  46. static void add_file(FileDB& filedb, const String& name)
  47. {
  48. auto file = Core::File::open(LexicalPath::join(TESTS_ROOT_DIR, name).string(), Core::OpenMode::ReadOnly);
  49. VERIFY(!file.is_error());
  50. filedb.add(name, file.value()->fd());
  51. }
  52. void test_complete_local_args()
  53. {
  54. I_TEST(Complete Local Args)
  55. FileDB filedb;
  56. add_file(filedb, "complete_local_args.cpp");
  57. CppComprehensionEngine engine(filedb);
  58. auto suggestions = engine.get_suggestions("complete_local_args.cpp", { 2, 6 });
  59. if (suggestions.size() != 2)
  60. FAIL(bad size);
  61. if (suggestions[0].completion == "argc" && suggestions[1].completion == "argv")
  62. PASS;
  63. FAIL("wrong results");
  64. }
  65. void test_complete_local_vars()
  66. {
  67. I_TEST(Complete Local Vars)
  68. FileDB filedb;
  69. add_file(filedb, "complete_local_vars.cpp");
  70. CppComprehensionEngine autocomplete(filedb);
  71. auto suggestions = autocomplete.get_suggestions("complete_local_vars.cpp", { 3, 7 });
  72. if (suggestions.size() != 1)
  73. FAIL(bad size);
  74. if (suggestions[0].completion == "myvar1")
  75. PASS;
  76. FAIL("wrong results");
  77. }
  78. void test_complete_type()
  79. {
  80. I_TEST(Complete Type)
  81. FileDB filedb;
  82. add_file(filedb, "complete_type.cpp");
  83. CppComprehensionEngine autocomplete(filedb);
  84. auto suggestions = autocomplete.get_suggestions("complete_type.cpp", { 5, 7 });
  85. if (suggestions.size() != 1)
  86. FAIL(bad size);
  87. if (suggestions[0].completion == "MyStruct")
  88. PASS;
  89. FAIL("wrong results");
  90. }
  91. void test_find_variable_definition()
  92. {
  93. I_TEST(Find Variable Declaration)
  94. FileDB filedb;
  95. add_file(filedb, "find_variable_declaration.cpp");
  96. CppComprehensionEngine engine(filedb);
  97. auto position = engine.find_declaration_of("find_variable_declaration.cpp", { 2, 5 });
  98. if (!position.has_value())
  99. FAIL("declaration not found");
  100. if (position.value().file == "find_variable_declaration.cpp" && position.value().line == 0 && position.value().column >= 19)
  101. PASS;
  102. FAIL("wrong declaration location");
  103. }
  104. void test_complete_includes()
  105. {
  106. I_TEST(Complete Type)
  107. FileDB filedb;
  108. filedb.set_project_root(TESTS_ROOT_DIR);
  109. add_file(filedb, "complete_includes.cpp");
  110. add_file(filedb, "sample_header.h");
  111. CppComprehensionEngine autocomplete(filedb);
  112. auto suggestions = autocomplete.get_suggestions("complete_includes.cpp", { 0, 23 });
  113. if (suggestions.size() != 1)
  114. FAIL(project include - bad size);
  115. if (suggestions[0].completion != "sample_header.h")
  116. FAIL("project include - wrong results");
  117. suggestions = autocomplete.get_suggestions("complete_includes.cpp", { 1, 19 });
  118. if (suggestions.size() != 1)
  119. FAIL(global include - bad size);
  120. if (suggestions[0].completion != "cdefs.h")
  121. FAIL("global include - wrong results");
  122. PASS;
  123. }