wibble  1.1
options.test.h
Go to the documentation of this file.
1 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2  (c) 2007 Enrico Zini <enrico@enricozini.org> */
3 
5 
6 #include <wibble/test.h>
7 
8 using namespace std;
9 using namespace wibble::commandline;
10 
12 
13  // Happy trick to get access to protected methods we need to use for the tests
14  template<typename T>
15  class Public : public T
16  {
17  public:
18  Public(const std::string& name)
19  : T(name) {}
20  Public(const std::string& name,
21  char shortName,
22  const std::string& longName,
23  const std::string& usage = std::string(),
24  const std::string& description = std::string())
25  : T(name, shortName, longName, usage, description) {}
26 
28  return T::parse(a, begin);
29  }
30  virtual bool parse(const std::string& str) {
31  return T::parse(str);
32  }
33  };
34 
36  Public<BoolOption> opt("test");
37 
38  assert_eq(opt.name(), string("test"));
39  assert_eq(opt.isSet(), false);
40  assert_eq(opt.boolValue(), false);
41  assert_eq(opt.stringValue(), string("false"));
42 
43  assert_eq(opt.parse(string()), false);
44  assert_eq(opt.isSet(), true);
45  assert_eq(opt.boolValue(), true);
46  assert_eq(opt.stringValue(), string("true"));
47  }
48 
50  Public<IntOption> opt("test");
51 
52  assert_eq(opt.name(), string("test"));
53  assert_eq(opt.isSet(), false);
54  assert_eq(opt.boolValue(), false);
55  assert_eq(opt.intValue(), 0);
56  assert_eq(opt.stringValue(), string("0"));
57 
58  assert_eq(opt.parse("42"), true);
59  assert_eq(opt.isSet(), true);
60  assert_eq(opt.boolValue(), true);
61  assert_eq(opt.intValue(), 42);
62  assert_eq(opt.stringValue(), string("42"));
63  }
64 
66  Public<StringOption> opt("test");
67 
68  assert_eq(opt.name(), string("test"));
69  assert_eq(opt.isSet(), false);
70  assert_eq(opt.boolValue(), false);
71  assert_eq(opt.stringValue(), string());
72 
73  assert_eq(opt.parse("-a"), true);
74  assert_eq(opt.isSet(), true);
75  assert_eq(opt.boolValue(), true);
76  assert_eq(opt.stringValue(), "-a");
77  }
78 
80  Public< VectorOption<Bool> > opt("test");
81  assert_eq(opt.name(), string("test"));
82  assert_eq(opt.isSet(), false);
83  assert_eq(opt.boolValue(), false);
84  assert_eq(opt.values().size(), 0u);
85 
86  assert_eq(opt.parse("yes"), true);
87  assert_eq(opt.isSet(), true);
88  assert_eq(opt.boolValue(), true);
89  assert_eq(opt.values().size(), 1u);
90  assert_eq(opt.values()[0], true);
91 
92  assert_eq(opt.parse("no"), true);
93  assert_eq(opt.isSet(), true);
94  assert_eq(opt.boolValue(), true);
95  assert_eq(opt.values().size(), 2u);
96  assert_eq(opt.values()[0], true);
97  assert_eq(opt.values()[1], false);
98  }
99 
101  Public< VectorOption<String> > opt("test");
102  assert_eq(opt.name(), string("test"));
103  assert_eq(opt.isSet(), false);
104  assert_eq(opt.boolValue(), false);
105  assert_eq(opt.values().size(), 0u);
106 
107  assert_eq(opt.parse("-a"), true);
108  assert_eq(opt.isSet(), true);
109  assert_eq(opt.boolValue(), true);
110  assert_eq(opt.values().size(), 1u);
111  assert_eq(opt.values()[0], "-a");
112 
113  assert_eq(opt.parse("foo"), true);
114  assert_eq(opt.isSet(), true);
115  assert_eq(opt.boolValue(), true);
116  assert_eq(opt.values().size(), 2u);
117  assert_eq(opt.values()[0], "-a");
118  assert_eq(opt.values()[1], "foo");
119  }
120 };
121 
122 // vim:set ts=4 sw=4:
virtual ArgList::iterator parse(ArgList &a, ArgList::iterator begin)
Definition: options.test.h:27
Iterator< typename I::value_type > iterator(I i)
Definition: iterator.h:123
Definition: core.cpp:6
Definition: options.test.h:15
virtual bool parse(const std::string &str)
Definition: options.test.h:30
void Test
Definition: test.h:178
Definition: options.test.h:11
Test boolOpt()
Definition: options.test.h:35
void usage(ostream &out, const string &argv0)
Definition: commandline-demo.cpp:75
ListIterator< List > begin(List l)
Definition: list.h:420
#define assert_eq(x, y)
Definition: test.h:33
Test vectorStringOpt()
Definition: options.test.h:100
Public(const std::string &name)
Definition: options.test.h:18
Public(const std::string &name, char shortName, const std::string &longName, const std::string &usage=std::string(), const std::string &description=std::string())
Definition: options.test.h:20
Test vectorBoolOpt()
Definition: options.test.h:79
Definition: core.h:29
Test stringOpt()
Definition: options.test.h:65
Test intOpt()
Definition: options.test.h:49