Tests.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "ParserAutoComplete.h"
  9. using namespace LanguageServers;
  10. using namespace LanguageServers::Cpp;
  11. static bool s_some_test_failed = false;
  12. #define I_TEST(name) \
  13. { \
  14. printf("Testing " #name "... "); \
  15. fflush(stdout); \
  16. }
  17. #define PASS \
  18. do { \
  19. printf("PASS\n"); \
  20. fflush(stdout); \
  21. return; \
  22. } while (0)
  23. #define FAIL(reason) \
  24. do { \
  25. printf("FAIL: " #reason "\n"); \
  26. s_some_test_failed = true; \
  27. return; \
  28. } while (0)
  29. static void test_complete_local_args();
  30. static void test_complete_local_vars();
  31. static void test_complete_type();
  32. static void test_find_variable_definition();
  33. int run_tests()
  34. {
  35. test_complete_local_args();
  36. test_complete_local_vars();
  37. test_complete_type();
  38. test_find_variable_definition();
  39. return s_some_test_failed ? 1 : 0;
  40. }
  41. void test_complete_local_args()
  42. {
  43. I_TEST(Complete Local Args)
  44. FileDB filedb;
  45. String content = R"(
  46. int main(int argc, char** argv){
  47. ar
  48. }
  49. )";
  50. filedb.add("a.cpp", content);
  51. ParserAutoComplete autocomplete(filedb);
  52. auto suggestions = autocomplete.get_suggestions("a.cpp", { 2, 2 });
  53. if (suggestions.size() != 2)
  54. FAIL(bad size);
  55. if (suggestions[0].completion == "argc" && suggestions[1].completion == "argv")
  56. PASS;
  57. FAIL("wrong results");
  58. }
  59. void test_complete_local_vars()
  60. {
  61. I_TEST(Complete Local Vars)
  62. FileDB filedb;
  63. String content = R"(
  64. int main(int argc, char** argv){
  65. int myvar1 = 3;
  66. myv
  67. }
  68. )";
  69. filedb.add("a.cpp", content);
  70. ParserAutoComplete autocomplete(filedb);
  71. auto suggestions = autocomplete.get_suggestions("a.cpp", { 3, 3 });
  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. String content = R"(
  83. struct MyStruct {
  84. int x;
  85. }
  86. void foo(){
  87. MyS
  88. }
  89. )";
  90. filedb.add("a.cpp", content);
  91. ParserAutoComplete autocomplete(filedb);
  92. auto suggestions = autocomplete.get_suggestions("a.cpp", { 5, 3 });
  93. if (suggestions.size() != 1)
  94. FAIL(bad size);
  95. if (suggestions[0].completion == "MyStruct")
  96. PASS;
  97. FAIL("wrong results");
  98. }
  99. void test_find_variable_definition()
  100. {
  101. I_TEST(Find Variable Declaration)
  102. FileDB filedb;
  103. String content = R"(
  104. int main(int argc, char** argv){
  105. argv = nullptr;
  106. }
  107. )";
  108. filedb.add("a.cpp", content);
  109. ParserAutoComplete autocomplete(filedb);
  110. auto position = autocomplete.find_declaration_of("a.cpp", { 2, 1 });
  111. if (!position.has_value())
  112. FAIL("declaration not found");
  113. if (position.value().file == "a.cpp" && position.value().line == 1 && position.value().column >= 19)
  114. PASS;
  115. FAIL("wrong declaration location");
  116. }