// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. // Unit tests for query_visitor. #include #include LOG_SETUP("query_visitor_test"); #include #include #include #include #include #include using namespace search::query; namespace { class Test : public vespalib::TestApp { void requireThatAllNodesCanBeVisited(); template void checkVisit(T *node); public: int Main(); }; int Test::Main() { TEST_INIT("query_visitor_test"); TEST_DO(requireThatAllNodesCanBeVisited()); TEST_DONE(); } class MyVisitor : public QueryVisitor { public: template bool &isVisited() { static bool b; return b; } virtual void visit(And &) { isVisited() = true; } virtual void visit(AndNot &) { isVisited() = true; } virtual void visit(Equiv &) { isVisited() = true; } virtual void visit(NumberTerm &) { isVisited() = true; } virtual void visit(LocationTerm &) { isVisited() = true; } virtual void visit(Near &) { isVisited() = true; } virtual void visit(ONear &) { isVisited() = true; } virtual void visit(Or &) { isVisited() = true; } virtual void visit(Phrase &) { isVisited() = true; } virtual void visit(PrefixTerm &) { isVisited() = true; } virtual void visit(RangeTerm &) { isVisited() = true; } virtual void visit(Rank &) { isVisited() = true; } virtual void visit(StringTerm &) { isVisited() = true; } virtual void visit(SubstringTerm &) { isVisited() = true; } virtual void visit(SuffixTerm &) { isVisited() = true; } virtual void visit(WeakAnd &) { isVisited() = true; } virtual void visit(WeightedSetTerm &) { isVisited() = true; } virtual void visit(DotProduct &) { isVisited() = true; } virtual void visit(WandTerm &) { isVisited() = true; } virtual void visit(PredicateQuery &) { isVisited() = true; } virtual void visit(RegExpTerm &) { isVisited() = true; } }; template void Test::checkVisit(T *node) { Node::UP query(node); MyVisitor visitor; visitor.isVisited() = false; query->accept(visitor); ASSERT_TRUE(visitor.isVisited()); } void Test::requireThatAllNodesCanBeVisited() { checkVisit(new SimpleAnd); checkVisit(new SimpleAndNot); checkVisit(new SimpleNear(0)); checkVisit(new SimpleONear(0)); checkVisit(new SimpleOr); checkVisit(new SimplePhrase("field", 0, Weight(42))); checkVisit( new SimpleWeightedSetTerm("field", 0, Weight(42))); checkVisit(new SimpleDotProduct("field", 0, Weight(42))); checkVisit( new SimpleWandTerm("field", 0, Weight(42), 57, 67, 77.7)); checkVisit(new SimpleRank); checkVisit( new SimpleNumberTerm("0.42", "field", 0, Weight(0))); const Location location(Point(10, 10), 20, 0); checkVisit( new SimpleLocationTerm(location, "field", 0, Weight(0))); checkVisit(new SimplePrefixTerm("t", "field", 0, Weight(0))); checkVisit( new SimpleRangeTerm(Range(0, 1), "field", 0, Weight(0))); checkVisit(new SimpleStringTerm("t", "field", 0, Weight(0))); checkVisit( new SimpleSubstringTerm("t", "field", 0, Weight(0))); checkVisit(new SimpleSuffixTerm("t", "field", 0, Weight(0))); checkVisit( new SimplePredicateQuery(PredicateQueryTerm::UP(), "field", 0, Weight(0))); checkVisit(new SimpleRegExpTerm("t", "field", 0, Weight(0))); } } // namespace TEST_APPHOOK(Test);