fixup Fix to build with libxml 2.12.x (fixes #505)
[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 template <class TCont>
18 std::ostream & nlist( std::ostream & str, const TCont & set_r )
19 {
20   str << "[" << set_r.size() << "]: ";
21   for ( const auto & solv : set_r )
22     str << " \"" << solv.name() << "\"";
23   return str << endl;
24 }
25
26 /////////////////////////////////////////////////////////////////////////////
27
28 static TestSetup test( TestSetup::initLater );
29 struct TestInit {
30   TestInit() {
31     test = TestSetup( Arch_x86_64 );
32
33     test.loadTargetHelix( TESTS_SRC_DIR "/zypp/data/PoolQueryCC/rxnames.xml" );
34     nlist( cout << "repo ", ResPool::instance() );
35   }
36   ~TestInit() { test.reset(); }
37 };
38 BOOST_GLOBAL_FIXTURE( TestInit );
39
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);
49
50 unsigned qtest( const std::string & pattern_r, Match::Mode mode_r, bool verbose_r = false )
51 {
52   static constexpr const bool noMatchInvalidRegexException = false;
53
54   typedef std::set<sat::Solvable> Result;
55   PoolQuery q;
56   q.addAttribute(sat::SolvAttr::name);
57   switch ( mode_r )
58   {
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;
64     default:
65       throw( "unhandled match mode" );
66       break;
67   }
68   q.addString( pattern_r );
69   Result o;
70   try {
71     o = Result( q.begin(), q.end() );   // original query
72   }
73   catch ( const zypp::MatchInvalidRegexException & excpt )
74   {
75     cout << "Caught: " << excpt << endl;
76     return qtestRXFAIL;
77   }
78
79   q.addString( "more" );
80   try {
81     Result r( q.begin(), q.end() );     // compiles into RX (o|more)
82
83     BOOST_CHECK( o == r );
84     if ( o != r || verbose_r )
85     {
86       cout << '"' << pattern_r << "\"  " << mode_r << endl;
87       nlist( cout << "    o", o );
88       nlist( cout << "    r", r );
89       if ( ! verbose_r )
90         return qtestSIZEMISS;
91     }
92   }
93   catch ( const zypp::MatchInvalidRegexException & excpt )
94   {
95     BOOST_CHECK( noMatchInvalidRegexException );
96     cout << "Caught: " << excpt << endl;
97     return qtestRXFAILCOMB;
98   }
99
100   return o.size();
101 }
102
103 inline unsigned qtest( const std::string & pattern_r, bool verbose_r = false )
104 { return qtest( pattern_r, Match::SUBSTRING, verbose_r ); }
105
106 /////////////////////////////////////////////////////////////////////////////
107 BOOST_AUTO_TEST_CASE(pool_query_init)
108 {
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 );
117
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 );
124 }
125
126 /////////////////////////////////////////////////////////////////////////////