Tests.cpp 3.3 KB

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