1f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//===- unittest/ASTMatchers/Dynamic/VariantValueTest.cpp - VariantValue unit tests -===//
2f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//
3f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//                     The LLVM Compiler Infrastructure
4f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//
5f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek// This file is distributed under the University of Illinois Open Source
6f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek// License. See LICENSE.TXT for details.
7f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//
8f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek//===-----------------------------------------------------------------------------===//
9f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
10f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "../ASTMatchersTest.h"
11f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "clang/ASTMatchers/Dynamic/VariantValue.h"
12f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek#include "gtest/gtest.h"
13f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
14f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace clang {
15f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace ast_matchers {
16f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace dynamic {
17f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimeknamespace {
18f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
19f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimekusing ast_matchers::internal::DynTypedMatcher;
20f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimekusing ast_matchers::internal::Matcher;
21f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
227a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel BenzaquenTEST(VariantValueTest, Unsigned) {
237a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  const unsigned kUnsigned = 17;
247a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  VariantValue Value = kUnsigned;
257a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen
267a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_TRUE(Value.isUnsigned());
277a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_EQ(kUnsigned, Value.getUnsigned());
287a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen
296bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(Value.hasValue());
307a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isString());
319d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(Value.isMatcher());
327a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen}
337a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen
34f7f295f321fd434e1e542844a71f538a56f2f8fbManuel KlimekTEST(VariantValueTest, String) {
35f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  const ::std::string kString = "string";
36f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  VariantValue Value = kString;
37f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
38f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_TRUE(Value.isString());
39f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_EQ(kString, Value.getString());
4076c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("String", Value.getTypeAsString());
41f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
426bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(Value.hasValue());
437a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isUnsigned());
449d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(Value.isMatcher());
45f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}
46f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
47f7f295f321fd434e1e542844a71f538a56f2f8fbManuel KlimekTEST(VariantValueTest, DynTypedMatcher) {
489d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  VariantValue Value = VariantMatcher::SingleMatcher(stmt());
49f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(Value.hasValue());
517a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isUnsigned());
52f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_FALSE(Value.isString());
53f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
549d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_TRUE(Value.isMatcher());
554e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>());
564e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
5776c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("Matcher<Stmt>", Value.getTypeAsString());
58f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
5976c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  // Can only convert to compatible matchers.
609d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  Value = VariantMatcher::SingleMatcher(recordDecl());
619d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_TRUE(Value.isMatcher());
624e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>());
634e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
6476c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
65f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
669d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  Value = VariantMatcher::SingleMatcher(ignoringImpCasts(expr()));
679d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_TRUE(Value.isMatcher());
684e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Decl>());
694e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<Stmt>());
704e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Expr>());
714e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<IntegerLiteral>());
724e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<GotoStmt>());
7376c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("Matcher<Expr>", Value.getTypeAsString());
74f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}
75f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
76f7f295f321fd434e1e542844a71f538a56f2f8fbManuel KlimekTEST(VariantValueTest, Assignment) {
77f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  VariantValue Value = std::string("A");
78f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_TRUE(Value.isString());
79f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_EQ("A", Value.getString());
806bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(Value.hasValue());
817a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isUnsigned());
829d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(Value.isMatcher());
8376c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("String", Value.getTypeAsString());
84f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
859d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  Value = VariantMatcher::SingleMatcher(recordDecl());
866bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(Value.hasValue());
877a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isUnsigned());
88f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_FALSE(Value.isString());
899d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_TRUE(Value.isMatcher());
904e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_TRUE(Value.getMatcher().hasTypedMatcher<Decl>());
914e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_FALSE(Value.getMatcher().hasTypedMatcher<UnaryOperator>());
9276c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("Matcher<Decl>", Value.getTypeAsString());
93f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
947a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  Value = 17;
957a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_TRUE(Value.isUnsigned());
967a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_EQ(17U, Value.getUnsigned());
976bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(Value.hasValue());
989d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(Value.isMatcher());
997a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isString());
1007a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen
101f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  Value = VariantValue();
1026bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_FALSE(Value.hasValue());
1037a337af9e8bc752a2d3b227e4058ed2baf7a19d1Samuel Benzaquen  EXPECT_FALSE(Value.isUnsigned());
104f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek  EXPECT_FALSE(Value.isString());
1059d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(Value.isMatcher());
10676c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_EQ("Nothing", Value.getTypeAsString());
107f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}
108f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
1096bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen HinesTEST(VariantValueTest, ImplicitBool) {
1106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  VariantValue Value;
1116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  bool IfTrue = false;
1126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Value) {
1136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IfTrue = true;
1146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
1156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_FALSE(IfTrue);
1166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(!Value);
1176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
1186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Value = std::string();
1196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  IfTrue = false;
1206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  if (Value) {
1216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    IfTrue = true;
1226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  }
1236bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_TRUE(IfTrue);
1246bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  EXPECT_FALSE(!Value);
1256bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}
1266bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines
127ef7eb024397a6a9d1455b31bc7b10288a817ac3bSamuel BenzaquenTEST(VariantValueTest, Matcher) {
1289d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_TRUE(matches("class X {};", VariantValue(VariantMatcher::SingleMatcher(
1299d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                                                      recordDecl(hasName("X"))))
1304e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                                         .getMatcher()
13176c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen                                         .getTypedMatcher<Decl>()));
1324e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen  EXPECT_TRUE(
1334e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen      matches("int x;", VariantValue(VariantMatcher::SingleMatcher(varDecl()))
1344e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                            .getMatcher()
1354e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                            .getTypedMatcher<Decl>()));
13676c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  EXPECT_TRUE(
1379d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen      matches("int foo() { return 1 + 1; }",
1389d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen              VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
1394e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                  .getMatcher()
1409d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                  .getTypedMatcher<Decl>()));
14176c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen  // Can't get the wrong matcher.
1429d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
1434e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                   .getMatcher()
1449d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                   .hasTypedMatcher<Stmt>());
145651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
1468711da1b6ba7d2bb94fa579f7d58b4d3e45825acReid Kleckner  // Trying to get the wrong matcher fails an assertion in Matcher<T>.  We don't
1478711da1b6ba7d2bb94fa579f7d58b4d3e45825acReid Kleckner  // do this test when building with MSVC because its debug C runtime prints the
1488711da1b6ba7d2bb94fa579f7d58b4d3e45825acReid Kleckner  // assertion failure message as a wide string, which gtest doesn't understand.
1499d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_DEATH(VariantValue(VariantMatcher::SingleMatcher(varDecl()))
1504e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                   .getMatcher()
1519d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                   .getTypedMatcher<Stmt>(),
152ef7eb024397a6a9d1455b31bc7b10288a817ac3bSamuel Benzaquen               "hasTypedMatcher");
15376c2f92c4b4ab7e02857661a05e53ba4b501d87aSamuel Benzaquen#endif
154f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
1559d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen  EXPECT_FALSE(matches(
1569d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen      "int x;", VariantValue(VariantMatcher::SingleMatcher(functionDecl()))
1574e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                    .getMatcher()
1589d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                    .getTypedMatcher<Decl>()));
159ef7eb024397a6a9d1455b31bc7b10288a817ac3bSamuel Benzaquen  EXPECT_FALSE(
160ef7eb024397a6a9d1455b31bc7b10288a817ac3bSamuel Benzaquen      matches("int foo() { return 1 + 1; }",
1619d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen              VariantValue(VariantMatcher::SingleMatcher(declRefExpr()))
1624e899d9b0b5aa31b0a3704374d245fc345c19fa8Samuel Benzaquen                  .getMatcher()
1639d02807c3ea9782442b98201df68294cd7cd7313Samuel Benzaquen                  .getTypedMatcher<Stmt>()));
164f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek}
165f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek
166f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek} // end anonymous namespace
167f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek} // end namespace dynamic
168f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek} // end namespace ast_matchers
169f7f295f321fd434e1e542844a71f538a56f2f8fbManuel Klimek} // end namespace clang
170