VariantValueTest.cpp revision 9d02807c3ea9782442b98201df68294cd7cd7313
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
14namespace clang {
15namespace ast_matchers {
16namespace dynamic {
17namespace {
18
19using ast_matchers::internal::DynTypedMatcher;
20using ast_matchers::internal::Matcher;
21
22TEST(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());
31  EXPECT_FALSE(Value.hasTypedMatcher<Decl>());
32  EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>());
33}
34
35TEST(VariantValueTest, String) {
36  const ::std::string kString = "string";
37  VariantValue Value = kString;
38
39  EXPECT_TRUE(Value.isString());
40  EXPECT_EQ(kString, Value.getString());
41  EXPECT_EQ("String", Value.getTypeAsString());
42
43  EXPECT_FALSE(Value.isUnsigned());
44  EXPECT_FALSE(Value.isMatcher());
45}
46
47TEST(VariantValueTest, DynTypedMatcher) {
48  VariantValue Value = VariantMatcher::SingleMatcher(stmt());
49
50  EXPECT_FALSE(Value.isUnsigned());
51  EXPECT_FALSE(Value.isString());
52
53  EXPECT_TRUE(Value.isMatcher());
54  EXPECT_FALSE(Value.hasTypedMatcher<Decl>());
55  EXPECT_TRUE(Value.hasTypedMatcher<UnaryOperator>());
56  EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString());
57
58  // Can only convert to compatible matchers.
59  Value = VariantMatcher::SingleMatcher(recordDecl());
60  EXPECT_TRUE(Value.isMatcher());
61  EXPECT_TRUE(Value.hasTypedMatcher<Decl>());
62  EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>());
63  EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
64
65  Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr()));
66  EXPECT_TRUE(Value.isMatcher());
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>());
72  EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString());
73}
74
75TEST(VariantValueTest, Assignment) {
76  VariantValue Value = std::string("A");
77  EXPECT_TRUE(Value.isString());
78  EXPECT_EQ("A", Value.getString());
79  EXPECT_FALSE(Value.isUnsigned());
80  EXPECT_FALSE(Value.isMatcher());
81  EXPECT_EQ("String", Value.getTypeAsString());
82
83  Value = VariantMatcher::SingleMatcher(recordDecl());
84  EXPECT_FALSE(Value.isUnsigned());
85  EXPECT_FALSE(Value.isString());
86  EXPECT_TRUE(Value.isMatcher());
87  EXPECT_TRUE(Value.hasTypedMatcher<Decl>());
88  EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>());
89  EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
90
91  Value = 17;
92  EXPECT_TRUE(Value.isUnsigned());
93  EXPECT_EQ(17U, Value.getUnsigned());
94  EXPECT_FALSE(Value.isMatcher());
95  EXPECT_FALSE(Value.isString());
96
97  Value = VariantValue();
98  EXPECT_FALSE(Value.isUnsigned());
99  EXPECT_FALSE(Value.isString());
100  EXPECT_FALSE(Value.isMatcher());
101  EXPECT_EQ("Nothing", Value.getTypeAsString());
102}
103
104TEST(VariantValueTest, Matcher) {
105  EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher(
106                                                      recordDecl(hasName("X"))))
107                                         .getTypedMatcher<Decl>()));
108  EXPECT_TRUE(matches("int x;",
109                      VariantValue(VariantMatcher::SingleMatcher(varDecl()))
110                          .getTypedMatcher<Decl>()));
111  EXPECT_TRUE(
112      matches("int foo() { return 1 + 1; }",
113              VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
114                  .getTypedMatcher<Decl>()));
115  // Can't get the wrong matcher.
116  EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
117                   .hasTypedMatcher<Stmt>());
118#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST && !defined(_MSC_VER)
119  // Trying to get the wrong matcher fails an assertion in Matcher<T>.  We don't
120  // do this test when building with MSVC because its debug C runtime prints the
121  // assertion failure message as a wide string, which gtest doesn't understand.
122  EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
123                   .getTypedMatcher<Stmt>(),
124               "hasTypedMatcher");
125#endif
126
127  EXPECT_FALSE(matches(
128      "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
129                    .getTypedMatcher<Decl>()));
130  EXPECT_FALSE(
131      matches("int foo() { return 1 + 1; }",
132              VariantValue(VariantMatcher::SingleMatcher(declRefExpr()))
133                  .getTypedMatcher<Stmt>()));
134}
135
136} // end anonymous namespace
137} // end namespace dynamic
138} // end namespace ast_matchers
139} // end namespace clang
140