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 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 22 | TEST(VariantValueTest, Unsigned) { |
| 23 | const unsigned kUnsigned = 17; |
| 24 | VariantValue Value = kUnsigned; |
| 25 | |
| 26 | EXPECT_TRUE(Value.isUnsigned()); |
| 27 | EXPECT_EQ(kUnsigned, Value.getUnsigned()); |
| 28 | |
| 29 | EXPECT_FALSE(Value.isString()); |
| 30 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 31 | EXPECT_FALSE(Value.hasTypedMatcher<clang::Decl>()); |
| 32 | EXPECT_FALSE(Value.hasTypedMatcher<clang::UnaryOperator>()); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 35 | TEST(VariantValueTest, String) { |
| 36 | const ::std::string kString = "string"; |
| 37 | VariantValue Value = kString; |
| 38 | |
| 39 | EXPECT_TRUE(Value.isString()); |
| 40 | EXPECT_EQ(kString, Value.getString()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 41 | EXPECT_EQ("String", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 42 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 43 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 44 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 45 | EXPECT_FALSE(Value.hasTypedMatcher<clang::Decl>()); |
| 46 | EXPECT_FALSE(Value.hasTypedMatcher<clang::UnaryOperator>()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(VariantValueTest, DynTypedMatcher) { |
| 50 | VariantValue Value = stmt(); |
| 51 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 52 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 53 | EXPECT_FALSE(Value.isString()); |
| 54 | |
| 55 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 56 | EXPECT_FALSE(Value.hasTypedMatcher<clang::Decl>()); |
| 57 | EXPECT_TRUE(Value.hasTypedMatcher<clang::UnaryOperator>()); |
| 58 | EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 59 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 60 | // Can only convert to compatible matchers. |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 61 | Value = recordDecl(); |
| 62 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 63 | EXPECT_TRUE(Value.hasTypedMatcher<clang::Decl>()); |
| 64 | EXPECT_FALSE(Value.hasTypedMatcher<clang::UnaryOperator>()); |
| 65 | EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 66 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 67 | Value = ignoringImpCasts(expr()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 68 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 69 | EXPECT_FALSE(Value.hasTypedMatcher<clang::Decl>()); |
| 70 | EXPECT_FALSE(Value.hasTypedMatcher<clang::Stmt>()); |
| 71 | EXPECT_TRUE(Value.hasTypedMatcher<clang::Expr>()); |
| 72 | EXPECT_TRUE(Value.hasTypedMatcher<clang::IntegerLiteral>()); |
| 73 | EXPECT_FALSE(Value.hasTypedMatcher<clang::GotoStmt>()); |
| 74 | EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(VariantValueTest, Assignment) { |
| 78 | VariantValue Value = std::string("A"); |
| 79 | EXPECT_TRUE(Value.isString()); |
| 80 | EXPECT_EQ("A", Value.getString()); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 81 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 82 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 83 | EXPECT_EQ("String", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 84 | |
| 85 | Value = recordDecl(); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 86 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 87 | EXPECT_FALSE(Value.isString()); |
| 88 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 89 | EXPECT_TRUE(Value.hasTypedMatcher<clang::Decl>()); |
| 90 | EXPECT_FALSE(Value.hasTypedMatcher<clang::UnaryOperator>()); |
| 91 | EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 92 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 93 | Value = 17; |
| 94 | EXPECT_TRUE(Value.isUnsigned()); |
| 95 | EXPECT_EQ(17U, Value.getUnsigned()); |
| 96 | EXPECT_FALSE(Value.isMatcher()); |
| 97 | EXPECT_FALSE(Value.isString()); |
| 98 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 99 | Value = VariantValue(); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 100 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 101 | EXPECT_FALSE(Value.isString()); |
| 102 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 103 | EXPECT_EQ("Nothing", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST(GeneicValueTest, Matcher) { |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 107 | EXPECT_TRUE(matches("class X {};", VariantValue(recordDecl(hasName("X"))) |
| 108 | .getTypedMatcher<Decl>())); |
| 109 | EXPECT_TRUE( |
| 110 | matches("int x;", VariantValue(varDecl()).getTypedMatcher<Decl>())); |
| 111 | EXPECT_TRUE(matches("int foo() { return 1 + 1; }", |
| 112 | VariantValue(functionDecl()).getTypedMatcher<Decl>())); |
| 113 | // Can't get the wrong matcher. |
| 114 | EXPECT_FALSE(VariantValue(varDecl()).hasTypedMatcher<Stmt>()); |
| 115 | #if GTEST_HAS_DEATH_TEST and DEBUG |
| 116 | // Trying to get the wrong matcher fails an assertion in Matcher<T>. |
| 117 | EXPECT_DEATH(VariantValue(varDecl()).getTypedMatcher<Stmt>(), |
| 118 | "canConstructFrom"); |
| 119 | #endif |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 120 | |
| 121 | EXPECT_FALSE( |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame^] | 122 | matches("int x;", VariantValue(functionDecl()).getTypedMatcher<Decl>())); |
| 123 | EXPECT_FALSE(matches("int foo() { return 1 + 1; }", |
| 124 | VariantValue(declRefExpr()).getTypedMatcher<Stmt>())); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // end anonymous namespace |
| 128 | } // end namespace dynamic |
| 129 | } // end namespace ast_matchers |
| 130 | } // end namespace clang |