FileIndex and MergedIndex for clice (#271)
This commit is contained in:
67
tests/unit/Index/MergedIndex.cpp
Normal file
67
tests/unit/Index/MergedIndex.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "Test/Tester.h"
|
||||
#include "Index/MergedIndex.h"
|
||||
|
||||
namespace clice::testing {
|
||||
|
||||
namespace {
|
||||
|
||||
suite<"MergedIndex"> suite = [] {
|
||||
Tester tester;
|
||||
index::TUIndex tu_index;
|
||||
|
||||
auto build_index = [&](llvm::StringRef code,
|
||||
std::source_location location = std::source_location::current()) {
|
||||
tester.clear();
|
||||
tester.add_main("main.cpp", code);
|
||||
fatal / expect(tester.compile(), location);
|
||||
|
||||
tu_index = index::TUIndex::build(*tester.unit);
|
||||
};
|
||||
|
||||
auto expect_select = [&](llvm::StringRef pos,
|
||||
llvm::StringRef expect_range,
|
||||
llvm::StringRef file = "",
|
||||
std::source_location location = std::source_location::current()) {
|
||||
auto offset = tester.point(pos, file);
|
||||
auto range = tester.range(expect_range, file);
|
||||
|
||||
auto fid = file.empty() ? tester.unit->interested_file() : tester.unit->file_id(file);
|
||||
auto& index = tu_index.file_indices[fid];
|
||||
|
||||
auto it = std::ranges::lower_bound(
|
||||
index.occurrences,
|
||||
offset,
|
||||
{},
|
||||
[](index::Occurrence& occurrence) { return occurrence.range.end; });
|
||||
|
||||
auto err =
|
||||
std::format("Fail to find symbol for offser: {} range: range: {}", offset, dump(range));
|
||||
|
||||
fatal / expect(it != index.occurrences.end(), location) << err;
|
||||
|
||||
/// FIXME: Make eq pretty print reflectable struct.
|
||||
expect(eq(dump(it->range), dump(range)), location);
|
||||
};
|
||||
|
||||
test("Assert") = [&] {
|
||||
build_index(R"(
|
||||
#include <iostream>
|
||||
)");
|
||||
|
||||
std::println("{}", tu_index.file_indices.size());
|
||||
|
||||
index::MergedIndex merged;
|
||||
|
||||
for(auto& [fid, index]: tu_index.file_indices) {
|
||||
auto path = tester.unit->file_path(fid);
|
||||
|
||||
if(path.ends_with("stddef.h")) {
|
||||
merged.merge(path, tu_index.graph.getInclude(fid), index);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace clice::testing
|
||||
68
tests/unit/Index/TUIndex.cpp
Normal file
68
tests/unit/Index/TUIndex.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "Test/Tester.h"
|
||||
#include "Index/TUIndex.h"
|
||||
|
||||
namespace clice::testing {
|
||||
|
||||
namespace {
|
||||
|
||||
suite<"TUIndex"> suite = [] {
|
||||
Tester tester;
|
||||
index::TUIndex tu_index;
|
||||
|
||||
auto build_index = [&](llvm::StringRef code,
|
||||
std::source_location location = std::source_location::current()) {
|
||||
tester.clear();
|
||||
tester.add_main("main.cpp", code);
|
||||
fatal / expect(tester.compile(), location);
|
||||
|
||||
tu_index = index::TUIndex::build(*tester.unit);
|
||||
};
|
||||
|
||||
auto expect_select = [&](llvm::StringRef pos,
|
||||
llvm::StringRef expect_range,
|
||||
llvm::StringRef file = "",
|
||||
std::source_location location = std::source_location::current()) {
|
||||
auto offset = tester.point(pos, file);
|
||||
auto range = tester.range(expect_range, file);
|
||||
|
||||
auto fid = file.empty() ? tester.unit->interested_file() : tester.unit->file_id(file);
|
||||
auto& index = tu_index.file_indices[fid];
|
||||
|
||||
auto it = std::ranges::lower_bound(
|
||||
index.occurrences,
|
||||
offset,
|
||||
{},
|
||||
[](index::Occurrence& occurrence) { return occurrence.range.end; });
|
||||
|
||||
auto err =
|
||||
std::format("Fail to find symbol for offser: {} range: range: {}", offset, dump(range));
|
||||
|
||||
fatal / expect(it != index.occurrences.end(), location) << err;
|
||||
|
||||
/// FIXME: Make eq pretty print reflectable struct.
|
||||
expect(eq(dump(it->range), dump(range)), location);
|
||||
};
|
||||
|
||||
test("Basic") = [&] {
|
||||
build_index(R"(
|
||||
int @1[f$(1)oo]();
|
||||
|
||||
int @2[b$(2)ar]() {
|
||||
return @3[fo$(3)o]() + 1;
|
||||
}
|
||||
)");
|
||||
|
||||
expect(eq(tu_index.file_indices.size(), 1));
|
||||
auto& index = tu_index.file_indices.begin()->second;
|
||||
expect(eq(index.relations.size(), 2));
|
||||
expect(eq(index.occurrences.size(), 3));
|
||||
|
||||
expect_select("1", "1");
|
||||
expect_select("2", "2");
|
||||
expect_select("3", "3");
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
} // namespace clice::testing
|
||||
Reference in New Issue
Block a user