Manuel Klimek | 24db0f0 | 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 | c31b352 | 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 | |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 29 | EXPECT_TRUE(Value.hasValue()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 30 | EXPECT_FALSE(Value.isString()); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 31 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 34 | TEST(VariantValueTest, String) { |
Yaron Keren | 5b81606 | 2015-07-06 08:47:15 +0000 | [diff] [blame^] | 35 | const StringRef kString = "string"; |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 36 | VariantValue Value = kString; |
| 37 | |
| 38 | EXPECT_TRUE(Value.isString()); |
| 39 | EXPECT_EQ(kString, Value.getString()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 40 | EXPECT_EQ("String", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 41 | |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 42 | EXPECT_TRUE(Value.hasValue()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 43 | EXPECT_FALSE(Value.isUnsigned()); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 44 | EXPECT_FALSE(Value.isMatcher()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | TEST(VariantValueTest, DynTypedMatcher) { |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 48 | VariantValue Value = VariantMatcher::SingleMatcher(stmt()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 49 | |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 50 | EXPECT_TRUE(Value.hasValue()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 51 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 52 | EXPECT_FALSE(Value.isString()); |
| 53 | |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 54 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 55 | EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 56 | EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<UnaryOperator>()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 57 | EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 58 | |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 59 | // Can only convert to compatible matchers. |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 60 | Value = VariantMatcher::SingleMatcher(recordDecl()); |
| 61 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 62 | EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 63 | EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 64 | EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 65 | |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 66 | Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr())); |
| 67 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 68 | EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 69 | EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Stmt>()); |
| 70 | EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Expr>()); |
| 71 | EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<IntegerLiteral>()); |
| 72 | EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<GotoStmt>()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 73 | EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | TEST(VariantValueTest, Assignment) { |
Yaron Keren | 5b81606 | 2015-07-06 08:47:15 +0000 | [diff] [blame^] | 77 | VariantValue Value = StringRef("A"); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 78 | EXPECT_TRUE(Value.isString()); |
| 79 | EXPECT_EQ("A", Value.getString()); |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 80 | EXPECT_TRUE(Value.hasValue()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 81 | EXPECT_FALSE(Value.isUnsigned()); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 82 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 83 | EXPECT_EQ("String", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 84 | |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 85 | Value = VariantMatcher::SingleMatcher(recordDecl()); |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 86 | EXPECT_TRUE(Value.hasValue()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 87 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 88 | EXPECT_FALSE(Value.isString()); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 89 | EXPECT_TRUE(Value.isMatcher()); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 90 | EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>()); |
| 91 | EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 92 | EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 93 | |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 94 | Value = 17; |
| 95 | EXPECT_TRUE(Value.isUnsigned()); |
| 96 | EXPECT_EQ(17U, Value.getUnsigned()); |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 97 | EXPECT_TRUE(Value.hasValue()); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 98 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 99 | EXPECT_FALSE(Value.isString()); |
| 100 | |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 101 | Value = VariantValue(); |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 102 | EXPECT_FALSE(Value.hasValue()); |
Samuel Benzaquen | c31b352 | 2013-06-04 15:46:22 +0000 | [diff] [blame] | 103 | EXPECT_FALSE(Value.isUnsigned()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 104 | EXPECT_FALSE(Value.isString()); |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 105 | EXPECT_FALSE(Value.isMatcher()); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 106 | EXPECT_EQ("Nothing", Value.getTypeAsString()); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 109 | TEST(VariantValueTest, ImplicitBool) { |
| 110 | VariantValue Value; |
| 111 | bool IfTrue = false; |
| 112 | if (Value) { |
| 113 | IfTrue = true; |
| 114 | } |
| 115 | EXPECT_FALSE(IfTrue); |
| 116 | EXPECT_TRUE(!Value); |
| 117 | |
Yaron Keren | 5b81606 | 2015-07-06 08:47:15 +0000 | [diff] [blame^] | 118 | Value = StringRef(); |
Samuel Benzaquen | f434c4f | 2014-04-14 13:51:21 +0000 | [diff] [blame] | 119 | IfTrue = false; |
| 120 | if (Value) { |
| 121 | IfTrue = true; |
| 122 | } |
| 123 | EXPECT_TRUE(IfTrue); |
| 124 | EXPECT_FALSE(!Value); |
| 125 | } |
| 126 | |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 127 | TEST(VariantValueTest, Matcher) { |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 128 | EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher( |
| 129 | recordDecl(hasName("X")))) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 130 | .getMatcher() |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 131 | .getTypedMatcher<Decl>())); |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 132 | EXPECT_TRUE( |
| 133 | matches("int x;", VariantValue(VariantMatcher::SingleMatcher(varDecl())) |
| 134 | .getMatcher() |
| 135 | .getTypedMatcher<Decl>())); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 136 | EXPECT_TRUE( |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 137 | matches("int foo() { return 1 + 1; }", |
| 138 | VariantValue(VariantMatcher::SingleMatcher(functionDecl())) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 139 | .getMatcher() |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 140 | .getTypedMatcher<Decl>())); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 141 | // Can't get the wrong matcher. |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 142 | EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl())) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 143 | .getMatcher() |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 144 | .hasTypedMatcher<Stmt>()); |
NAKAMURA Takumi | 9b435fe | 2014-01-12 17:49:26 +0000 | [diff] [blame] | 145 | #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST |
Reid Kleckner | f9794c1 | 2013-06-21 12:58:12 +0000 | [diff] [blame] | 146 | // Trying to get the wrong matcher fails an assertion in Matcher<T>. We don't |
| 147 | // do this test when building with MSVC because its debug C runtime prints the |
| 148 | // assertion failure message as a wide string, which gtest doesn't understand. |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 149 | EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl())) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 150 | .getMatcher() |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 151 | .getTypedMatcher<Stmt>(), |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 152 | "hasTypedMatcher"); |
Samuel Benzaquen | 81ef929 | 2013-06-20 14:28:32 +0000 | [diff] [blame] | 153 | #endif |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 154 | |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 155 | EXPECT_FALSE(matches( |
| 156 | "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl())) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 157 | .getMatcher() |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 158 | .getTypedMatcher<Decl>())); |
Samuel Benzaquen | c6f2c9b | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 159 | EXPECT_FALSE( |
| 160 | matches("int foo() { return 1 + 1; }", |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 161 | VariantValue(VariantMatcher::SingleMatcher(declRefExpr())) |
Samuel Benzaquen | 998cda23 | 2013-08-30 15:09:52 +0000 | [diff] [blame] | 162 | .getMatcher() |
Samuel Benzaquen | 0239b69 | 2013-08-13 14:54:51 +0000 | [diff] [blame] | 163 | .getTypedMatcher<Stmt>())); |
Manuel Klimek | 24db0f0 | 2013-05-14 09:13:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | } // end anonymous namespace |
| 167 | } // end namespace dynamic |
| 168 | } // end namespace ast_matchers |
| 169 | } // end namespace clang |