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 static TestSetup test;
19 /////////////////////////////////////////////////////////////////////////////
20 template <class TCont>
21 std::ostream & nlist( std::ostream & str, const TCont & set_r )
23 str << "[" << set_r.size() << "]: ";
24 for_( it, set_r.begin(), set_r.end() )
25 str << " \"" << PoolItem(*it).satSolvable().name() << "\"";
29 BOOST_AUTO_TEST_CASE(init)
31 test.loadTargetHelix( TESTS_SRC_DIR "/zypp/data/PoolQueryCC/rxnames.xml" );
32 nlist( cout << "repo ", ResPool::instance() );
35 /////////////////////////////////////////////////////////////////////////////
36 // Basic issue: Multiple match strings are compiled into a singe regex. The
37 // semantic of the individual match strings must be preserved. I.e. a literal
38 // "." must become "\.". Globbing patterns must match the whole string, so they
39 // need to be anchored within the regex. Etc.
40 /////////////////////////////////////////////////////////////////////////////
41 static const unsigned qtestSIZEMISS = unsigned(-1);
42 static const unsigned qtestRXFAIL = unsigned(-2);
43 static const unsigned qtestRXFAILCOMB = unsigned(-3);
45 unsigned qtest( const std::string & pattern_r, Match::Mode mode_r, bool verbose_r = false )
47 static const bool noMatchInvalidRegexException = false;
49 typedef std::set<sat::Solvable> Result;
51 q.addAttribute(sat::SolvAttr::name);
54 case Match::STRING: q.setMatchExact(); break;
55 case Match::SUBSTRING: q.setMatchSubstring(); break;
56 case Match::OTHER: q.setMatchWord(); break; // OTHER missused for matchWord()
57 case Match::GLOB: q.setMatchGlob(); break;
58 case Match::REGEX: q.setMatchRegex(); break;
60 throw( "unhandled match mode" );
63 q.addString( pattern_r );
66 o = Result( q.begin(), q.end() ); // original query
68 catch ( const zypp::MatchInvalidRegexException & excpt )
70 cout << "Caught: " << excpt << endl;
74 q.addString( "more" );
76 Result r( q.begin(), q.end() ); // compiles into RX (o|more)
78 BOOST_CHECK( o == r );
79 if ( o != r || verbose_r )
81 cout << '"' << pattern_r << "\" " << mode_r << endl;
82 nlist( cout << " o", o );
83 nlist( cout << " r", r );
88 catch ( const zypp::MatchInvalidRegexException & excpt )
90 BOOST_CHECK( noMatchInvalidRegexException );
91 cout << "Caught: " << excpt << endl;
92 return qtestRXFAILCOMB;
98 inline unsigned qtest( const std::string & pattern_r, bool verbose_r = false )
99 { return qtest( pattern_r, Match::SUBSTRING, verbose_r ); }
101 /////////////////////////////////////////////////////////////////////////////
102 BOOST_AUTO_TEST_CASE(pool_query_init)
104 // NOTE: qtest( , Match::OTHER ) is missused for matchWord()
105 BOOST_CHECK_EQUAL( qtest( "?", Match::SUBSTRING ), 1 );
106 BOOST_CHECK_EQUAL( qtest( "?", Match::STRING ), 1 );
107 BOOST_CHECK_EQUAL( qtest( "?", Match::OTHER ), 0 ); // not word boundary
108 BOOST_CHECK_EQUAL( qtest( "?", Match::GLOB ), 15 );
109 BOOST_CHECK_EQUAL( qtest( "\\?", Match::GLOB ), 1 );
110 BOOST_CHECK_EQUAL( qtest( "?", Match::REGEX ), qtestRXFAIL );
111 BOOST_CHECK_EQUAL( qtest( "\\?", Match::REGEX ), 1 );
113 BOOST_CHECK_EQUAL( qtest( "A", Match::SUBSTRING ), 4 );
114 BOOST_CHECK_EQUAL( qtest( "A", Match::OTHER ), 2 );
115 BOOST_CHECK_EQUAL( qtest( "A*", Match::OTHER ), 0 );
116 BOOST_CHECK_EQUAL( qtest( "*A", Match::OTHER ), 0 );
117 BOOST_CHECK_EQUAL( qtest( "A*", Match::GLOB ), 2 );
118 BOOST_CHECK_EQUAL( qtest( "*A", Match::GLOB ), 1 );
121 /////////////////////////////////////////////////////////////////////////////