blob: 64af120193bf482f5f9685c546ed6c8a371bbeed [file] [log] [blame]
Manuel Klimekf7f295f2013-05-14 09:13:00 +00001//===- unittest/ASTMatchers/Dynamic/RegistryTest.cpp - Registry 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 <vector>
11
12#include "../ASTMatchersTest.h"
13#include "clang/ASTMatchers/Dynamic/Registry.h"
14#include "gtest/gtest.h"
15
16namespace clang {
17namespace ast_matchers {
18namespace dynamic {
19namespace {
20
21using ast_matchers::internal::Matcher;
22
23DynTypedMatcher *constructMatcher(StringRef MatcherName, Diagnostics *Error) {
24 const std::vector<ParserValue> Args;
25 return Registry::constructMatcher(MatcherName, SourceRange(), Args, Error);
26}
27
28DynTypedMatcher *constructMatcher(StringRef MatcherName,
29 const VariantValue &Arg1,
30 Diagnostics *Error) {
31 std::vector<ParserValue> Args(1);
32 Args[0].Value = Arg1;
33 return Registry::constructMatcher(MatcherName, SourceRange(), Args, Error);
34}
35
36DynTypedMatcher *constructMatcher(StringRef MatcherName,
37 const VariantValue &Arg1,
38 const VariantValue &Arg2,
39 Diagnostics *Error) {
40 std::vector<ParserValue> Args(2);
41 Args[0].Value = Arg1;
42 Args[1].Value = Arg2;
43 return Registry::constructMatcher(MatcherName, SourceRange(), Args, Error);
44}
45
46TEST(RegistryTest, CanConstructNoArgs) {
47 OwningPtr<DynTypedMatcher> IsArrowValue(constructMatcher("isArrow", NULL));
48 OwningPtr<DynTypedMatcher> BoolValue(constructMatcher("boolLiteral", NULL));
49
50 const std::string ClassSnippet = "struct Foo { int x; };\n"
51 "Foo *foo = new Foo;\n"
52 "int i = foo->x;\n";
53 const std::string BoolSnippet = "bool Foo = true;\n";
54
55 EXPECT_TRUE(matchesDynamic(ClassSnippet, *IsArrowValue));
56 EXPECT_TRUE(matchesDynamic(BoolSnippet, *BoolValue));
57 EXPECT_FALSE(matchesDynamic(ClassSnippet, *BoolValue));
58 EXPECT_FALSE(matchesDynamic(BoolSnippet, *IsArrowValue));
59}
60
61TEST(RegistryTest, ConstructWithSimpleArgs) {
62 OwningPtr<DynTypedMatcher> Value(
63 constructMatcher("hasName", std::string("X"), NULL));
64 EXPECT_TRUE(matchesDynamic("class X {};", *Value));
65 EXPECT_FALSE(matchesDynamic("int x;", *Value));
66}
67
68TEST(RegistryTest, ConstructWithMatcherArgs) {
69 OwningPtr<DynTypedMatcher> HasInitializerSimple(
70 constructMatcher("hasInitializer", stmt(), NULL));
71 OwningPtr<DynTypedMatcher> HasInitializerComplex(
72 constructMatcher("hasInitializer", callExpr(), NULL));
73
74 std::string code = "int i;";
75 EXPECT_FALSE(matchesDynamic(code, *HasInitializerSimple));
76 EXPECT_FALSE(matchesDynamic(code, *HasInitializerComplex));
77
78 code = "int i = 1;";
79 EXPECT_TRUE(matchesDynamic(code, *HasInitializerSimple));
80 EXPECT_FALSE(matchesDynamic(code, *HasInitializerComplex));
81
82 code = "int y(); int i = y();";
83 EXPECT_TRUE(matchesDynamic(code, *HasInitializerSimple));
84 EXPECT_TRUE(matchesDynamic(code, *HasInitializerComplex));
85}
86
87TEST(RegistryTest, Errors) {
88 // Incorrect argument count.
89 OwningPtr<Diagnostics> Error(new Diagnostics());
90 EXPECT_TRUE(NULL == constructMatcher("hasInitializer", Error.get()));
91 EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)",
92 Error->ToString());
93 Error.reset(new Diagnostics());
94 EXPECT_TRUE(NULL == constructMatcher("isArrow", std::string(), Error.get()));
95 EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)",
96 Error->ToString());
97
98 // Bad argument type
99 Error.reset(new Diagnostics());
100 EXPECT_TRUE(NULL == constructMatcher("ofClass", std::string(), Error.get()));
101 EXPECT_EQ("Incorrect type on function ofClass for arg 1.", Error->ToString());
102 Error.reset(new Diagnostics());
103 EXPECT_TRUE(NULL == constructMatcher("recordDecl", recordDecl(),
104 ::std::string(), Error.get()));
105 EXPECT_EQ("Incorrect type on function recordDecl for arg 2.",
106 Error->ToString());
107}
108
109} // end anonymous namespace
110} // end namespace dynamic
111} // end namespace ast_matchers
112} // end namespace clang