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//===----------------------------------------------------------------------===//
8
9#include "FuzzySymbolIndex.h"
10#include "gmock/gmock.h"
11#include "llvm/Support/Regex.h"
12#include "gtest/gtest.h"
13
14using testing::ElementsAre;
15using testing::Not;
16
17namespace clang {
18namespace include_fixer {
19namespace {
20
21TEST(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"));
28}
29
30MATCHER_P(MatchesSymbol, Identifier, "") {
31 llvm::Regex Pattern("^" + arg);
32 std::string err;
33 if (!Pattern.isValid(Error&: err)) {
34 *result_listener << "invalid regex: " << err;
35 return false;
36 }
37 auto Tokens = FuzzySymbolIndex::tokenize(Text: Identifier);
38 std::string Target = llvm::join(Tokens.begin(), Tokens.end(), " ");
39 *result_listener << "matching against '" << Target << "'";
40 return llvm::Regex("^" + arg).match(String: Target);
41}
42
43TEST(FuzzySymbolIndexTest, QueryRegexp) {
44 auto QueryRegexp = [](const std::string &query) {
45 return FuzzySymbolIndex::queryRegexp(Tokens: FuzzySymbolIndex::tokenize(Text: query));
46 };
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";
53
54 EXPECT_THAT(QueryRegexp("uptr"), MatchesSymbol("unique_ptr"));
55 EXPECT_THAT(QueryRegexp("UniP"), MatchesSymbol("unique_ptr"));
56}
57
58} // namespace
59} // namespace include_fixer
60} // namespace clang
61

source code of clang-tools-extra/unittests/clang-include-fixer/FuzzySymbolIndexTests.cpp