1 //===- unittest/ASTMatchers/Dynamic/VariantValueTest.cpp - VariantValue unit tests -===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===-----------------------------------------------------------------------------===//
9 #include "../ASTMatchersTest.h"
10 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
11 #include "gtest/gtest.h"
14 namespace ast_matchers
{
18 TEST(VariantValueTest
, Unsigned
) {
19 const unsigned kUnsigned
= 17;
20 VariantValue Value
= kUnsigned
;
22 EXPECT_TRUE(Value
.isUnsigned());
23 EXPECT_EQ(kUnsigned
, Value
.getUnsigned());
25 EXPECT_TRUE(Value
.hasValue());
26 EXPECT_FALSE(Value
.isString());
27 EXPECT_FALSE(Value
.isMatcher());
30 TEST(VariantValueTest
, String
) {
31 const StringRef kString
= "string";
32 VariantValue Value
= kString
;
34 EXPECT_TRUE(Value
.isString());
35 EXPECT_EQ(kString
, Value
.getString());
36 EXPECT_EQ("String", Value
.getTypeAsString());
38 EXPECT_TRUE(Value
.hasValue());
39 EXPECT_FALSE(Value
.isUnsigned());
40 EXPECT_FALSE(Value
.isMatcher());
43 TEST(VariantValueTest
, DynTypedMatcher
) {
44 VariantValue Value
= VariantMatcher::SingleMatcher(stmt());
46 EXPECT_TRUE(Value
.hasValue());
47 EXPECT_FALSE(Value
.isUnsigned());
48 EXPECT_FALSE(Value
.isString());
50 EXPECT_TRUE(Value
.isMatcher());
51 EXPECT_FALSE(Value
.getMatcher().hasTypedMatcher
<Decl
>());
52 EXPECT_TRUE(Value
.getMatcher().hasTypedMatcher
<UnaryOperator
>());
53 EXPECT_EQ("Matcher<Stmt>", Value
.getTypeAsString());
55 // Can only convert to compatible matchers.
56 Value
= VariantMatcher::SingleMatcher(recordDecl());
57 EXPECT_TRUE(Value
.isMatcher());
58 EXPECT_TRUE(Value
.getMatcher().hasTypedMatcher
<Decl
>());
59 EXPECT_FALSE(Value
.getMatcher().hasTypedMatcher
<UnaryOperator
>());
60 EXPECT_EQ("Matcher<Decl>", Value
.getTypeAsString());
62 Value
= VariantMatcher::SingleMatcher(ignoringImpCasts(expr()));
63 EXPECT_TRUE(Value
.isMatcher());
64 EXPECT_FALSE(Value
.getMatcher().hasTypedMatcher
<Decl
>());
65 EXPECT_FALSE(Value
.getMatcher().hasTypedMatcher
<Stmt
>());
66 EXPECT_TRUE(Value
.getMatcher().hasTypedMatcher
<Expr
>());
67 EXPECT_TRUE(Value
.getMatcher().hasTypedMatcher
<IntegerLiteral
>());
68 EXPECT_FALSE(Value
.getMatcher().hasTypedMatcher
<GotoStmt
>());
69 EXPECT_EQ("Matcher<Expr>", Value
.getTypeAsString());
72 TEST(VariantValueTest
, Assignment
) {
73 VariantValue Value
= StringRef("A");
74 EXPECT_TRUE(Value
.isString());
75 EXPECT_EQ("A", Value
.getString());
76 EXPECT_TRUE(Value
.hasValue());
77 EXPECT_FALSE(Value
.isBoolean());
78 EXPECT_FALSE(Value
.isDouble());
79 EXPECT_FALSE(Value
.isUnsigned());
80 EXPECT_FALSE(Value
.isMatcher());
81 EXPECT_EQ("String", Value
.getTypeAsString());
83 Value
= VariantMatcher::SingleMatcher(recordDecl());
84 EXPECT_TRUE(Value
.hasValue());
85 EXPECT_FALSE(Value
.isBoolean());
86 EXPECT_FALSE(Value
.isDouble());
87 EXPECT_FALSE(Value
.isUnsigned());
88 EXPECT_FALSE(Value
.isString());
89 EXPECT_TRUE(Value
.isMatcher());
90 EXPECT_TRUE(Value
.getMatcher().hasTypedMatcher
<Decl
>());
91 EXPECT_FALSE(Value
.getMatcher().hasTypedMatcher
<UnaryOperator
>());
92 EXPECT_EQ("Matcher<Decl>", Value
.getTypeAsString());
95 EXPECT_TRUE(Value
.isBoolean());
96 EXPECT_EQ(true, Value
.getBoolean());
97 EXPECT_TRUE(Value
.hasValue());
98 EXPECT_FALSE(Value
.isUnsigned());
99 EXPECT_FALSE(Value
.isMatcher());
100 EXPECT_FALSE(Value
.isString());
103 EXPECT_TRUE(Value
.isDouble());
104 EXPECT_EQ(3.14, Value
.getDouble());
105 EXPECT_TRUE(Value
.hasValue());
106 EXPECT_FALSE(Value
.isBoolean());
107 EXPECT_FALSE(Value
.isUnsigned());
108 EXPECT_FALSE(Value
.isMatcher());
109 EXPECT_FALSE(Value
.isString());
112 EXPECT_TRUE(Value
.isUnsigned());
113 EXPECT_EQ(17U, Value
.getUnsigned());
114 EXPECT_FALSE(Value
.isBoolean());
115 EXPECT_FALSE(Value
.isDouble());
116 EXPECT_TRUE(Value
.hasValue());
117 EXPECT_FALSE(Value
.isMatcher());
118 EXPECT_FALSE(Value
.isString());
120 Value
= VariantValue();
121 EXPECT_FALSE(Value
.hasValue());
122 EXPECT_FALSE(Value
.isBoolean());
123 EXPECT_FALSE(Value
.isDouble());
124 EXPECT_FALSE(Value
.isUnsigned());
125 EXPECT_FALSE(Value
.isString());
126 EXPECT_FALSE(Value
.isMatcher());
127 EXPECT_EQ("Nothing", Value
.getTypeAsString());
130 TEST(VariantValueTest
, ImplicitBool
) {
136 EXPECT_FALSE(IfTrue
);
145 EXPECT_FALSE(!Value
);
148 TEST(VariantValueTest
, Matcher
) {
149 EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher(
150 recordDecl(hasName("X"))))
152 .getTypedMatcher
<Decl
>()));
154 matches("int x;", VariantValue(VariantMatcher::SingleMatcher(varDecl()))
156 .getTypedMatcher
<Decl
>()));
158 matches("int foo() { return 1 + 1; }",
159 VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
161 .getTypedMatcher
<Decl
>()));
162 // Can't get the wrong matcher.
163 EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
165 .hasTypedMatcher
<Stmt
>());
166 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
167 // Trying to get the wrong matcher fails an assertion in Matcher<T>. We don't
168 // do this test when building with MSVC because its debug C runtime prints the
169 // assertion failure message as a wide string, which gtest doesn't understand.
170 EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
172 .getTypedMatcher
<Stmt
>(),
176 EXPECT_FALSE(matches(
177 "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
179 .getTypedMatcher
<Decl
>()));
181 matches("int foo() { return 1 + 1; }",
182 VariantValue(VariantMatcher::SingleMatcher(declRefExpr()))
184 .getTypedMatcher
<Stmt
>()));
187 TEST(VariantValueTest
, NodeKind
) {
188 VariantValue Value
= ASTNodeKind::getFromNodeKind
<Stmt
>();
189 EXPECT_TRUE(Value
.isNodeKind());
190 EXPECT_TRUE(Value
.getNodeKind().isSame(ASTNodeKind::getFromNodeKind
<Stmt
>()));
192 Value
= ASTNodeKind::getFromNodeKind
<CXXMethodDecl
>();
193 EXPECT_TRUE(Value
.isNodeKind());
194 EXPECT_TRUE(Value
.getNodeKind().isSame(
195 ASTNodeKind::getFromNodeKind
<CXXMethodDecl
>()));
197 Value
.setNodeKind(ASTNodeKind::getFromNodeKind
<PointerType
>());
198 EXPECT_TRUE(Value
.isNodeKind());
200 Value
.getNodeKind().isSame(ASTNodeKind::getFromNodeKind
<PointerType
>()));
203 EXPECT_TRUE(!Value
.isNodeKind());
206 } // end anonymous namespace
207 } // end namespace dynamic
208 } // end namespace ast_matchers
209 } // end namespace clang