Skip to content

Commit f40d2c7

Browse files
author
Dimitri Ratz
committed
Extended unit-tests to check the occurrence of symbol tags
in call-hierarchy and type-hierarchy.
1 parent d74fdd2 commit f40d2c7

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

clang-tools-extra/clangd/unittests/CallHierarchyTests.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ MATCHER_P(withDetail, N, "") { return arg.detail == N; }
4848
MATCHER_P(withFile, N, "") { return arg.uri.file() == N; }
4949
MATCHER_P(withSelectionRange, R, "") { return arg.selectionRange == R; }
5050

51+
template <typename... Tags>
52+
::testing::Matcher<CallHierarchyItem> withSymbolTags(Tags... tags) {
53+
// Matches the tags vector ignoring element order.
54+
return Field(&CallHierarchyItem::tags, UnorderedElementsAre(tags...));
55+
}
56+
5157
template <class ItemMatcher>
5258
::testing::Matcher<CallHierarchyIncomingCall> from(ItemMatcher M) {
5359
return Field(&CallHierarchyIncomingCall::from, M);
@@ -728,6 +734,56 @@ TEST(CallHierarchy, CallInDifferentFileThanCaller) {
728734
ElementsAre(AllOf(from(withName("caller")), iFromRanges())));
729735
}
730736

737+
TEST(CallHierarchy, IncomingCalls) {
738+
Annotations Source(R"cpp(
739+
class A {
740+
public:
741+
void call^ee() {};
742+
};
743+
void caller(A &a) {
744+
a.callee();
745+
}
746+
)cpp");
747+
TestTU TU = TestTU::withCode(Source.code());
748+
auto AST = TU.build();
749+
auto Index = TU.index();
750+
751+
std::vector<CallHierarchyItem> Items =
752+
prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
753+
ASSERT_THAT(Items, ElementsAre(withName("callee")));
754+
755+
auto Incoming = incomingCalls(Items[0], Index.get(), AST);
756+
EXPECT_THAT(
757+
Incoming,
758+
UnorderedElementsAre(AllOf(from(
759+
AllOf(withName("caller"), withSymbolTags(SymbolTag::Declaration,
760+
SymbolTag::Definition))))));
761+
}
762+
763+
TEST(CallHierarchy, OutgoingCalls) {
764+
Annotations Source(R"cpp(
765+
void callee() {}
766+
class A {
767+
public:
768+
void call^er() {
769+
callee();
770+
};
771+
};
772+
)cpp");
773+
TestTU TU = TestTU::withCode(Source.code());
774+
auto AST = TU.build();
775+
auto Index = TU.index();
776+
777+
std::vector<CallHierarchyItem> Items =
778+
prepareCallHierarchy(AST, Source.point(), testPath(TU.Filename));
779+
ASSERT_THAT(Items, ElementsAre(withName("caller")));
780+
781+
auto Outgoing = outgoingCalls(Items[0], Index.get(), AST);
782+
EXPECT_THAT(Outgoing, UnorderedElementsAre(AllOf(
783+
to(AllOf(withName("callee"),
784+
withSymbolTags(SymbolTag::Declaration,
785+
SymbolTag::Definition))))));
786+
}
731787
} // namespace
732788
} // namespace clangd
733789
} // namespace clang

clang-tools-extra/clangd/unittests/TypeHierarchyTests.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ MATCHER_P(withResolveParents, M, "") {
5454
return testing::ExplainMatchResult(M, arg.data.parents, result_listener);
5555
}
5656

57+
template <typename... Tags>
58+
::testing::Matcher<TypeHierarchyItem> withSymbolTags(Tags... tags) {
59+
// Matches the tags vector ignoring element order.
60+
return Field(&TypeHierarchyItem::tags, UnorderedElementsAre(tags...));
61+
}
62+
5763
TEST(FindRecordTypeAt, TypeOrVariable) {
5864
Annotations Source(R"cpp(
5965
struct Ch^ild2 {
@@ -770,10 +776,10 @@ struct Child2b : Child1 {};
770776

771777
TEST(Standard, SubTypes) {
772778
Annotations Source(R"cpp(
773-
struct Pare^nt1 {};
774-
struct Parent2 {};
775-
struct Child : Parent1, Parent2 {};
776-
)cpp");
779+
struct Pare^nt1 {};
780+
struct Parent2 {};
781+
struct Child final : Parent1, Parent2 {};
782+
)cpp");
777783

778784
TestTU TU = TestTU::withCode(Source.code());
779785
auto AST = TU.build();
@@ -791,15 +797,17 @@ struct Child : Parent1, Parent2 {};
791797
Children,
792798
UnorderedElementsAre(
793799
AllOf(withName("Child"),
800+
withSymbolTags(SymbolTag::Declaration, SymbolTag::Definition,
801+
SymbolTag::Final),
794802
withResolveParents(Optional(UnorderedElementsAre(withResolveID(
795803
getSymbolID(&findDecl(AST, "Parent1")).str())))))));
796804
}
797805

798806
TEST(Standard, SuperTypes) {
799807
Annotations Source(R"cpp(
800-
struct Parent {};
801-
struct Chil^d : Parent {};
802-
)cpp");
808+
struct Parent {};
809+
struct Chil^d : Parent {};
810+
)cpp");
803811

804812
TestTU TU = TestTU::withCode(Source.code());
805813
auto AST = TU.build();
@@ -811,9 +819,11 @@ struct Chil^d : Parent {};
811819
ASSERT_THAT(Result, SizeIs(1));
812820
auto Parents = superTypes(Result.front(), Index.get(), AST);
813821

814-
EXPECT_THAT(Parents, Optional(UnorderedElementsAre(
815-
AllOf(withName("Parent"),
816-
withResolveParents(Optional(IsEmpty()))))));
822+
EXPECT_THAT(Parents,
823+
Optional(UnorderedElementsAre(AllOf(
824+
withName("Parent"),
825+
withSymbolTags(SymbolTag::Declaration, SymbolTag::Definition),
826+
withResolveParents(Optional(IsEmpty()))))));
817827
}
818828
} // namespace
819829
} // namespace clangd

0 commit comments

Comments
 (0)