Imported Upstream version 15.0.0
[platform/upstream/libzypp.git] / tests / parser / inifile / iniparser_test.cc
1 #include <stdio.h>
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <list>
6 #include <boost/test/auto_unit_test.hpp>
7
8 #include "zypp/base/Logger.h"
9 #include "zypp/base/InputStream.h"
10 #include "zypp/parser/IniParser.h"
11 #include "zypp/Url.h"
12 #include "zypp/PathInfo.h"
13
14 using std::cout;
15 using std::endl;
16 using std::string;
17 using namespace zypp;
18 using namespace zypp::parser;
19 using namespace boost::unit_test;
20
21 #define DATADIR (Pathname(TESTS_SRC_DIR) +  "/parser/inifile/data")
22
23 class IniTest : public IniParser
24 {
25   virtual void consume( const std::string &section )
26   {
27     MIL << section << endl;
28   }
29
30   virtual void consume( const std::string &section, const std::string &key, const std::string &value )
31   {
32     MIL << "'" << section << "'" << " | " << "'" << key << "'" << " | " << "'" << value << "'" << endl;
33     if (section == "base" && key == "gpgcheck")
34       BOOST_CHECK_EQUAL(value, "1");
35   }
36 };
37
38
39 class WithSpacesTest : public IniParser
40 {
41   virtual void consume( const std::string &section )
42   {
43     MIL << section << endl;
44     BOOST_CHECK(section == "base" || section == "equal");
45   }
46
47   virtual void consume( const std::string &section, const std::string &key, const std::string &value )
48   {
49     MIL << "'" << section << "'" << " | " << "'" << key << "'" << " | " << "'" << value << "'" << endl;
50     if ( section == "base")
51     {
52       if ( key == "name" )
53         BOOST_CHECK_EQUAL( value, "foo" );
54     }
55     else if ( section == "equal" )
56     {
57       if ( key == "name1" )
58         BOOST_CHECK_EQUAL( value, "=foo" );
59       else if ( key == "name2" )
60         BOOST_CHECK_EQUAL( value, "f=oo" );
61       else if ( key == "name3" )
62         BOOST_CHECK_EQUAL( value, "foo=" );
63       else
64       {
65         cout << "'" << section << "'" << " | " << "'" << key << "'" << " | " << "'" << value << "'" << endl;
66         BOOST_CHECK_MESSAGE( false, "Unhandled key" );
67       }
68     }
69   }
70 };
71
72 BOOST_AUTO_TEST_CASE(ini_read)
73 {
74   InputStream is((DATADIR+"/1.ini"));
75   IniTest parser;
76   parser.parse(is);
77 }
78
79 BOOST_AUTO_TEST_CASE(ini_spaces_test)
80 {
81   InputStream is((DATADIR+"/2.ini"));
82   WithSpacesTest parser;
83   parser.parse(is);
84 }