source: trunk/debathena/third/schroot/test/sbuild-keyfile.cc @ 24167

Revision 24167, 7.3 KB checked in by broder, 15 years ago (diff)
Import schroot upstream into subversion.
Line 
1/* Copyright © 2006-2007  Roger Leigh <rleigh@debian.org>
2 *
3 * schroot is free software: you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * schroot is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 *********************************************************************/
18
19#include <sbuild/sbuild-keyfile.h>
20
21#include <fstream>
22#include <sstream>
23#include <vector>
24
25#include <cppunit/extensions/HelperMacros.h>
26
27using namespace CppUnit;
28
29class test_keyfile : public TestFixture
30{
31  CPPUNIT_TEST_SUITE(test_keyfile);
32  CPPUNIT_TEST(test_construction_file);
33  CPPUNIT_TEST(test_construction_stream);
34  CPPUNIT_TEST_EXCEPTION(test_construction_fail, sbuild::keyfile::error);
35  CPPUNIT_TEST(test_get_groups);
36  CPPUNIT_TEST(test_get_keys);
37  CPPUNIT_TEST(test_get_value);
38  CPPUNIT_TEST(test_get_value_fail);
39  CPPUNIT_TEST(test_get_list_value);
40  CPPUNIT_TEST(test_get_list_value_fail);
41  CPPUNIT_TEST(test_get_line);
42  CPPUNIT_TEST(test_set_value);
43  CPPUNIT_TEST(test_set_list_value);
44  CPPUNIT_TEST(test_remove_group);
45  CPPUNIT_TEST(test_remove_key);
46  CPPUNIT_TEST(test_output);
47  CPPUNIT_TEST_SUITE_END();
48
49protected:
50  sbuild::keyfile *kf;
51
52public:
53  test_keyfile():
54    TestFixture(),
55    kf()
56  {}
57
58  virtual ~test_keyfile()
59  {}
60
61  void setUp()
62  {
63    std::istringstream is("# Comment\n"
64                          "[group1]\n"
65                          "name=Fred Walker\n"
66                          "age=32\n"
67                          "# Test item comment\n"
68                          "#\n"
69                          "# spanning multiple lines\n"
70                          "numbers=1,2,3,4,5,6\n"
71                          "\n"
72                          "[group2]\n"
73                          "name=Mary King\n"
74                          "age=43\n"
75                          "photo=mary.jpeg\n");
76    this->kf = new sbuild::keyfile(is);
77  }
78
79  void tearDown()
80  {
81    delete this->kf;
82  }
83
84  void
85  test_construction_file()
86  {
87    sbuild::keyfile k(TESTDATADIR "/keyfile.ex1");
88  }
89
90  void
91  test_construction_stream()
92  {
93    std::ifstream strm(TESTDATADIR "/keyfile.ex1");
94    CPPUNIT_ASSERT(strm);
95    sbuild::keyfile k(strm);
96  }
97
98  void
99  test_construction_fail()
100  {
101    sbuild::keyfile k(TESTDATADIR "/nonexistent-keyfile-will-throw-exception");
102  }
103
104  void test_get_groups()
105  {
106    sbuild::string_list l = this->kf->get_groups();
107    CPPUNIT_ASSERT(l.size() == 2);
108    CPPUNIT_ASSERT(l[0] == "group1");
109    CPPUNIT_ASSERT(l[1] == "group2");
110
111    CPPUNIT_ASSERT(this->kf->has_group("group1") == true);
112    CPPUNIT_ASSERT(this->kf->has_group("nonexistent") == false);
113  }
114
115  void test_get_keys()
116  {
117    sbuild::string_list l = this->kf->get_keys("group2");
118    CPPUNIT_ASSERT(l.size() == 3);
119    CPPUNIT_ASSERT(l[0] == "age");
120    CPPUNIT_ASSERT(l[1] == "name");
121    CPPUNIT_ASSERT(l[2] == "photo");
122
123    CPPUNIT_ASSERT(this->kf->has_key("group2", "name") == true);
124    CPPUNIT_ASSERT(this->kf->has_key("nonexistent", "name") == false);
125    CPPUNIT_ASSERT(this->kf->has_key("group1", "nonexistent") == false);
126  }
127
128  void test_get_value()
129  {
130    std::string sval;
131    int ival;
132
133    CPPUNIT_ASSERT(this->kf->get_value("group2", "name", sval) == true);
134    CPPUNIT_ASSERT(sval == "Mary King");
135    CPPUNIT_ASSERT(this->kf->get_value("group2", "age", ival) == true);
136    CPPUNIT_ASSERT(ival == 43);
137
138    // Check failure does not alter value.
139    ival = 11;
140    CPPUNIT_ASSERT(this->kf->get_value("group2", "nonexistent", ival) == false);
141    CPPUNIT_ASSERT(ival == 11);
142  }
143
144  void test_get_value_fail()
145  {
146    bool bval = false;
147
148    // Expect a warning here.
149    CPPUNIT_ASSERT(this->kf->get_value("group2", "age", bval) == false);
150    CPPUNIT_ASSERT(bval == false);
151  }
152
153  void test_get_list_value()
154  {
155    std::vector<int> expected;
156    expected.push_back(1);
157    expected.push_back(2);
158    expected.push_back(3);
159    expected.push_back(4);
160    expected.push_back(5);
161    expected.push_back(6);
162
163    std::vector<int> found;
164    CPPUNIT_ASSERT(this->kf->get_list_value("group1", "numbers", found) == true);
165    CPPUNIT_ASSERT(found == expected);
166  }
167
168  void test_get_list_value_fail()
169  {
170    std::vector<int> expected;
171    expected.push_back(1);
172    expected.push_back(2);
173    expected.push_back(3);
174    expected.push_back(4);
175    expected.push_back(5);
176    expected.push_back(6);
177
178    std::vector<bool> found;
179
180    // Expect a warning here.
181    CPPUNIT_ASSERT(this->kf->get_list_value("group1", "numbers", found) == false);
182    CPPUNIT_ASSERT(found.size() == 1); // 1 converts to bool.
183  }
184
185  // TODO: Test priority.
186  // TODO: Test comments, when available.
187
188  void test_get_line()
189  {
190    CPPUNIT_ASSERT(this->kf->get_line("group2") == 10);
191    CPPUNIT_ASSERT(this->kf->get_line("group1", "age") == 4);
192    CPPUNIT_ASSERT(this->kf->get_line("group2", "name") == 11);
193  }
194
195  void test_set_value()
196  {
197    this->kf->set_value("group1", "name", "Adam Smith");
198    this->kf->set_value("group1", "age", 27);
199    this->kf->set_value("group1", "interests", "Ice Hockey,GNU/Linux");
200    this->kf->set_value("newgroup", "newitem", 89);
201
202    std::string result;
203    int number = 0;
204    CPPUNIT_ASSERT(this->kf->get_value("group1", "name", result) == true);
205    CPPUNIT_ASSERT(result == "Adam Smith");
206    CPPUNIT_ASSERT(this->kf->get_value("group1", "age", number) == true);
207    CPPUNIT_ASSERT(number == 27);
208    CPPUNIT_ASSERT(this->kf->get_value("group1", "interests", result) == true);
209    CPPUNIT_ASSERT(result == "Ice Hockey,GNU/Linux");
210    CPPUNIT_ASSERT(this->kf->get_value("newgroup", "newitem", number) == true);
211    CPPUNIT_ASSERT(number == 89);
212  }
213
214  void test_set_list_value()
215  {
216    std::vector<int> expected;
217    expected.push_back(1);
218    expected.push_back(2);
219    expected.push_back(3);
220    expected.push_back(4);
221    expected.push_back(5);
222    expected.push_back(6);
223
224    std::vector<int> found;
225
226    this->kf->set_list_value("listgroup", "numbers2",
227                             expected.begin(), expected.end());
228    CPPUNIT_ASSERT(this->kf->get_list_value("listgroup", "numbers2", found) == true);
229    CPPUNIT_ASSERT(found == expected);
230  }
231
232  void test_remove_group()
233  {
234    CPPUNIT_ASSERT(this->kf->get_groups().size() == 2);
235
236    this->kf->set_value("newgroup", "newitem", 89);
237
238    CPPUNIT_ASSERT(this->kf->get_groups().size() == 3);
239
240    this->kf->remove_group("group1");
241
242    sbuild::string_list l = this->kf->get_groups();
243    CPPUNIT_ASSERT(l.size() == 2);
244    CPPUNIT_ASSERT(l[0] == "group2");
245    CPPUNIT_ASSERT(l[1] == "newgroup");
246  }
247
248  void test_remove_key()
249  {
250    CPPUNIT_ASSERT(this->kf->get_keys("group2").size() == 3);
251
252    this->kf->remove_key("group2", "photo");
253
254    sbuild::string_list l = this->kf->get_keys("group2");
255    CPPUNIT_ASSERT(l.size() == 2);
256    CPPUNIT_ASSERT(l[0] == "age");
257    CPPUNIT_ASSERT(l[1] == "name");
258  }
259
260  void test_output()
261  {
262    // TODO: Add comments, when available.
263    std::ostringstream os;
264    os << *this->kf;
265
266    CPPUNIT_ASSERT(os.str() ==
267                   "# Comment\n"
268                   "[group1]\n"
269                   "age=32\n"
270                   "name=Fred Walker\n"
271                   "# Test item comment\n"
272                   "#\n"
273                   "# spanning multiple lines\n"
274                   "numbers=1,2,3,4,5,6\n"
275                   "\n"
276                   "[group2]\n"
277                   "age=43\n"
278                   "name=Mary King\n"
279                   "photo=mary.jpeg\n");
280  }
281
282};
283
284CPPUNIT_TEST_SUITE_REGISTRATION(test_keyfile);
Note: See TracBrowser for help on using the repository browser.