From 8f69550afc20daa00d49ecb5eda3343fb9fb414d Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Thu, 23 Jan 2014 22:48:38 +0000 Subject: Introduce Registry::getCompletions. This returns a list of valid (and useful) completions for a context (a list of outer matchers), ordered by decreasing relevance then alphabetically. It will be used by the matcher parser to implement completion. Differential Revision: http://llvm-reviews.chandlerc.com/D2210 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@199950 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ASTMatchers/Dynamic/RegistryTest.cpp | 85 ++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) (limited to 'unittests/ASTMatchers') diff --git a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp index c260b467ad..8426649d3e 100644 --- a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -84,6 +84,50 @@ public: EXPECT_EQ("", DummyError.toStringFull()); return Out; } + + typedef std::vector CompVector; + + CompVector getCompletions() { + return Registry::getCompletions( + llvm::ArrayRef >()); + } + + CompVector getCompletions(StringRef MatcherName1, unsigned ArgNo1) { + std::vector > Context; + llvm::Optional Ctor = lookupMatcherCtor(MatcherName1); + if (!Ctor) + return CompVector(); + Context.push_back(std::make_pair(*Ctor, ArgNo1)); + return Registry::getCompletions(Context); + } + + CompVector getCompletions(StringRef MatcherName1, unsigned ArgNo1, + StringRef MatcherName2, unsigned ArgNo2) { + std::vector > Context; + llvm::Optional Ctor = lookupMatcherCtor(MatcherName1); + if (!Ctor) + return CompVector(); + Context.push_back(std::make_pair(*Ctor, ArgNo1)); + Ctor = lookupMatcherCtor(MatcherName2); + if (!Ctor) + return CompVector(); + Context.push_back(std::make_pair(*Ctor, ArgNo2)); + return Registry::getCompletions(Context); + } + + bool hasCompletion(const CompVector &Comps, StringRef TypedText, + StringRef MatcherDecl = StringRef(), unsigned *Index = 0) { + for (CompVector::const_iterator I = Comps.begin(), E = Comps.end(); I != E; + ++I) { + if (I->TypedText == TypedText && + (MatcherDecl.empty() || I->MatcherDecl == MatcherDecl)) { + if (Index) + *Index = I - Comps.begin(); + return true; + } + } + return false; + } }; TEST_F(RegistryTest, CanConstructNoArgs) { @@ -378,6 +422,47 @@ TEST_F(RegistryTest, Errors) { Error->toString()); } +TEST_F(RegistryTest, Completion) { + CompVector Comps = getCompletions(); + EXPECT_TRUE(hasCompletion( + Comps, "hasParent(", "Matcher hasParent(Matcher)")); + EXPECT_TRUE(hasCompletion(Comps, "whileStmt(", + "Matcher whileStmt(Matcher...)")); + + CompVector WhileComps = getCompletions("whileStmt", 0); + + unsigned HasBodyIndex, HasParentIndex, AllOfIndex; + EXPECT_TRUE(hasCompletion(WhileComps, "hasBody(", + "Matcher hasBody(Matcher)", + &HasBodyIndex)); + EXPECT_TRUE(hasCompletion(WhileComps, "hasParent(", + "Matcher hasParent(Matcher)", + &HasParentIndex)); + EXPECT_TRUE(hasCompletion(WhileComps, "allOf(", + "Matcher allOf(Matcher...)", &AllOfIndex)); + EXPECT_GT(HasParentIndex, HasBodyIndex); + EXPECT_GT(AllOfIndex, HasParentIndex); + + EXPECT_FALSE(hasCompletion(WhileComps, "whileStmt(")); + EXPECT_FALSE(hasCompletion(WhileComps, "ifStmt(")); + + CompVector AllOfWhileComps = + getCompletions("allOf", 0, "whileStmt", 0); + ASSERT_EQ(AllOfWhileComps.size(), WhileComps.size()); + EXPECT_TRUE(std::equal(WhileComps.begin(), WhileComps.end(), + AllOfWhileComps.begin())); + + CompVector DeclWhileComps = + getCompletions("decl", 0, "whileStmt", 0); + EXPECT_EQ(0u, DeclWhileComps.size()); + + CompVector NamedDeclComps = getCompletions("namedDecl", 0); + EXPECT_TRUE( + hasCompletion(NamedDeclComps, "isPublic()", "Matcher isPublic()")); + EXPECT_TRUE(hasCompletion(NamedDeclComps, "hasName(\"", + "Matcher hasName(string)")); +} + } // end anonymous namespace } // end namespace dynamic } // end namespace ast_matchers -- cgit v1.2.3