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()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 30 | EXPECT_FALSE(Value.isMatchers()); |
| 31 | EXPECT_FALSE(Value.hasTypedMatcher<Decl>()); |
| 32 | EXPECT_FALSE(Value.hasTypedMatcher<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()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 44 | EXPECT_FALSE(Value.isMatchers()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST(VariantValueTest, DynTypedMatcher) { |
| 48 | VariantValue Value = stmt(); |
| 49 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 50 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 51 | EXPECT_FALSE(Value.isString()); |
| 52 | |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 53 | EXPECT_TRUE(Value.isMatchers()); |
| 54 | EXPECT_FALSE(Value.hasTypedMatcher<Decl>()); |
| 55 | EXPECT_TRUE(Value.hasTypedMatcher<UnaryOperator>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 56 | EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 57 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 58 | // Can only convert to compatible matchers. |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 59 | Value = recordDecl(); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 60 | EXPECT_TRUE(Value.isMatchers()); |
| 61 | EXPECT_TRUE(Value.hasTypedMatcher<Decl>()); |
| 62 | EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 63 | EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 64 | |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 65 | Value = ignoringImpCasts(expr()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 66 | EXPECT_TRUE(Value.isMatchers()); |
| 67 | EXPECT_FALSE(Value.hasTypedMatcher<Decl>()); |
| 68 | EXPECT_FALSE(Value.hasTypedMatcher<Stmt>()); |
| 69 | EXPECT_TRUE(Value.hasTypedMatcher<Expr>()); |
| 70 | EXPECT_TRUE(Value.hasTypedMatcher<IntegerLiteral>()); |
| 71 | EXPECT_FALSE(Value.hasTypedMatcher<GotoStmt>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 72 | EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST(VariantValueTest, Assignment) { |
| 76 | VariantValue Value = std::string("A"); |
| 77 | EXPECT_TRUE(Value.isString()); |
| 78 | EXPECT_EQ("A", Value.getString()); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 79 | EXPECT_FALSE(Value.isUnsigned()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 80 | EXPECT_FALSE(Value.isMatchers()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 81 | EXPECT_EQ("String", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 82 | |
| 83 | Value = recordDecl(); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 84 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 85 | EXPECT_FALSE(Value.isString()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 86 | EXPECT_TRUE(Value.isMatchers()); |
| 87 | EXPECT_TRUE(Value.hasTypedMatcher<Decl>()); |
| 88 | EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 89 | EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 90 | |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 91 | Value = 17; |
| 92 | EXPECT_TRUE(Value.isUnsigned()); |
| 93 | EXPECT_EQ(17U, Value.getUnsigned()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 94 | EXPECT_FALSE(Value.isMatchers()); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 95 | EXPECT_FALSE(Value.isString()); |
| 96 | |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 97 | Value = VariantValue(); |
Samuel Benzaquen | 7a337af | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 98 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 99 | EXPECT_FALSE(Value.isString()); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 100 | EXPECT_FALSE(Value.isMatchers()); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 101 | EXPECT_EQ("Nothing", Value.getTypeAsString()); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 104 | TEST(VariantValueTest, Matcher) { |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 105 | EXPECT_TRUE(matches("class X {};", VariantValue(recordDecl(hasName("X"))) |
| 106 | .getTypedMatcher<Decl>())); |
| 107 | EXPECT_TRUE( |
| 108 | matches("int x;", VariantValue(varDecl()).getTypedMatcher<Decl>())); |
| 109 | EXPECT_TRUE(matches("int foo() { return 1 + 1; }", |
| 110 | VariantValue(functionDecl()).getTypedMatcher<Decl>())); |
| 111 | // Can't get the wrong matcher. |
| 112 | EXPECT_FALSE(VariantValue(varDecl()).hasTypedMatcher<Stmt>()); |
Reid Kleckner | 8711da1 | 2013-06-21 12:58:12 +0000 | [diff] [blame] | 113 | #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST && !defined(_MSC_VER) |
| 114 | // Trying to get the wrong matcher fails an assertion in Matcher<T>. We don't |
| 115 | // do this test when building with MSVC because its debug C runtime prints the |
| 116 | // assertion failure message as a wide string, which gtest doesn't understand. |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 117 | EXPECT_DEATH(VariantValue(varDecl()).getTypedMatcher<Stmt>(), |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 118 | "hasTypedMatcher"); |
Samuel Benzaquen | 76c2f92 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 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>())); |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame^] | 123 | EXPECT_FALSE( |
| 124 | matches("int foo() { return 1 + 1; }", |
| 125 | |
| 126 | VariantValue(declRefExpr()).getTypedMatcher<Stmt>())); |
Manuel Klimek | f7f295f | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | } // end anonymous namespace |
| 130 | } // end namespace dynamic |
| 131 | } // end namespace ast_matchers |
| 132 | } // end namespace clang |