1 | //===-- TidyProviderTests.cpp - Clang tidy configuration provider 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 "Feature.h" |
10 | #include "TestFS.h" |
11 | #include "TidyProvider.h" |
12 | #include "llvm/Testing/Support/SupportHelpers.h" |
13 | #include "gtest/gtest.h" |
14 | |
15 | namespace clang { |
16 | namespace clangd { |
17 | |
18 | namespace { |
19 | |
20 | TEST(TidyProvider, NestedDirectories) { |
21 | MockFS FS; |
22 | FS.Files[testPath(File: ".clang-tidy" )] = R"yaml( |
23 | Checks: 'llvm-*' |
24 | CheckOptions: |
25 | TestKey: 1 |
26 | )yaml" ; |
27 | FS.Files[testPath(File: "sub1/.clang-tidy" )] = R"yaml( |
28 | Checks: 'misc-*' |
29 | CheckOptions: |
30 | TestKey: 2 |
31 | )yaml" ; |
32 | FS.Files[testPath(File: "sub1/sub2/.clang-tidy" )] = R"yaml( |
33 | Checks: 'bugprone-*' |
34 | CheckOptions: |
35 | TestKey: 3 |
36 | InheritParentConfig: true |
37 | )yaml" ; |
38 | |
39 | TidyProvider Provider = provideClangTidyFiles(FS); |
40 | |
41 | auto BaseOptions = getTidyOptionsForFile(Provider, Filename: testPath(File: "File.cpp" )); |
42 | ASSERT_TRUE(BaseOptions.Checks.has_value()); |
43 | EXPECT_EQ(*BaseOptions.Checks, "llvm-*" ); |
44 | EXPECT_EQ(BaseOptions.CheckOptions.lookup("TestKey" ).Value, "1" ); |
45 | |
46 | auto Sub1Options = getTidyOptionsForFile(Provider, Filename: testPath(File: "sub1/File.cpp" )); |
47 | ASSERT_TRUE(Sub1Options.Checks.has_value()); |
48 | EXPECT_EQ(*Sub1Options.Checks, "misc-*" ); |
49 | EXPECT_EQ(Sub1Options.CheckOptions.lookup("TestKey" ).Value, "2" ); |
50 | |
51 | auto Sub2Options = |
52 | getTidyOptionsForFile(Provider, Filename: testPath(File: "sub1/sub2/File.cpp" )); |
53 | ASSERT_TRUE(Sub2Options.Checks.has_value()); |
54 | EXPECT_EQ(*Sub2Options.Checks, "misc-*,bugprone-*" ); |
55 | EXPECT_EQ(Sub2Options.CheckOptions.lookup("TestKey" ).Value, "3" ); |
56 | } |
57 | |
58 | TEST(TidyProvider, IsFastTidyCheck) { |
59 | EXPECT_THAT(isFastTidyCheck("misc-const-correctness" ), llvm::ValueIs(false)); |
60 | EXPECT_THAT(isFastTidyCheck("bugprone-suspicious-include" ), |
61 | llvm::ValueIs(true)); |
62 | // Linked in (ParsedASTTests.cpp) but not measured. |
63 | EXPECT_EQ(isFastTidyCheck("replay-preamble-check" ), std::nullopt); |
64 | } |
65 | |
66 | #if CLANGD_TIDY_CHECKS |
67 | TEST(TidyProvider, IsValidCheck) { |
68 | EXPECT_TRUE(isRegisteredTidyCheck("bugprone-argument-comment" )); |
69 | EXPECT_FALSE(isRegisteredTidyCheck("bugprone-argument-clinic" )); |
70 | } |
71 | #endif |
72 | |
73 | } // namespace |
74 | } // namespace clangd |
75 | } // namespace clang |
76 | |