Files
clice/tests/unit/index/project_index_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

169 lines
4.7 KiB
C++

#include "test/test.h"
#include "test/tester.h"
#include "index/project_index.h"
namespace clice::testing {
namespace {
TEST_SUITE(ProjectIndex, Tester) {
bool build_and_index(llvm::StringRef code, index::TUIndex& out) {
add_main("main.cpp", code);
if(!compile()) {
return false;
}
out = index::TUIndex::build(*unit);
return true;
}
TEST_CASE(MergeSingleTU) {
index::TUIndex tu;
ASSERT_TRUE(build_and_index(R"(
int foo() { return 42; }
int bar() { return foo() + 1; }
)",
tu));
index::ProjectIndex project;
auto file_ids_map = project.merge(tu);
// Path pool should have entries for the TU's files.
ASSERT_FALSE(project.path_pool.paths.empty());
// Symbols from the TU should be merged into the project.
ASSERT_FALSE(project.symbols.empty());
// Every symbol from the TU should be in the project.
for(auto& [hash, symbol]: tu.symbols) {
ASSERT_TRUE(project.symbols.contains(hash));
}
}
TEST_CASE(MergeMultipleTUs) {
index::TUIndex tu1;
ASSERT_TRUE(build_and_index(R"(
int foo() { return 42; }
)",
tu1));
index::TUIndex tu2;
ASSERT_TRUE(build_and_index(R"(
int bar() { return 99; }
)",
tu2));
index::ProjectIndex project;
project.merge(tu1);
project.merge(tu2);
// All symbols from both TUs should be present.
for(auto& [hash, symbol]: tu1.symbols) {
ASSERT_TRUE(project.symbols.contains(hash));
}
for(auto& [hash, symbol]: tu2.symbols) {
ASSERT_TRUE(project.symbols.contains(hash));
}
}
TEST_CASE(MergeDuplicateSymbol) {
// Build two TUs that both define/reference the same function via header.
add_file("shared.h", R"(
#pragma once
inline int shared_func() { return 1; }
)");
add_main("a.cpp", R"(
#include "shared.h"
int use_a() { return shared_func(); }
)");
ASSERT_TRUE(compile());
auto tu_a = index::TUIndex::build(*unit);
add_file("shared.h", R"(
#pragma once
inline int shared_func() { return 1; }
)");
add_main("b.cpp", R"(
#include "shared.h"
int use_b() { return shared_func(); }
)");
ASSERT_TRUE(compile());
auto tu_b = index::TUIndex::build(*unit);
index::ProjectIndex project;
project.merge(tu_a);
project.merge(tu_b);
// Find the shared_func symbol hash from TU A's symbol table.
index::SymbolHash shared_hash = 0;
for(auto& [hash, symbol]: tu_a.symbols) {
if(symbol.name == "shared_func") {
shared_hash = hash;
break;
}
}
ASSERT_TRUE(shared_hash != 0);
// The same hash should exist in project symbols.
ASSERT_TRUE(project.symbols.contains(shared_hash));
// reference_files bitmap should contain entries from both TUs.
auto& proj_sym = project.symbols[shared_hash];
ASSERT_TRUE(proj_sym.reference_files.cardinality() >= 2U);
}
TEST_CASE(SerializationRoundTrip) {
index::TUIndex tu;
ASSERT_TRUE(build_and_index(R"(
struct Foo { int x; };
void bar(Foo f) { f.x = 42; }
)",
tu));
index::ProjectIndex project;
project.merge(tu);
// Serialize.
llvm::SmallString<4096> buf;
llvm::raw_svector_ostream os(buf);
project.serialize(os);
// Deserialize.
auto restored = index::ProjectIndex::from(buf.data());
// Path pools should match.
ASSERT_EQ(project.path_pool.paths.size(), restored.path_pool.paths.size());
// Symbol tables should have same size.
ASSERT_EQ(project.symbols.size(), restored.symbols.size());
// Each symbol should be present in restored with same reference count.
for(auto& [hash, symbol]: project.symbols) {
ASSERT_TRUE(restored.symbols.contains(hash));
auto& restored_sym = restored.symbols[hash];
ASSERT_EQ(symbol.reference_files.cardinality(), restored_sym.reference_files.cardinality());
}
}
TEST_CASE(FileIdsMapCorrectness) {
index::TUIndex tu;
ASSERT_TRUE(build_and_index(R"(
int x = 1;
)",
tu));
index::ProjectIndex project;
auto file_ids_map = project.merge(tu);
// file_ids_map should have same size as TU's include graph paths.
ASSERT_EQ(file_ids_map.size(), tu.graph.paths.size());
// Each mapped ID should be valid in the project path pool.
for(auto mapped_id: file_ids_map) {
ASSERT_TRUE(mapped_id < project.path_pool.paths.size());
}
}
}; // TEST_SUITE(ProjectIndex)
} // namespace
} // namespace clice::testing