Bump version to 19.1.0git
[llvm-project.git] / clang-tools-extra / unittests / clang-include-fixer / FuzzySymbolIndexTests.cpp
blob3886269fcad50e02421e36a972ffac6feadfffd0
1 //===-- FuzzySymbolIndexTests.cpp - Fuzzy symbol index unit tests ---------===//
2 //
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
6 //
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;
15 using testing::Not;
17 namespace clang {
18 namespace include_fixer {
19 namespace {
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);
32 std::string err;
33 if (!Pattern.isValid(err)) {
34 *result_listener << "invalid regex: " << err;
35 return false;
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")))
50 << "Non-prefix";
51 EXPECT_THAT(QueryRegexp("uc"), Not(MatchesSymbol("URLHandlerCallback")))
52 << "Skip token";
54 EXPECT_THAT(QueryRegexp("uptr"), MatchesSymbol("unique_ptr"));
55 EXPECT_THAT(QueryRegexp("UniP"), MatchesSymbol("unique_ptr"));
58 } // namespace
59 } // namespace include_fixer
60 } // namespace clang