xref: /minix3/external/bsd/llvm/dist/clang/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- unittest/ASTMatchers/Dynamic/VariantValueTest.cpp - VariantValue unit tests -===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===-----------------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "../ASTMatchersTest.h"
11f4a2713aSLionel Sambuc #include "clang/ASTMatchers/Dynamic/VariantValue.h"
12f4a2713aSLionel Sambuc #include "gtest/gtest.h"
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc namespace clang {
15f4a2713aSLionel Sambuc namespace ast_matchers {
16f4a2713aSLionel Sambuc namespace dynamic {
17f4a2713aSLionel Sambuc namespace {
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc using ast_matchers::internal::DynTypedMatcher;
20f4a2713aSLionel Sambuc using ast_matchers::internal::Matcher;
21f4a2713aSLionel Sambuc 
TEST(VariantValueTest,Unsigned)22f4a2713aSLionel Sambuc TEST(VariantValueTest, Unsigned) {
23f4a2713aSLionel Sambuc   const unsigned kUnsigned = 17;
24f4a2713aSLionel Sambuc   VariantValue Value = kUnsigned;
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isUnsigned());
27f4a2713aSLionel Sambuc   EXPECT_EQ(kUnsigned, Value.getUnsigned());
28f4a2713aSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Value.hasValue());
30f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isString());
31f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isMatcher());
32f4a2713aSLionel Sambuc }
33f4a2713aSLionel Sambuc 
TEST(VariantValueTest,String)34f4a2713aSLionel Sambuc TEST(VariantValueTest, String) {
35f4a2713aSLionel Sambuc   const ::std::string kString = "string";
36f4a2713aSLionel Sambuc   VariantValue Value = kString;
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isString());
39f4a2713aSLionel Sambuc   EXPECT_EQ(kString, Value.getString());
40f4a2713aSLionel Sambuc   EXPECT_EQ("String", Value.getTypeAsString());
41f4a2713aSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Value.hasValue());
43f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isUnsigned());
44f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isMatcher());
45f4a2713aSLionel Sambuc }
46f4a2713aSLionel Sambuc 
TEST(VariantValueTest,DynTypedMatcher)47f4a2713aSLionel Sambuc TEST(VariantValueTest, DynTypedMatcher) {
48f4a2713aSLionel Sambuc   VariantValue Value = VariantMatcher::SingleMatcher(stmt());
49f4a2713aSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Value.hasValue());
51f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isUnsigned());
52f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isString());
53f4a2713aSLionel Sambuc 
54f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isMatcher());
55f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>());
56f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
57f4a2713aSLionel Sambuc   EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString());
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc   // Can only convert to compatible matchers.
60f4a2713aSLionel Sambuc   Value = VariantMatcher::SingleMatcher(recordDecl());
61f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isMatcher());
62f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>());
63f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
64f4a2713aSLionel Sambuc   EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr()));
67f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isMatcher());
68f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>());
69f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Stmt>());
70f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Expr>());
71f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<IntegerLiteral>());
72f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<GotoStmt>());
73f4a2713aSLionel Sambuc   EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString());
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
TEST(VariantValueTest,Assignment)76f4a2713aSLionel Sambuc TEST(VariantValueTest, Assignment) {
77f4a2713aSLionel Sambuc   VariantValue Value = std::string("A");
78f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isString());
79f4a2713aSLionel Sambuc   EXPECT_EQ("A", Value.getString());
80*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Value.hasValue());
81f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isUnsigned());
82f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isMatcher());
83f4a2713aSLionel Sambuc   EXPECT_EQ("String", Value.getTypeAsString());
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   Value = VariantMatcher::SingleMatcher(recordDecl());
86*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Value.hasValue());
87f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isUnsigned());
88f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isString());
89f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isMatcher());
90f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>());
91f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
92f4a2713aSLionel Sambuc   EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   Value = 17;
95f4a2713aSLionel Sambuc   EXPECT_TRUE(Value.isUnsigned());
96f4a2713aSLionel Sambuc   EXPECT_EQ(17U, Value.getUnsigned());
97*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(Value.hasValue());
98f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isMatcher());
99f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isString());
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc   Value = VariantValue();
102*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(Value.hasValue());
103f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isUnsigned());
104f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isString());
105f4a2713aSLionel Sambuc   EXPECT_FALSE(Value.isMatcher());
106f4a2713aSLionel Sambuc   EXPECT_EQ("Nothing", Value.getTypeAsString());
107f4a2713aSLionel Sambuc }
108f4a2713aSLionel Sambuc 
TEST(VariantValueTest,ImplicitBool)109*0a6a1f1dSLionel Sambuc TEST(VariantValueTest, ImplicitBool) {
110*0a6a1f1dSLionel Sambuc   VariantValue Value;
111*0a6a1f1dSLionel Sambuc   bool IfTrue = false;
112*0a6a1f1dSLionel Sambuc   if (Value) {
113*0a6a1f1dSLionel Sambuc     IfTrue = true;
114*0a6a1f1dSLionel Sambuc   }
115*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(IfTrue);
116*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(!Value);
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc   Value = std::string();
119*0a6a1f1dSLionel Sambuc   IfTrue = false;
120*0a6a1f1dSLionel Sambuc   if (Value) {
121*0a6a1f1dSLionel Sambuc     IfTrue = true;
122*0a6a1f1dSLionel Sambuc   }
123*0a6a1f1dSLionel Sambuc   EXPECT_TRUE(IfTrue);
124*0a6a1f1dSLionel Sambuc   EXPECT_FALSE(!Value);
125*0a6a1f1dSLionel Sambuc }
126*0a6a1f1dSLionel Sambuc 
TEST(VariantValueTest,Matcher)127f4a2713aSLionel Sambuc TEST(VariantValueTest, Matcher) {
128f4a2713aSLionel Sambuc   EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher(
129f4a2713aSLionel Sambuc                                                       recordDecl(hasName("X"))))
130f4a2713aSLionel Sambuc                                          .getMatcher()
131f4a2713aSLionel Sambuc                                          .getTypedMatcher<Decl>()));
132f4a2713aSLionel Sambuc   EXPECT_TRUE(
133f4a2713aSLionel Sambuc       matches("int x;", VariantValue(VariantMatcher::SingleMatcher(varDecl()))
134f4a2713aSLionel Sambuc                             .getMatcher()
135f4a2713aSLionel Sambuc                             .getTypedMatcher<Decl>()));
136f4a2713aSLionel Sambuc   EXPECT_TRUE(
137f4a2713aSLionel Sambuc       matches("int foo() { return 1 + 1; }",
138f4a2713aSLionel Sambuc               VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
139f4a2713aSLionel Sambuc                   .getMatcher()
140f4a2713aSLionel Sambuc                   .getTypedMatcher<Decl>()));
141f4a2713aSLionel Sambuc   // Can't get the wrong matcher.
142f4a2713aSLionel Sambuc   EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
143f4a2713aSLionel Sambuc                    .getMatcher()
144f4a2713aSLionel Sambuc                    .hasTypedMatcher<Stmt>());
145*0a6a1f1dSLionel Sambuc #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
146f4a2713aSLionel Sambuc   // Trying to get the wrong matcher fails an assertion in Matcher<T>.  We don't
147f4a2713aSLionel Sambuc   // do this test when building with MSVC because its debug C runtime prints the
148f4a2713aSLionel Sambuc   // assertion failure message as a wide string, which gtest doesn't understand.
149f4a2713aSLionel Sambuc   EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
150f4a2713aSLionel Sambuc                    .getMatcher()
151f4a2713aSLionel Sambuc                    .getTypedMatcher<Stmt>(),
152f4a2713aSLionel Sambuc                "hasTypedMatcher");
153f4a2713aSLionel Sambuc #endif
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc   EXPECT_FALSE(matches(
156f4a2713aSLionel Sambuc       "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
157f4a2713aSLionel Sambuc                     .getMatcher()
158f4a2713aSLionel Sambuc                     .getTypedMatcher<Decl>()));
159f4a2713aSLionel Sambuc   EXPECT_FALSE(
160f4a2713aSLionel Sambuc       matches("int foo() { return 1 + 1; }",
161f4a2713aSLionel Sambuc               VariantValue(VariantMatcher::SingleMatcher(declRefExpr()))
162f4a2713aSLionel Sambuc                   .getMatcher()
163f4a2713aSLionel Sambuc                   .getTypedMatcher<Stmt>()));
164f4a2713aSLionel Sambuc }
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc } // end anonymous namespace
167f4a2713aSLionel Sambuc } // end namespace dynamic
168f4a2713aSLionel Sambuc } // end namespace ast_matchers
169f4a2713aSLionel Sambuc } // end namespace clang
170