Alexander Kornienko | 9eb8c92 | 2014-04-08 12:27:49 +0000 | [diff] [blame] | 1 | #include "ClangTidy.h" |
| 2 | #include "ClangTidyTest.h" |
| 3 | #include "gtest/gtest.h" |
| 4 | |
| 5 | namespace clang { |
| 6 | namespace tidy { |
| 7 | namespace test { |
| 8 | |
| 9 | class TestCheck : public ClangTidyCheck { |
| 10 | public: |
| 11 | void registerMatchers(ast_matchers::MatchFinder *Finder) override { |
| 12 | Finder->addMatcher(ast_matchers::varDecl().bind("var"), this); |
| 13 | } |
| 14 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override { |
| 15 | const VarDecl *Var = Result.Nodes.getNodeAs<VarDecl>("var"); |
| 16 | // Add diagnostics in the wrong order. |
| 17 | diag(Var->getLocation(), "variable"); |
| 18 | diag(Var->getTypeSpecStartLoc(), "type specifier"); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | TEST(ClangTidyDiagnosticConsumer, SortsErrors) { |
Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 23 | std::vector<ClangTidyError> Errors; |
| 24 | runCheckOnCode<TestCheck>("int a;", &Errors); |
Alexander Kornienko | 9eb8c92 | 2014-04-08 12:27:49 +0000 | [diff] [blame] | 25 | EXPECT_EQ(2ul, Errors.size()); |
| 26 | // FIXME: Remove " []" once the check name is removed from the message text. |
| 27 | EXPECT_EQ("type specifier []", Errors[0].Message.Message); |
| 28 | EXPECT_EQ("variable []", Errors[1].Message.Message); |
| 29 | } |
| 30 | |
Alexander Kornienko | 23fe959 | 2014-05-15 14:27:36 +0000 | [diff] [blame^] | 31 | TEST(ChecksFilter, Empty) { |
| 32 | ChecksFilter Filter(""); |
| 33 | |
| 34 | EXPECT_TRUE(Filter.isCheckEnabled("")); |
| 35 | EXPECT_FALSE(Filter.isCheckEnabled("aaa")); |
| 36 | } |
| 37 | |
| 38 | TEST(ChecksFilter, Nothing) { |
| 39 | ChecksFilter Filter("-*"); |
| 40 | |
| 41 | EXPECT_FALSE(Filter.isCheckEnabled("")); |
| 42 | EXPECT_FALSE(Filter.isCheckEnabled("a")); |
| 43 | EXPECT_FALSE(Filter.isCheckEnabled("-*")); |
| 44 | EXPECT_FALSE(Filter.isCheckEnabled("-")); |
| 45 | EXPECT_FALSE(Filter.isCheckEnabled("*")); |
| 46 | } |
| 47 | |
| 48 | TEST(ChecksFilter, Everything) { |
| 49 | ChecksFilter Filter("*"); |
| 50 | |
| 51 | EXPECT_TRUE(Filter.isCheckEnabled("")); |
| 52 | EXPECT_TRUE(Filter.isCheckEnabled("aaaa")); |
| 53 | EXPECT_TRUE(Filter.isCheckEnabled("-*")); |
| 54 | EXPECT_TRUE(Filter.isCheckEnabled("-")); |
| 55 | EXPECT_TRUE(Filter.isCheckEnabled("*")); |
| 56 | } |
| 57 | |
| 58 | TEST(ChecksFilter, Simple) { |
| 59 | ChecksFilter Filter("aaa"); |
| 60 | |
| 61 | EXPECT_TRUE(Filter.isCheckEnabled("aaa")); |
| 62 | EXPECT_FALSE(Filter.isCheckEnabled("")); |
| 63 | EXPECT_FALSE(Filter.isCheckEnabled("aa")); |
| 64 | EXPECT_FALSE(Filter.isCheckEnabled("aaaa")); |
| 65 | EXPECT_FALSE(Filter.isCheckEnabled("bbb")); |
| 66 | } |
| 67 | |
| 68 | TEST(ChecksFilter, Complex) { |
| 69 | ChecksFilter Filter("*,-a.*,-b.*,a.a.*,-a.a.a.*,-..,-...,-..+,-*$,-*qwe*"); |
| 70 | |
| 71 | EXPECT_TRUE(Filter.isCheckEnabled("aaa")); |
| 72 | EXPECT_TRUE(Filter.isCheckEnabled("qqq")); |
| 73 | EXPECT_FALSE(Filter.isCheckEnabled("a.")); |
| 74 | EXPECT_FALSE(Filter.isCheckEnabled("a.b")); |
| 75 | EXPECT_FALSE(Filter.isCheckEnabled("b.")); |
| 76 | EXPECT_FALSE(Filter.isCheckEnabled("b.b")); |
| 77 | EXPECT_TRUE(Filter.isCheckEnabled("a.a.b")); |
| 78 | EXPECT_FALSE(Filter.isCheckEnabled("a.a.a.a")); |
| 79 | EXPECT_FALSE(Filter.isCheckEnabled("qwe")); |
| 80 | EXPECT_FALSE(Filter.isCheckEnabled("asdfqweasdf")); |
| 81 | EXPECT_TRUE(Filter.isCheckEnabled("asdfqwEasdf")); |
| 82 | } |
| 83 | |
Alexander Kornienko | 9eb8c92 | 2014-04-08 12:27:49 +0000 | [diff] [blame] | 84 | } // namespace test |
| 85 | } // namespace tidy |
| 86 | } // namespace clang |