1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
#include <wibble/config.h>
#include <wibble/commandline/engine.h>
#if 0
#include <ostream>
#endif
#include <wibble/tests/tut-wibble.h>
namespace tut {
struct commandline_engine_shar {
};
TESTGRP( commandline_engine );
using namespace wibble::commandline;
using namespace std;
// Happy trick to get access to protected methods we need to use for the tests
template<typename T>
class Public : public T
{
public:
Public(MemoryManager* mman = 0, const std::string& name = std::string(),
const std::string& usage = std::string(),
const std::string& description = std::string(),
const std::string& longDescription = std::string())
: T(mman, name, usage, description, longDescription) {}
ArgList::iterator parseList(ArgList& list) { return T::parseList(list); }
ArgList::iterator parse(ArgList& list, ArgList::iterator begin)
{
return T::parse(list, begin);
}
};
class TestEngine : public Public<Engine>
{
MemoryManager mman;
public:
TestEngine() : Public<Engine>(&mman)
{
antani = add<BoolOption>("antani", 'a', "antani");
blinda = add<StringOption>("blinda", 'b', "blinda");
antani->addAlias("an-tani");
}
BoolOption* antani;
StringOption* blinda;
};
// Test mix of options and arguments
template<> template<>
void to::test<1>()
{
ArgList opts;
opts.push_back("ciaps");
opts.push_back("-b");
opts.push_back("cippo");
opts.push_back("foobar");
TestEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.begin());
ensure_equals(opts.size(), 2u);
ensure_equals(string(*opts.begin()), string("ciaps"));
ensure_equals(string(*opts.rbegin()), string("foobar"));
ensure_equals(engine.antani->boolValue(), false);
ensure_equals(engine.blinda->stringValue(), "cippo");
}
// Test only options
template<> template<>
void to::test<2>()
{
ArgList opts;
opts.push_back("-a");
opts.push_back("foobar");
TestEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.begin());
ensure_equals(opts.size(), 1u);
ensure_equals(string(*opts.begin()), string("foobar"));
ensure_equals(engine.antani->boolValue(), true);
ensure_equals(engine.blinda->boolValue(), false);
}
// Test clustered short options
template<> template<>
void to::test<3>()
{
ArgList opts;
opts.push_back("-ab");
opts.push_back("cippo");
TestEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.end());
ensure_equals(opts.size(), 0u);
ensure_equals(engine.antani->boolValue(), true);
ensure_equals(engine.blinda->stringValue(), "cippo");
}
// Test long options with dashes inside
template<> template<>
void to::test<4>()
{
ArgList opts;
opts.push_back("--an-tani");
opts.push_back("foobar");
TestEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.begin());
ensure_equals(opts.size(), 1u);
ensure_equals(string(*opts.begin()), string("foobar"));
ensure_equals(engine.antani->boolValue(), true);
ensure_equals(engine.blinda->boolValue(), false);
}
// Test long options with arguments
template<> template<>
void to::test<5>()
{
ArgList opts;
opts.push_back("--blinda=cippo");
opts.push_back("foobar");
opts.push_back("--antani");
TestEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.begin());
ensure_equals(opts.size(), 1u);
ensure_equals(string(*opts.begin()), string("foobar"));
ensure_equals(engine.antani->boolValue(), true);
ensure_equals(engine.blinda->stringValue(), "cippo");
}
class TestCEngine : public Public<Engine>
{
MemoryManager mman;
public:
TestCEngine() : Public<Engine>(&mman)
{
help = add<BoolOption>("help", 'h', "help", "get help");
scramble = addEngine("scramble");
scramble_random = scramble->add<BoolOption>("random", 'r', "random");
scramble_yell = scramble->add<StringOption>("yell", 0, "yell");
scramble->aliases.push_back("mess");
fix = addEngine("fix");
fix_quick = fix->add<BoolOption>("quick", 'Q', "quick");
fix_yell = fix->add<StringOption>("yell", 0, "yell");
}
BoolOption* help;
Engine* scramble;
BoolOption* scramble_random;
StringOption* scramble_yell;
Engine* fix;
BoolOption* fix_quick;
StringOption* fix_yell;
};
// Test command with arguments
template<> template<>
void to::test<6>()
{
ArgList opts;
opts.push_back("--yell=foo");
opts.push_back("mess");
opts.push_back("-r");
TestCEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.end());
ensure_equals(opts.size(), 0u);
ensure_equals(engine.foundCommand(), engine.scramble);
ensure_equals(engine.scramble_yell->stringValue(), "foo");
ensure_equals(engine.scramble_random->boolValue(), true);
ensure_equals(engine.fix_yell->stringValue(), string());
ensure_equals(engine.fix_quick->boolValue(), false);
ensure_equals(engine.help->boolValue(), false);
}
// Test the other command, with overlapping arguments
template<> template<>
void to::test<7>()
{
ArgList opts;
opts.push_back("--yell=foo");
opts.push_back("fix");
opts.push_back("--help");
opts.push_back("-Q");
TestCEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.end());
ensure_equals(opts.size(), 0u);
ensure_equals(engine.foundCommand(), engine.fix);
ensure_equals(engine.scramble_yell->stringValue(), string());
ensure_equals(engine.scramble_random->boolValue(), false);
ensure_equals(engine.fix_yell->stringValue(), "foo");
ensure_equals(engine.fix_quick->boolValue(), true);
ensure_equals(engine.help->boolValue(), true);
}
// Test invocation without command
template<> template<>
void to::test<8>()
{
ArgList opts;
opts.push_back("--help");
TestCEngine engine;
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.end());
ensure_equals(opts.size(), 0u);
ensure_equals(engine.foundCommand(), (Engine*)0);
ensure_equals(engine.scramble_yell->stringValue(), string());
ensure_equals(engine.scramble_random->boolValue(), false);
ensure_equals(engine.fix_yell->stringValue(), string());
ensure_equals(engine.fix_quick->boolValue(), false);
ensure_equals(engine.help->boolValue(), true);
}
// Test creation shortcuts
template<> template<>
void to::test<9>()
{
MemoryManager mman;
Public<Engine> engine(&mman, "test", "[options]", "test engine", "this is the long description of a test engine");
OptionGroup* group = engine.addGroup("test option group");
BoolOption* testBool = group->add<BoolOption>("tbool", 0, "testbool", "<val>", "a test bool switch");
IntOption* testInt = group->add<IntOption>("tint", 0, "testint", "<val>", "a test int switch");
StringOption* testString = group->add<StringOption>("tstring", 0, "teststring", "<val>", "a test string switch");
BoolOption* testBool1 = engine.add<BoolOption>("tbool", 0, "testbool1", "<val>", "a test bool switch");
IntOption* testInt1 = engine.add<IntOption>("tint", 0, "testint1", "<val>", "a test int switch");
StringOption* testString1 = engine.add<StringOption>("tstring", 0, "teststring1", "<val>", "a test string switch");
ArgList opts;
opts.push_back("--testbool=true");
opts.push_back("--testint=3");
opts.push_back("--teststring=antani");
opts.push_back("--testbool1=true");
opts.push_back("--testint1=5");
opts.push_back("--teststring1=blinda");
ArgList::iterator i = engine.parseList(opts);
ensure(i == opts.end());
ensure_equals(opts.size(), 0u);
ensure_equals(testBool->boolValue(), true);
ensure_equals(testInt->intValue(), 3);
ensure_equals(testString->stringValue(), "antani");
ensure_equals(testBool1->boolValue(), true);
ensure_equals(testInt1->intValue(), 5);
ensure_equals(testString1->stringValue(), "blinda");
}
}
// vim:set ts=4 sw=4:
|