Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame^] | 1 | //===- unittest/ASTMatchers/Dynamic/VariantValueTest.cpp - VariantValue unit tests -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===-----------------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "../ASTMatchersTest.h" |
| 11 | #include "clang/ASTMatchers/Dynamic/VariantValue.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | namespace clang { |
| 15 | namespace ast_matchers { |
| 16 | namespace dynamic { |
| 17 | namespace { |
| 18 | |
| 19 | using ast_matchers::internal::DynTypedMatcher; |
| 20 | using ast_matchers::internal::Matcher; |
| 21 | |
| 22 | TEST(VariantValueTest, String) { |
| 23 | const ::std::string kString = "string"; |
| 24 | VariantValue Value = kString; |
| 25 | |
| 26 | EXPECT_TRUE(Value.isString()); |
| 27 | EXPECT_EQ(kString, Value.getString()); |
| 28 | |
| 29 | EXPECT_FALSE(Value.isMatcher()); |
| 30 | EXPECT_FALSE(Value.isTypedMatcher<clang::Decl>()); |
| 31 | EXPECT_FALSE(Value.isTypedMatcher<clang::UnaryOperator>()); |
| 32 | } |
| 33 | |
| 34 | TEST(VariantValueTest, DynTypedMatcher) { |
| 35 | VariantValue Value = stmt(); |
| 36 | |
| 37 | EXPECT_FALSE(Value.isString()); |
| 38 | |
| 39 | EXPECT_TRUE(Value.isMatcher()); |
| 40 | EXPECT_TRUE(Value.isTypedMatcher<clang::Decl>()); |
| 41 | EXPECT_TRUE(Value.isTypedMatcher<clang::UnaryOperator>()); |
| 42 | |
| 43 | // Conversion to any type of matcher works. |
| 44 | // If they are not compatible it would just return a matcher that matches |
| 45 | // nothing. We test this below. |
| 46 | Value = recordDecl(); |
| 47 | EXPECT_TRUE(Value.isMatcher()); |
| 48 | EXPECT_TRUE(Value.isTypedMatcher<clang::Decl>()); |
| 49 | EXPECT_TRUE(Value.isTypedMatcher<clang::UnaryOperator>()); |
| 50 | |
| 51 | Value = unaryOperator(); |
| 52 | EXPECT_TRUE(Value.isMatcher()); |
| 53 | EXPECT_TRUE(Value.isTypedMatcher<clang::Decl>()); |
| 54 | EXPECT_TRUE(Value.isTypedMatcher<clang::Stmt>()); |
| 55 | EXPECT_TRUE(Value.isTypedMatcher<clang::UnaryOperator>()); |
| 56 | } |
| 57 | |
| 58 | TEST(VariantValueTest, Assignment) { |
| 59 | VariantValue Value = std::string("A"); |
| 60 | EXPECT_TRUE(Value.isString()); |
| 61 | EXPECT_EQ("A", Value.getString()); |
| 62 | EXPECT_FALSE(Value.isMatcher()); |
| 63 | |
| 64 | Value = recordDecl(); |
| 65 | EXPECT_FALSE(Value.isString()); |
| 66 | EXPECT_TRUE(Value.isMatcher()); |
| 67 | EXPECT_TRUE(Value.isTypedMatcher<clang::Decl>()); |
| 68 | EXPECT_TRUE(Value.isTypedMatcher<clang::UnaryOperator>()); |
| 69 | |
| 70 | Value = VariantValue(); |
| 71 | EXPECT_FALSE(Value.isString()); |
| 72 | EXPECT_FALSE(Value.isMatcher()); |
| 73 | } |
| 74 | |
| 75 | TEST(GeneicValueTest, Matcher) { |
| 76 | EXPECT_TRUE(matchesDynamic( |
| 77 | "class X {};", VariantValue(recordDecl(hasName("X"))).getMatcher())); |
| 78 | EXPECT_TRUE(matchesDynamic( |
| 79 | "int x;", VariantValue(varDecl()).getTypedMatcher<clang::Decl>())); |
| 80 | EXPECT_TRUE(matchesDynamic("int foo() { return 1 + 1; }", |
| 81 | VariantValue(functionDecl()).getMatcher())); |
| 82 | // Going through the wrong Matcher<T> will fail to match, even if the |
| 83 | // underlying matcher is correct. |
| 84 | EXPECT_FALSE(matchesDynamic( |
| 85 | "int x;", VariantValue(varDecl()).getTypedMatcher<clang::Stmt>())); |
| 86 | |
| 87 | EXPECT_FALSE( |
| 88 | matchesDynamic("int x;", VariantValue(functionDecl()).getMatcher())); |
| 89 | EXPECT_FALSE(matchesDynamic( |
| 90 | "int foo() { return 1 + 1; }", |
| 91 | VariantValue(declRefExpr()).getTypedMatcher<clang::DeclRefExpr>())); |
| 92 | } |
| 93 | |
| 94 | } // end anonymous namespace |
| 95 | } // end namespace dynamic |
| 96 | } // end namespace ast_matchers |
| 97 | } // end namespace clang |