2 #include <zypp/base/String.h>
3 #include <zypp/base/LogTools.h>
5 #include <zypp/PoolQuery.h>
6 #include <zypp/PoolQueryUtil.tcc>
8 #define BOOST_TEST_MODULE PoolQuery_CC
10 using boost::unit_test::test_case;
17 template <class TCont>
18 std::ostream & nlist( std::ostream & str, const TCont & set_r )
20 str << "[" << set_r.size() << "]: ";
21 for ( const auto & solv : set_r )
22 str << " \"" << solv.name() << "\"";
26 /////////////////////////////////////////////////////////////////////////////
28 static TestSetup test( TestSetup::initLater );
31 test = TestSetup( Arch_x86_64 );
33 test.loadTargetHelix( TESTS_SRC_DIR "/zypp/data/PoolQueryCC/rxnames.xml" );
34 nlist( cout << "repo ", ResPool::instance() );
36 ~TestInit() { test.reset(); }
38 BOOST_GLOBAL_FIXTURE( TestInit );
40 /////////////////////////////////////////////////////////////////////////////
41 // Basic issue: Multiple match strings are compiled into a singe regex. The
42 // semantic of the individual match strings must be preserved. I.e. a literal
43 // "." must become "\.". Globbing patterns must match the whole string, so they
44 // need to be anchored within the regex. Etc.
45 /////////////////////////////////////////////////////////////////////////////
46 static const unsigned qtestSIZEMISS = unsigned(-1);
47 static const unsigned qtestRXFAIL = unsigned(-2);
48 static const unsigned qtestRXFAILCOMB = unsigned(-3);
50 unsigned qtest( const std::string & pattern_r, Match::Mode mode_r, bool verbose_r = false )
52 static constexpr const bool noMatchInvalidRegexException = false;
54 typedef std::set<sat::Solvable> Result;
56 q.addAttribute(sat::SolvAttr::name);
59 case Match::STRING: q.setMatchExact(); break;
60 case Match::SUBSTRING: q.setMatchSubstring(); break;
61 case Match::OTHER: q.setMatchWord(); break; // OTHER missused for matchWord()
62 case Match::GLOB: q.setMatchGlob(); break;
63 case Match::REGEX: q.setMatchRegex(); break;
65 throw( "unhandled match mode" );
68 q.addString( pattern_r );
71 o = Result( q.begin(), q.end() ); // original query
73 catch ( const zypp::MatchInvalidRegexException & excpt )
75 cout << "Caught: " << excpt << endl;
79 q.addString( "more" );
81 Result r( q.begin(), q.end() ); // compiles into RX (o|more)
83 BOOST_CHECK( o == r );
84 if ( o != r || verbose_r )
86 cout << '"' << pattern_r << "\" " << mode_r << endl;
87 nlist( cout << " o", o );
88 nlist( cout << " r", r );
93 catch ( const zypp::MatchInvalidRegexException & excpt )
95 BOOST_CHECK( noMatchInvalidRegexException );
96 cout << "Caught: " << excpt << endl;
97 return qtestRXFAILCOMB;
103 inline unsigned qtest( const std::string & pattern_r, bool verbose_r = false )
104 { return qtest( pattern_r, Match::SUBSTRING, verbose_r ); }
106 /////////////////////////////////////////////////////////////////////////////
107 BOOST_AUTO_TEST_CASE(pool_query_init)
109 // NOTE: qtest( , Match::OTHER ) is missused for matchWord()
110 BOOST_CHECK_EQUAL( qtest( "?", Match::SUBSTRING ), 1 );
111 BOOST_CHECK_EQUAL( qtest( "?", Match::STRING ), 1 );
112 BOOST_CHECK_EQUAL( qtest( "?", Match::OTHER ), 0 ); // not word boundary
113 BOOST_CHECK_EQUAL( qtest( "?", Match::GLOB ), 15 );
114 BOOST_CHECK_EQUAL( qtest( "\\?", Match::GLOB ), 1 );
115 BOOST_CHECK_EQUAL( qtest( "?", Match::REGEX ), qtestRXFAIL );
116 BOOST_CHECK_EQUAL( qtest( "\\?", Match::REGEX ), 1 );
118 BOOST_CHECK_EQUAL( qtest( "A", Match::SUBSTRING ), 4 );
119 BOOST_CHECK_EQUAL( qtest( "A", Match::OTHER ), 2 );
120 BOOST_CHECK_EQUAL( qtest( "A*", Match::OTHER ), 0 );
121 BOOST_CHECK_EQUAL( qtest( "*A", Match::OTHER ), 0 );
122 BOOST_CHECK_EQUAL( qtest( "A*", Match::GLOB ), 2 );
123 BOOST_CHECK_EQUAL( qtest( "*A", Match::GLOB ), 1 );
126 /////////////////////////////////////////////////////////////////////////////