93c5aae6adf322adcb2bf50b7a3a8e07b4598408
[platform/upstream/libzypp.git] / tests / zypp / PoolQueryCC_test.cc
1 #include "TestSetup.h"
2 #include <zypp/base/String.h>
3 #include <zypp/base/LogTools.h>
4
5 #include "zypp/PoolQuery.h"
6 #include "zypp/PoolQueryUtil.tcc"
7
8 #define BOOST_TEST_MODULE PoolQuery_CC
9
10 using boost::unit_test::test_case;
11 using std::cin;
12 using std::cout;
13 using std::cerr;
14 using std::endl;
15 using namespace zypp;
16
17 static TestSetup test;
18
19 /////////////////////////////////////////////////////////////////////////////
20 template <class TCont>
21 std::ostream & nlist( std::ostream & str, const TCont & set_r )
22 {
23   str << "[" << set_r.size() << "]: ";
24   for_( it, set_r.begin(), set_r.end() )
25     str << " \"" << PoolItem(*it).satSolvable().name() << "\"";
26   return str << endl;
27 }
28
29 BOOST_AUTO_TEST_CASE(init)
30 {
31   test.loadTargetHelix( TESTS_SRC_DIR "/zypp/data/PoolQueryCC/rxnames.xml" );
32   nlist( cout << "repo ", ResPool::instance() );
33 }
34
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);
44
45 unsigned qtest( const std::string & pattern_r, Match::Mode mode_r, bool verbose_r = false )
46 {
47   static const bool noMatchInvalidRegexException = false;
48
49   typedef std::set<sat::Solvable> Result;
50   PoolQuery q;
51   q.addAttribute(sat::SolvAttr::name);
52   switch ( mode_r )
53   {
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;
59     default:
60       throw( "unhandled match mode" );
61       break;
62   }
63   q.addString( pattern_r );
64   Result o;
65   try {
66     o = Result( q.begin(), q.end() );   // original query
67   }
68   catch ( const zypp::MatchInvalidRegexException & excpt )
69   {
70     cout << "Caught: " << excpt << endl;
71     return qtestRXFAIL;
72   }
73
74   q.addString( "more" );
75   try {
76     Result r( q.begin(), q.end() );     // compiles into RX (o|more)
77
78     BOOST_CHECK( o == r );
79     if ( o != r || verbose_r )
80     {
81       cout << '"' << pattern_r << "\"  " << mode_r << endl;
82       nlist( cout << "    o", o );
83       nlist( cout << "    r", r );
84       if ( ! verbose_r )
85         return qtestSIZEMISS;
86     }
87   }
88   catch ( const zypp::MatchInvalidRegexException & excpt )
89   {
90     BOOST_CHECK( noMatchInvalidRegexException );
91     cout << "Caught: " << excpt << endl;
92     return qtestRXFAILCOMB;
93   }
94
95   return o.size();
96 }
97
98 inline unsigned qtest( const std::string & pattern_r, bool verbose_r = false )
99 { return qtest( pattern_r, Match::SUBSTRING, verbose_r ); }
100
101 /////////////////////////////////////////////////////////////////////////////
102 BOOST_AUTO_TEST_CASE(pool_query_init)
103 {
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 );
112
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 );
119 }
120
121 /////////////////////////////////////////////////////////////////////////////