2
// OptionProcessorTest.cpp
4
// $Id: //poco/1.2/Util/testsuite/src/OptionProcessorTest.cpp#1 $
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9
// Permission is hereby granted, free of charge, to any person or organization
10
// obtaining a copy of the software and accompanying documentation covered by
11
// this license (the "Software") to use, reproduce, display, distribute,
12
// execute, and transmit the Software, and to prepare derivative works of the
13
// Software, and to permit third-parties to whom the Software is furnished to
14
// do so, all subject to the following:
16
// The copyright notices in the Software and this entire statement, including
17
// the above license grant, this restriction and the following disclaimer,
18
// must be included in all copies of the Software, in whole or in part, and
19
// all derivative works of the Software, unless such copies or derivative
20
// works are solely in the form of machine-executable object code generated by
21
// a source language processor.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
26
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
27
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
28
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29
// DEALINGS IN THE SOFTWARE.
33
#include "OptionProcessorTest.h"
34
#include "CppUnit/TestCaller.h"
35
#include "CppUnit/TestSuite.h"
36
#include "Poco/Util/Option.h"
37
#include "Poco/Util/OptionSet.h"
38
#include "Poco/Util/OptionProcessor.h"
39
#include "Poco/Util/OptionException.h"
42
using Poco::Util::Option;
43
using Poco::Util::OptionSet;
44
using Poco::Util::OptionProcessor;
47
OptionProcessorTest::OptionProcessorTest(const std::string& name): CppUnit::TestCase(name)
52
OptionProcessorTest::~OptionProcessorTest()
57
void OptionProcessorTest::testUnix()
61
Option("include-dir", "I", "specify a search path for locating header files")
67
Option("library-dir", "L", "specify a search path for locating library files")
73
Option("output", "o", "specify the output file", true)
74
.argument("file", true));
77
Option("verbose", "v")
78
.description("enable verbose mode")
83
Option("optimize", "O")
84
.description("enable optimization")
87
.argument("level", false)
92
.description("generate debug information")
99
.description("print information")
103
OptionProcessor p1(set);
107
assert (p1.process("-I/usr/include", name, value));
108
assert (name == "include-dir");
109
assert (value == "/usr/include");
111
assert (p1.process("--include:/usr/local/include", name, value));
112
assert (name == "include-dir");
113
assert (value == "/usr/local/include");
115
assert (p1.process("--lib=/usr/local/lib", name, value));
116
assert (name == "library-dir");
117
assert (value == "/usr/local/lib");
119
assert (p1.process("-ofile", name, value));
120
assert (name == "output");
121
assert (value == "file");
123
assert (!p1.process("src/file.cpp", name, value));
124
assert (!p1.process("/src/file.cpp", name, value));
128
p1.process("--output:file", name, value);
129
fail("duplicate - must throw");
131
catch (Poco::Util::DuplicateOptionException&)
135
assert (p1.process("-g", name, value));
136
assert (name == "debug");
137
assert (value == "");
141
p1.process("--optimize", name, value);
142
fail("incompatible - must throw");
144
catch (Poco::Util::IncompatibleOptionsException&)
150
p1.process("-x", name, value);
151
fail("unknown option - must throw");
153
catch (Poco::Util::UnknownOptionException&)
159
p1.process("--in", name, value);
160
fail("ambiguous option - must throw");
162
catch (Poco::Util::AmbiguousOptionException&)
168
void OptionProcessorTest::testDefault()
172
Option("include-dir", "I", "specify a search path for locating header files")
178
Option("library-dir", "L", "specify a search path for locating library files")
184
Option("output", "o", "specify the output file", true)
185
.argument("file", true));
188
Option("verbose", "v")
189
.description("enable verbose mode")
194
Option("optimize", "O")
195
.description("enable optimization")
198
.argument("level", false)
203
.description("generate debug information")
210
.description("print information")
214
OptionProcessor p1(set);
215
p1.setUnixStyle(false);
219
assert (p1.process("/Inc:/usr/include", name, value));
220
assert (name == "include-dir");
221
assert (value == "/usr/include");
223
assert (p1.process("/include:/usr/local/include", name, value));
224
assert (name == "include-dir");
225
assert (value == "/usr/local/include");
227
assert (p1.process("/lib=/usr/local/lib", name, value));
228
assert (name == "library-dir");
229
assert (value == "/usr/local/lib");
231
assert (p1.process("/out:file", name, value));
232
assert (name == "output");
233
assert (value == "file");
235
assert (!p1.process("src/file.cpp", name, value));
236
assert (!p1.process("\\src\\file.cpp", name, value));
240
p1.process("/output:file", name, value);
241
fail("duplicate - must throw");
243
catch (Poco::Util::DuplicateOptionException&)
247
assert (p1.process("/debug", name, value));
248
assert (name == "debug");
249
assert (value == "");
253
p1.process("/OPT", name, value);
254
fail("incompatible - must throw");
256
catch (Poco::Util::IncompatibleOptionsException&)
262
p1.process("/x", name, value);
263
fail("unknown option - must throw");
265
catch (Poco::Util::UnknownOptionException&)
271
p1.process("/in", name, value);
272
fail("ambiguous option - must throw");
274
catch (Poco::Util::AmbiguousOptionException&)
280
void OptionProcessorTest::testRequired()
284
Option("option", "o")
288
OptionProcessor p1(set);
295
fail("no options specified - must throw");
297
catch (Poco::Util::MissingOptionException&)
301
assert (p1.process("-o", name, value));
306
void OptionProcessorTest::setUp()
311
void OptionProcessorTest::tearDown()
316
CppUnit::Test* OptionProcessorTest::suite()
318
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OptionProcessorTest");
320
CppUnit_addTest(pSuite, OptionProcessorTest, testUnix);
321
CppUnit_addTest(pSuite, OptionProcessorTest, testDefault);
322
CppUnit_addTest(pSuite, OptionProcessorTest, testRequired);