1 //===-- ProjectAwareIndexTests.cpp -------------------*- C++ -*-----------===//
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 //===----------------------------------------------------------------------===//
10 #include "TestIndex.h"
11 #include "index/Index.h"
12 #include "index/MemIndex.h"
13 #include "index/ProjectAware.h"
14 #include "index/Ref.h"
15 #include "index/Relation.h"
16 #include "support/Context.h"
17 #include "support/Threading.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
26 using testing::ElementsAre
;
27 using testing::IsEmpty
;
29 std::unique_ptr
<SymbolIndex
> createIndex() {
30 SymbolSlab::Builder Builder
;
31 Builder
.insert(symbol("1"));
32 return MemIndex::build(std::move(Builder
).build(), RefSlab(), RelationSlab());
35 TEST(ProjectAware
, Test
) {
36 IndexFactory Gen
= [](const Config::ExternalIndexSpec
&, AsyncTaskRunner
*) {
40 auto Idx
= createProjectAwareIndex(std::move(Gen
), true);
45 EXPECT_THAT(match(*Idx
, Req
), IsEmpty());
48 C
.Index
.External
.Kind
= Config::ExternalIndexSpec::File
;
49 C
.Index
.External
.Location
= "test";
50 WithContextValue
With(Config::Key
, std::move(C
));
51 EXPECT_THAT(match(*Idx
, Req
), ElementsAre("1"));
55 TEST(ProjectAware
, CreatedOnce
) {
56 unsigned InvocationCount
= 0;
57 IndexFactory Gen
= [&](const Config::ExternalIndexSpec
&, AsyncTaskRunner
*) {
62 auto Idx
= createProjectAwareIndex(std::move(Gen
), true);
63 // No invocation at start.
64 EXPECT_EQ(InvocationCount
, 0U);
69 // Cannot invoke without proper config.
71 EXPECT_EQ(InvocationCount
, 0U);
74 C
.Index
.External
.Kind
= Config::ExternalIndexSpec::File
;
75 C
.Index
.External
.Location
= "test";
76 WithContextValue
With(Config::Key
, std::move(C
));
78 // Now it should be created.
79 EXPECT_EQ(InvocationCount
, 1U);
81 // It is cached afterwards.
82 EXPECT_EQ(InvocationCount
, 1U);