#include #include #include #include namespace { using namespace vls::features::call_hierarchy; using Void::Testing::expect; using Void::Testing::require; Range range(std::uint32_t line, std::uint32_t character = 0, std::uint32_t length = 4) { return {{line, character}, {line, character + length}}; } Definition definition(std::string name, std::string path, Range span) { return {std::move(name), std::move(path), span}; } Call call(std::string callee, Definition target, std::string path, Range span) { return {std::move(callee), std::move(target), std::move(path), span}; } } // namespace VOID_TEST(CallHierarchy, groups_calls_by_the_enclosing_definition) { const auto target = definition("f", "/p/a.cpp", range(1)); const std::vector known{definition("outer", "/p/a.cpp", {{0, 0}, {20, 0}}), definition("inner", "/p/a.cpp", {{5, 0}, {9, 0}})}; const std::vector calls{call("f", target, "/p/a.cpp", range(6)), call("f", target, "/p/a.cpp", range(7)), call("f", target, "/p/a.cpp", range(12))}; const auto result = incoming(calls, known, target); require(result.size() == 2); expect(result[0].name == "inner" && result[0].resolved_definition); require(result[0].from_ranges.size() == 2); expect(result[0].from_ranges[0] == range(6)); expect(result[1].name == "outer" && result[1].from_ranges.size() == 1); } VOID_TEST(CallHierarchy, without_an_enclosing_definition_the_caller_is_the_file) { const auto target = definition("f", "/p/a.cpp", range(1)); const std::vector calls{call("f", target, "/p/lib/b.cpp", range(3))}; const auto result = incoming(calls, {}, target); require(result.size() == 1); expect(result[0].name == "b.cpp" && result[0].path == "/p/lib/b.cpp"); expect(!result[0].resolved_definition); expect(result[0].range == range(3)); expect(result[0].from_ranges == std::vector{range(3)}); } VOID_TEST(CallHierarchy, only_calls_to_the_exact_target_are_reported) { const auto target = definition("f", "/p/a.cpp", range(1)); const std::vector calls{ call("f", definition("f", "/p/a.cpp", range(9)), "/p/a.cpp", range(6)), call("f", target, "/p/a.cpp", range(7)), {"f", std::nullopt, "/p/a.cpp", range(8)}}; const auto result = incoming(calls, {}, target); require(result.size() == 1); expect(result[0].from_ranges == std::vector{range(7)}); } VOID_TEST(CallHierarchy, duplicate_sites_are_deduplicated_and_sorted) { const auto target = definition("f", "/p/a.cpp", range(1)); const std::vector calls{call("f", target, "/p/a.cpp", range(9)), call("f", target, "/p/a.cpp", range(4)), call("f", target, "/p/a.cpp", range(9))}; const auto result = incoming(calls, {}, target); require(result.size() == 1); require(result[0].from_ranges.size() == 2); expect(result[0].from_ranges[0] == range(4)); expect(result[0].from_ranges[1] == range(9)); } VOID_TEST(CallHierarchy, callers_and_ranges_are_bounded) { const auto target = definition("f", "/p/a.cpp", range(1)); std::vector known; std::vector calls; for (int index = 0; index != 4; ++index) { known.push_back(definition("g" + std::to_string(index), "/p/a.cpp", {{index * 10u, 0}, {index * 10u + 9u, 0}})); calls.push_back(call("f", target, "/p/a.cpp", range(index * 10u + 2))); } expect(incoming(calls, known, target, Limits{.callers = 2}).size() == 2); expect(incoming(calls, known, target, Limits{.ranges = 1}).size() == 4); std::size_t ranges{}; for (const auto &entry : incoming(calls, known, target, Limits{.ranges = 1})) ranges += entry.from_ranges.size(); expect(ranges == 1); }