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