Files
clice/tests/unit/feature/code_completion_tests.cpp
ykiko 0a891d8b4a refactor(tests): use Tester fixture, normalize helpers, add index tests (#377)
## Summary

- **Use `Tester` as fixture base** for all test suites that need
compilation, replacing `TesterFixture` and removing redundant
`tester.clear()` calls (eventide zest now creates fresh instances per
TEST_CASE)
- **Remove local `Tester` variables** in `compilation_tests`,
`template_resolver_tests`, `selection_tests` — use inherited fixture
members directly
- **Normalize helper naming**: `expect_xxx` → `EXPECT_XXX`,
`go_to_definition` → `GO_TO_DEFINITION` for consistency
- **Extract shared `test/cdb_helper.h`**: deduplicate `CDBEntry`,
`json_escape`, `build_cdb_json` from `dependency_graph_tests` and
`compile_graph_integration_tests`
- **Add new test files/cases**: `project_index_tests.cpp`, expanded
`tu_index_tests`, `merged_index_tests`, `compilation_tests`

## Test plan

- [x] All existing unit tests pass
- [x] New index tests (TUIndex, MergedIndex, ProjectIndex) pass
- [x] Compilation tests (PCH, PCM, stop) pass

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Tests**
* Standardized test fixtures and helper naming, moved suites to a shared
fixture, and unified in-memory VFS and compile flows.
* Added broad new coverage: indexing, project indexing, compilation/PCH,
diagnostics, semantic features, and many targeted unit cases.
* Introduced a small compile-database helper and improved driver-style
test compilation paths.

* **Chores**
* Consolidated and reorganized test utilities and tester APIs for easier
maintenance and reuse.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 10:29:49 +08:00

115 lines
2.3 KiB
C++

#include <algorithm>
#include <vector>
#include "test/annotation.h"
#include "test/test.h"
#include "feature/feature.h"
namespace clice::testing {
namespace {
namespace protocol = eventide::ipc::protocol;
TEST_SUITE(CodeCompletion) {
std::vector<protocol::CompletionItem> items;
llvm::IntrusiveRefCntPtr<TestVFS> vfs;
std::string main_path;
void code_complete(llvm::StringRef code) {
vfs = llvm::makeIntrusiveRefCnt<TestVFS>();
CompilationParams params;
auto annotation = AnnotatedSource::from(code);
vfs->add("main.cpp", annotation.content);
params.vfs = vfs;
main_path = TestVFS::path("main.cpp");
params.arguments =
{"clang++", "-std=c++20", "-ffreestanding", "-Xclang", "-undef", main_path.c_str()};
params.completion = {main_path, annotation.offsets.lookup("pos")};
params.add_remapped_file(main_path, annotation.content);
feature::CodeCompletionOptions options = {};
items = feature::code_complete(params, options, feature::PositionEncoding::UTF8);
}
TEST_CASE(Score) {
code_complete(R"cpp(
int foooo(int x);
int x = fo$(pos)
)cpp");
auto it = std::ranges::find_if(items, [](const protocol::CompletionItem& item) {
return item.label == "foooo";
});
ASSERT_TRUE(it != items.end());
ASSERT_TRUE(it->kind.has_value());
ASSERT_EQ(*it->kind, protocol::CompletionItemKind::Function);
}
TEST_CASE(Snippet) {
code_complete(R"cpp(
int x = tru$(pos)
)cpp");
ASSERT_TRUE(!items.empty());
}
TEST_CASE(Overload) {
code_complete(R"cpp(
int foooo(int x);
int foooo(int x, int y);
int x = fooo$(pos)
)cpp");
ASSERT_TRUE(!items.empty());
}
TEST_CASE(Unqualified) {
code_complete(R"cpp(
namespace A {
void fooooo();
}
void bar() {
fo$(pos)
}
)cpp");
// Legacy parity: keep as smoke case without strict expectation.
}
TEST_CASE(Functor) {
code_complete(R"cpp(
struct X {
void operator() () {}
};
void bar() {
X foo;
fo$(pos);
}
)cpp");
// Legacy parity: keep as smoke case without strict expectation.
}
TEST_CASE(Lambda) {
code_complete(R"cpp(
void bar() {
auto foo = [](int x){ };
fo$(pos);
}
)cpp");
// Legacy parity: keep as smoke case without strict expectation.
}
}; // TEST_SUITE(CodeCompletion)
} // namespace
} // namespace clice::testing