diff options
Diffstat (limited to 'clang-tools-extra/unittests/clangd')
-rw-r--r-- | clang-tools-extra/unittests/clangd/FuzzyMatchTests.cpp | 34 | ||||
-rw-r--r-- | clang-tools-extra/unittests/clangd/IndexTests.cpp | 28 |
2 files changed, 33 insertions, 29 deletions
diff --git a/clang-tools-extra/unittests/clangd/FuzzyMatchTests.cpp b/clang-tools-extra/unittests/clangd/FuzzyMatchTests.cpp index 6efcdbe8077..b665fe36e4d 100644 --- a/clang-tools-extra/unittests/clangd/FuzzyMatchTests.cpp +++ b/clang-tools-extra/unittests/clangd/FuzzyMatchTests.cpp @@ -20,16 +20,27 @@ using namespace llvm; using testing::Not; struct ExpectedMatch { - ExpectedMatch(StringRef Annotated) : Word(Annotated), Annotated(Annotated) { + // Annotations are optional, and will not be asserted if absent. + ExpectedMatch(StringRef Match) : Word(Match), Annotated(Match) { for (char C : "[]") Word.erase(std::remove(Word.begin(), Word.end(), C), Word.end()); + if (Word.size() == Annotated->size()) + Annotated = None; + } + bool accepts(StringRef ActualAnnotated) const { + return !Annotated || ActualAnnotated == *Annotated; } std::string Word; - StringRef Annotated; + + friend raw_ostream &operator<<(raw_ostream &OS, const ExpectedMatch &M) { + return OS << "'" << M.Word; + if (M.Annotated) + OS << "' as " << *M.Annotated; + } + +private: + Optional<StringRef> Annotated; }; -raw_ostream &operator<<(raw_ostream &OS, const ExpectedMatch &M) { - return OS << "'" << M.Word << "' as " << M.Annotated; -} struct MatchesMatcher : public testing::MatcherInterface<StringRef> { ExpectedMatch Candidate; @@ -47,7 +58,7 @@ struct MatchesMatcher : public testing::MatcherInterface<StringRef> { FuzzyMatcher Matcher(Pattern); auto Result = Matcher.match(Candidate.Word); auto AnnotatedMatch = Matcher.dumpLast(*OS << "\n"); - return Result && AnnotatedMatch == Candidate.Annotated; + return Result && Candidate.accepts(AnnotatedMatch); } }; @@ -122,6 +133,7 @@ TEST(FuzzyMatch, Matches) { EXPECT_THAT("sl", matches("[S]Visual[L]oggerLogsList")); EXPECT_THAT("sllll", matches("[S]Visua[lL]ogger[L]ogs[L]ist")); EXPECT_THAT("Three", matches("H[T]ML[HRE]l[e]ment")); + EXPECT_THAT("b", Not(matches("NDEBUG"))); EXPECT_THAT("Three", matches("[Three]")); EXPECT_THAT("fo", Not(matches("barfoo"))); EXPECT_THAT("fo", matches("bar_[fo]o")); @@ -151,8 +163,9 @@ TEST(FuzzyMatch, Matches) { EXPECT_THAT("g", matches("zz[G]roup")); EXPECT_THAT("aaaa", matches("_a_[aaaa]")); // Prefer consecutive. - EXPECT_THAT("printf", matches("s[printf]")); - EXPECT_THAT("str", matches("o[str]eam")); + // These would ideally match, but would need special segmentation rules. + EXPECT_THAT("printf", Not(matches("s[printf]"))); + EXPECT_THAT("str", Not(matches("o[str]eam"))); } struct RankMatcher : public testing::MatcherInterface<StringRef> { @@ -188,9 +201,8 @@ struct RankMatcher : public testing::MatcherInterface<StringRef> { llvm::raw_string_ostream Info(Buf); auto AnnotatedMatch = Matcher.dumpLast(Info); - if (AnnotatedMatch != Str.Annotated) { - *OS << "\nMatched " << Str.Word << " as " << AnnotatedMatch - << " instead of " << Str.Annotated << "\n" + if (!Str.accepts(AnnotatedMatch)) { + *OS << "\nDoesn't match " << Str << ", but " << AnnotatedMatch << "\n" << Info.str(); Ok = false; } else if (LastScore && *LastScore < *Score) { diff --git a/clang-tools-extra/unittests/clangd/IndexTests.cpp b/clang-tools-extra/unittests/clangd/IndexTests.cpp index 3c61533818e..20eb4527d9f 100644 --- a/clang-tools-extra/unittests/clangd/IndexTests.cpp +++ b/clang-tools-extra/unittests/clangd/IndexTests.cpp @@ -116,14 +116,6 @@ TEST(MemIndexTest, MemIndexSymbolsRecycled) { EXPECT_TRUE(Symbols.expired()); } -TEST(MemIndexTest, MemIndexMatchSubstring) { - MemIndex I; - I.build(generateNumSymbols(5, 25)); - FuzzyFindRequest Req; - Req.Query = "5"; - EXPECT_THAT(match(I, Req), UnorderedElementsAre("5", "15", "25")); -} - TEST(MemIndexTest, MemIndexDeduplicate) { auto Symbols = generateNumSymbols(0, 10); @@ -166,46 +158,46 @@ TEST(MemIndexTest, FuzzyMatch) { TEST(MemIndexTest, MatchQualifiedNamesWithoutSpecificScope) { MemIndex I; - I.build(generateSymbols({"a::xyz", "b::yz", "yz"})); + I.build(generateSymbols({"a::y1", "b::y2", "y3"})); FuzzyFindRequest Req; Req.Query = "y"; - EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz", "b::yz", "yz")); + EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::y1", "b::y2", "y3")); } TEST(MemIndexTest, MatchQualifiedNamesWithGlobalScope) { MemIndex I; - I.build(generateSymbols({"a::xyz", "b::yz", "yz"})); + I.build(generateSymbols({"a::y1", "b::y2", "y3"})); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {""}; - EXPECT_THAT(match(I, Req), UnorderedElementsAre("yz")); + EXPECT_THAT(match(I, Req), UnorderedElementsAre("y3")); } TEST(MemIndexTest, MatchQualifiedNamesWithOneScope) { MemIndex I; - I.build(generateSymbols({"a::xyz", "a::yy", "a::xz", "b::yz", "yz"})); + I.build(generateSymbols({"a::y1", "a::y2", "a::x", "b::y2", "y3"})); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a"}; - EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz", "a::yy")); + EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::y1", "a::y2")); } TEST(MemIndexTest, MatchQualifiedNamesWithMultipleScopes) { MemIndex I; - I.build(generateSymbols({"a::xyz", "a::yy", "a::xz", "b::yz", "yz"})); + I.build(generateSymbols({"a::y1", "a::y2", "a::x", "b::y3", "y3"})); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a", "b"}; - EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz", "a::yy", "b::yz")); + EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::y1", "a::y2", "b::y3")); } TEST(MemIndexTest, NoMatchNestedScopes) { MemIndex I; - I.build(generateSymbols({"a::xyz", "a::b::yy"})); + I.build(generateSymbols({"a::y1", "a::b::y2"})); FuzzyFindRequest Req; Req.Query = "y"; Req.Scopes = {"a"}; - EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::xyz")); + EXPECT_THAT(match(I, Req), UnorderedElementsAre("a::y1")); } TEST(MemIndexTest, IgnoreCases) { |