1 //===-- FuzzySymbolIndexTests.cpp - Fuzzy symbol index unit tests ---------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "FuzzySymbolIndex.h"
10 #include "gmock/gmock.h"
11 #include "llvm/Support/Regex.h"
12 #include "gtest/gtest.h"
14 using testing::ElementsAre
;
18 namespace include_fixer
{
21 TEST(FuzzySymbolIndexTest
, Tokenize
) {
22 EXPECT_THAT(FuzzySymbolIndex::tokenize("URLHandlerCallback"),
23 ElementsAre("url", "handler", "callback"));
24 EXPECT_THAT(FuzzySymbolIndex::tokenize("snake_case11"),
25 ElementsAre("snake", "case", "11"));
26 EXPECT_THAT(FuzzySymbolIndex::tokenize("__$42!!BOB\nbob"),
27 ElementsAre("42", "bob", "bob"));
30 MATCHER_P(MatchesSymbol
, Identifier
, "") {
31 llvm::Regex
Pattern("^" + arg
);
33 if (!Pattern
.isValid(err
)) {
34 *result_listener
<< "invalid regex: " << err
;
37 auto Tokens
= FuzzySymbolIndex::tokenize(Identifier
);
38 std::string Target
= llvm::join(Tokens
.begin(), Tokens
.end(), " ");
39 *result_listener
<< "matching against '" << Target
<< "'";
40 return llvm::Regex("^" + arg
).match(Target
);
43 TEST(FuzzySymbolIndexTest
, QueryRegexp
) {
44 auto QueryRegexp
= [](const std::string
&query
) {
45 return FuzzySymbolIndex::queryRegexp(FuzzySymbolIndex::tokenize(query
));
47 EXPECT_THAT(QueryRegexp("uhc"), MatchesSymbol("URLHandlerCallback"));
48 EXPECT_THAT(QueryRegexp("urhaca"), MatchesSymbol("URLHandlerCallback"));
49 EXPECT_THAT(QueryRegexp("uhcb"), Not(MatchesSymbol("URLHandlerCallback")))
51 EXPECT_THAT(QueryRegexp("uc"), Not(MatchesSymbol("URLHandlerCallback")))
54 EXPECT_THAT(QueryRegexp("uptr"), MatchesSymbol("unique_ptr"));
55 EXPECT_THAT(QueryRegexp("UniP"), MatchesSymbol("unique_ptr"));
59 } // namespace include_fixer