source: trunk/debathena/third/schroot/test/schroot-base-option-action.cc @ 24167

Revision 24167, 3.6 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 <schroot-base/schroot-base-option-action.h>
20
21#include <boost/program_options.hpp>
22
23#include <iostream>
24
25#include <cppunit/extensions/HelperMacros.h>
26
27using schroot_base::option_action;
28namespace opt = boost::program_options;
29using namespace CppUnit;
30
31class test_option_action : public TestFixture
32{
33  CPPUNIT_TEST_SUITE(test_option_action);
34  CPPUNIT_TEST(test_construction);
35  CPPUNIT_TEST(test_default);
36  CPPUNIT_TEST_EXCEPTION(test_default_fail, std::logic_error);
37  CPPUNIT_TEST(test_current);
38  CPPUNIT_TEST_EXCEPTION(test_current_fail, std::logic_error);
39  CPPUNIT_TEST_EXCEPTION(test_current_fail_multipleset,
40                         opt::validation_error);
41  CPPUNIT_TEST(test_operators);
42  CPPUNIT_TEST_EXCEPTION(test_operators_fail_multipleset,
43                         opt::validation_error);
44  CPPUNIT_TEST_SUITE_END();
45
46protected:
47  option_action *action;
48
49public:
50  test_option_action():
51    TestFixture(),
52    action()
53  {}
54
55  virtual ~test_option_action()
56  {}
57
58  void setUp()
59  {
60    this->action = new option_action;
61  }
62
63  static void
64  add_examples(option_action& act)
65  {
66    act.add("help");
67    act.add("version");
68    act.add("list");
69    act.add("info");
70    act.add("config");
71  }
72
73  void tearDown()
74  {
75    delete this->action;
76  }
77
78  void
79  test_construction()
80  {
81    option_action act;
82  }
83
84  void
85  test_default()
86  {
87    add_examples(*this->action);
88    CPPUNIT_ASSERT(this->action->get_default() == "");
89    CPPUNIT_ASSERT(this->action->get() == "");
90
91    this->action->set_default("info");
92    CPPUNIT_ASSERT(this->action->get_default() == "info");
93    CPPUNIT_ASSERT(this->action->get() == "info");
94  }
95
96  void
97  test_default_fail()
98  {
99    add_examples(*this->action);
100
101    this->action->set_default("invalid");
102  }
103
104  void
105  test_current()
106  {
107    add_examples(*this->action);
108
109    CPPUNIT_ASSERT(this->action->get_default() == "");
110    CPPUNIT_ASSERT(this->action->get() == "");
111    this->action->set_default("list");
112
113    CPPUNIT_ASSERT(this->action->get_default() == "list");
114    CPPUNIT_ASSERT(this->action->get() == "list");
115
116    this->action->set("config");
117    CPPUNIT_ASSERT(this->action->get_default() == "list");
118    CPPUNIT_ASSERT(this->action->get() == "config");
119  }
120
121  void
122  test_current_fail()
123  {
124    add_examples(*this->action);
125
126    this->action->set("invalid");
127  }
128
129  void
130  test_current_fail_multipleset()
131  {
132    add_examples(*this->action);
133
134    this->action->set("list");
135    this->action->set("info");
136  }
137
138  void
139  test_operators()
140  {
141    add_examples(*this->action);
142
143    *this->action = "list";
144    CPPUNIT_ASSERT(*this->action == "list");
145    CPPUNIT_ASSERT(!(*this->action != "list"));
146    CPPUNIT_ASSERT(*this->action != "invalid");
147    CPPUNIT_ASSERT(!(*this->action == "invalid"));
148  }
149
150  void
151  test_operators_fail_multipleset()
152  {
153    add_examples(*this->action);
154
155    *this->action = "list";
156    *this->action = "config";
157  }
158};
159
160CPPUNIT_TEST_SUITE_REGISTRATION(test_option_action);
Note: See TracBrowser for help on using the repository browser.