2013-11-08 08:08:23 +08:00
|
|
|
//===---- QueryParserTest.cpp - clang-query test --------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "QueryParser.h"
|
|
|
|
#include "Query.h"
|
|
|
|
#include "QuerySession.h"
|
2014-02-01 09:42:46 +08:00
|
|
|
#include "llvm/LineEditor/LineEditor.h"
|
2014-03-04 18:03:05 +08:00
|
|
|
#include "gtest/gtest.h"
|
2013-11-08 08:08:23 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::query;
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
class QueryParserTest : public ::testing::Test {
|
|
|
|
protected:
|
2014-04-25 23:21:43 +08:00
|
|
|
QueryParserTest() : QS(llvm::ArrayRef<std::unique_ptr<ASTUnit>>()) {}
|
2014-04-23 22:04:52 +08:00
|
|
|
QueryRef parse(StringRef Code) { return QueryParser::parse(Code, QS); }
|
|
|
|
|
2014-04-24 18:58:17 +08:00
|
|
|
QuerySession QS;
|
2014-04-23 22:04:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(QueryParserTest, NoOp) {
|
|
|
|
QueryRef Q = parse("");
|
2013-11-08 08:08:23 +08:00
|
|
|
EXPECT_TRUE(isa<NoOpQuery>(Q));
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("\n");
|
2013-11-08 08:08:23 +08:00
|
|
|
EXPECT_TRUE(isa<NoOpQuery>(Q));
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
TEST_F(QueryParserTest, Invalid) {
|
|
|
|
QueryRef Q = parse("foo");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("unknown command: foo", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
TEST_F(QueryParserTest, Help) {
|
|
|
|
QueryRef Q = parse("help");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<HelpQuery>(Q));
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("help me");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("unexpected extra input: ' me'", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
TEST_F(QueryParserTest, Set) {
|
|
|
|
QueryRef Q = parse("set");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set foo bar");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("unknown variable: 'foo'", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set output");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
2014-02-01 09:42:46 +08:00
|
|
|
EXPECT_EQ("expected 'diag', 'print' or 'dump', got ''",
|
|
|
|
cast<InvalidQuery>(Q)->ErrStr);
|
2013-11-08 08:08:23 +08:00
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set bind-root true foo");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("unexpected extra input: ' foo'", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set output foo");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("expected 'diag', 'print' or 'dump', got 'foo'",
|
|
|
|
cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set output dump");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<SetQuery<OutputKind> >(Q));
|
|
|
|
EXPECT_EQ(&QuerySession::OutKind, cast<SetQuery<OutputKind> >(Q)->Var);
|
|
|
|
EXPECT_EQ(OK_Dump, cast<SetQuery<OutputKind> >(Q)->Value);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set bind-root foo");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("expected 'true' or 'false', got 'foo'",
|
|
|
|
cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("set bind-root true");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<SetQuery<bool> >(Q));
|
|
|
|
EXPECT_EQ(&QuerySession::BindRoot, cast<SetQuery<bool> >(Q)->Var);
|
|
|
|
EXPECT_EQ(true, cast<SetQuery<bool> >(Q)->Value);
|
|
|
|
}
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
TEST_F(QueryParserTest, Match) {
|
|
|
|
QueryRef Q = parse("match decl()");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<MatchQuery>(Q));
|
|
|
|
EXPECT_TRUE(cast<MatchQuery>(Q)->Matcher.canConvertTo<Decl>());
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Q = parse("m stmt()");
|
2013-11-08 08:08:23 +08:00
|
|
|
ASSERT_TRUE(isa<MatchQuery>(Q));
|
|
|
|
EXPECT_TRUE(cast<MatchQuery>(Q)->Matcher.canConvertTo<Stmt>());
|
|
|
|
}
|
2014-02-01 09:42:46 +08:00
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
TEST_F(QueryParserTest, LetUnlet) {
|
|
|
|
QueryRef Q = parse("let foo decl()");
|
|
|
|
ASSERT_TRUE(isa<LetQuery>(Q));
|
|
|
|
EXPECT_EQ("foo", cast<LetQuery>(Q)->Name);
|
|
|
|
EXPECT_TRUE(cast<LetQuery>(Q)->Value.isMatcher());
|
|
|
|
EXPECT_TRUE(cast<LetQuery>(Q)->Value.getMatcher().hasTypedMatcher<Decl>());
|
|
|
|
|
|
|
|
Q = parse("let bar \"str\"");
|
|
|
|
ASSERT_TRUE(isa<LetQuery>(Q));
|
|
|
|
EXPECT_EQ("bar", cast<LetQuery>(Q)->Name);
|
|
|
|
EXPECT_TRUE(cast<LetQuery>(Q)->Value.isString());
|
|
|
|
EXPECT_EQ("str", cast<LetQuery>(Q)->Value.getString());
|
|
|
|
|
|
|
|
Q = parse("let");
|
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
|
|
|
Q = parse("unlet x");
|
|
|
|
ASSERT_TRUE(isa<LetQuery>(Q));
|
|
|
|
EXPECT_EQ("x", cast<LetQuery>(Q)->Name);
|
|
|
|
EXPECT_FALSE(cast<LetQuery>(Q)->Value.hasValue());
|
|
|
|
|
|
|
|
Q = parse("unlet");
|
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("expected variable name", cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
|
|
|
|
Q = parse("unlet x bad_data");
|
|
|
|
ASSERT_TRUE(isa<InvalidQuery>(Q));
|
|
|
|
EXPECT_EQ("unexpected extra input: ' bad_data'",
|
|
|
|
cast<InvalidQuery>(Q)->ErrStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(QueryParserTest, Complete) {
|
2014-02-01 09:42:46 +08:00
|
|
|
std::vector<llvm::LineEditor::Completion> Comps =
|
2014-04-23 22:04:52 +08:00
|
|
|
QueryParser::complete("", 0, QS);
|
|
|
|
ASSERT_EQ(5u, Comps.size());
|
2014-02-01 09:42:46 +08:00
|
|
|
EXPECT_EQ("help ", Comps[0].TypedText);
|
|
|
|
EXPECT_EQ("help", Comps[0].DisplayText);
|
2014-04-23 22:04:52 +08:00
|
|
|
EXPECT_EQ("let ", Comps[1].TypedText);
|
|
|
|
EXPECT_EQ("let", Comps[1].DisplayText);
|
|
|
|
EXPECT_EQ("match ", Comps[2].TypedText);
|
|
|
|
EXPECT_EQ("match", Comps[2].DisplayText);
|
|
|
|
EXPECT_EQ("set ", Comps[3].TypedText);
|
|
|
|
EXPECT_EQ("set", Comps[3].DisplayText);
|
|
|
|
EXPECT_EQ("unlet ", Comps[4].TypedText);
|
|
|
|
EXPECT_EQ("unlet", Comps[4].DisplayText);
|
|
|
|
|
|
|
|
Comps = QueryParser::complete("set o", 5, QS);
|
2014-02-01 09:42:46 +08:00
|
|
|
ASSERT_EQ(1u, Comps.size());
|
|
|
|
EXPECT_EQ("utput ", Comps[0].TypedText);
|
|
|
|
EXPECT_EQ("output", Comps[0].DisplayText);
|
|
|
|
|
2014-04-23 22:04:52 +08:00
|
|
|
Comps = QueryParser::complete("match while", 11, QS);
|
2014-02-01 09:42:46 +08:00
|
|
|
ASSERT_EQ(1u, Comps.size());
|
|
|
|
EXPECT_EQ("Stmt(", Comps[0].TypedText);
|
|
|
|
EXPECT_EQ("Matcher<Stmt> whileStmt(Matcher<WhileStmt>...)",
|
|
|
|
Comps[0].DisplayText);
|
|
|
|
}
|