blob: 625f70bd4655ac8e15766c0de1b1d80aa3d53ef8 [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===- 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
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000022TEST(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());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000030 EXPECT_FALSE(Value.isMatchers());
31 EXPECT_FALSE(Value.hasTypedMatcher<Decl>());
32 EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>());
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000033}
34
Manuel Klimekf7f295f2013-05-14 09:13:00 +000035TEST(VariantValueTest, String) {
36 const ::std::string kString = "string";
37 VariantValue Value = kString;
38
39 EXPECT_TRUE(Value.isString());
40 EXPECT_EQ(kString, Value.getString());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000041 EXPECT_EQ("String", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000042
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000043 EXPECT_FALSE(Value.isUnsigned());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000044 EXPECT_FALSE(Value.isMatchers());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000045}
46
47TEST(VariantValueTest, DynTypedMatcher) {
48 VariantValue Value = stmt();
49
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000050 EXPECT_FALSE(Value.isUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000051 EXPECT_FALSE(Value.isString());
52
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000053 EXPECT_TRUE(Value.isMatchers());
54 EXPECT_FALSE(Value.hasTypedMatcher<Decl>());
55 EXPECT_TRUE(Value.hasTypedMatcher<UnaryOperator>());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000056 EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000057
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000058 // Can only convert to compatible matchers.
Manuel Klimekf7f295f2013-05-14 09:13:00 +000059 Value = recordDecl();
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000060 EXPECT_TRUE(Value.isMatchers());
61 EXPECT_TRUE(Value.hasTypedMatcher<Decl>());
62 EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000063 EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000064
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000065 Value = ignoringImpCasts(expr());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000066 EXPECT_TRUE(Value.isMatchers());
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>());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000072 EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000073}
74
75TEST(VariantValueTest, Assignment) {
76 VariantValue Value = std::string("A");
77 EXPECT_TRUE(Value.isString());
78 EXPECT_EQ("A", Value.getString());
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000079 EXPECT_FALSE(Value.isUnsigned());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000080 EXPECT_FALSE(Value.isMatchers());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000081 EXPECT_EQ("String", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000082
83 Value = recordDecl();
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000084 EXPECT_FALSE(Value.isUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000085 EXPECT_FALSE(Value.isString());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000086 EXPECT_TRUE(Value.isMatchers());
87 EXPECT_TRUE(Value.hasTypedMatcher<Decl>());
88 EXPECT_FALSE(Value.hasTypedMatcher<UnaryOperator>());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +000089 EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000090
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000091 Value = 17;
92 EXPECT_TRUE(Value.isUnsigned());
93 EXPECT_EQ(17U, Value.getUnsigned());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +000094 EXPECT_FALSE(Value.isMatchers());
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000095 EXPECT_FALSE(Value.isString());
96
Manuel Klimekf7f295f2013-05-14 09:13:00 +000097 Value = VariantValue();
Samuel Benzaquen7a337af2013-06-04 15:46:22 +000098 EXPECT_FALSE(Value.isUnsigned());
Manuel Klimekf7f295f2013-05-14 09:13:00 +000099 EXPECT_FALSE(Value.isString());
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000100 EXPECT_FALSE(Value.isMatchers());
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000101 EXPECT_EQ("Nothing", Value.getTypeAsString());
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000102}
103
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000104TEST(VariantValueTest, Matcher) {
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000105 EXPECT_TRUE(matches("class X {};", VariantValue(recordDecl(hasName("X")))
106 .getTypedMatcher<Decl>()));
107 EXPECT_TRUE(
108 matches("int x;", VariantValue(varDecl()).getTypedMatcher<Decl>()));
109 EXPECT_TRUE(matches("int foo() { return 1 + 1; }",
110 VariantValue(functionDecl()).getTypedMatcher<Decl>()));
111 // Can't get the wrong matcher.
112 EXPECT_FALSE(VariantValue(varDecl()).hasTypedMatcher<Stmt>());
Reid Kleckner8711da12013-06-21 12:58:12 +0000113#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST && !defined(_MSC_VER)
114 // Trying to get the wrong matcher fails an assertion in Matcher<T>. We don't
115 // do this test when building with MSVC because its debug C runtime prints the
116 // assertion failure message as a wide string, which gtest doesn't understand.
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000117 EXPECT_DEATH(VariantValue(varDecl()).getTypedMatcher<Stmt>(),
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000118 "hasTypedMatcher");
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000119#endif
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000120
121 EXPECT_FALSE(
Samuel Benzaquen76c2f922013-06-20 14:28:32 +0000122 matches("int x;", VariantValue(functionDecl()).getTypedMatcher<Decl>()));
Samuel Benzaquenef7eb022013-06-21 15:51:31 +0000123 EXPECT_FALSE(
124 matches("int foo() { return 1 + 1; }",
125
126 VariantValue(declRefExpr()).getTypedMatcher<Stmt>()));
Manuel Klimekf7f295f2013-05-14 09:13:00 +0000127}
128
129} // end anonymous namespace
130} // end namespace dynamic
131} // end namespace ast_matchers
132} // end namespace clang