~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to Util/testsuite/src/OptionProcessorTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// OptionProcessorTest.cpp
 
3
//
 
4
// $Id: //poco/1.2/Util/testsuite/src/OptionProcessorTest.cpp#1 $
 
5
//
 
6
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
 
7
// and Contributors.
 
8
//
 
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:
 
15
// 
 
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.
 
22
// 
 
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.
 
30
//
 
31
 
 
32
 
 
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"
 
40
 
 
41
 
 
42
using Poco::Util::Option;
 
43
using Poco::Util::OptionSet;
 
44
using Poco::Util::OptionProcessor;
 
45
 
 
46
 
 
47
OptionProcessorTest::OptionProcessorTest(const std::string& name): CppUnit::TestCase(name)
 
48
{
 
49
}
 
50
 
 
51
 
 
52
OptionProcessorTest::~OptionProcessorTest()
 
53
{
 
54
}
 
55
 
 
56
 
 
57
void OptionProcessorTest::testUnix()
 
58
{
 
59
        OptionSet set;
 
60
        set.addOption(
 
61
                Option("include-dir", "I", "specify a search path for locating header files")
 
62
                        .required(false)
 
63
                        .repeatable(true)
 
64
                        .argument("path"));
 
65
                        
 
66
        set.addOption(
 
67
                Option("library-dir", "L", "specify a search path for locating library files")
 
68
                        .required(false)
 
69
                        .repeatable(true)
 
70
                        .argument("path"));
 
71
 
 
72
        set.addOption(
 
73
                Option("output", "o", "specify the output file", true)
 
74
                        .argument("file", true));
 
75
 
 
76
        set.addOption(
 
77
                Option("verbose", "v")
 
78
                .description("enable verbose mode")
 
79
                .required(false)
 
80
                .repeatable(false));
 
81
                
 
82
        set.addOption(
 
83
                Option("optimize", "O")
 
84
                .description("enable optimization")
 
85
                .required(false)
 
86
                .repeatable(false)
 
87
                .argument("level", false)
 
88
                .group("mode"));
 
89
                
 
90
        set.addOption(
 
91
                Option("debug", "g")
 
92
                .description("generate debug information")
 
93
                .required(false)
 
94
                .repeatable(false)
 
95
                .group("mode"));
 
96
 
 
97
        set.addOption(
 
98
                Option("info", "i")
 
99
                .description("print information")
 
100
                .required(false)
 
101
                .repeatable(false));
 
102
 
 
103
        OptionProcessor p1(set);
 
104
        std::string name;
 
105
        std::string value;
 
106
        
 
107
        assert (p1.process("-I/usr/include", name, value));
 
108
        assert (name == "include-dir");
 
109
        assert (value == "/usr/include");
 
110
 
 
111
        assert (p1.process("--include:/usr/local/include", name, value));
 
112
        assert (name == "include-dir");
 
113
        assert (value == "/usr/local/include");
 
114
 
 
115
        assert (p1.process("--lib=/usr/local/lib", name, value));
 
116
        assert (name == "library-dir");
 
117
        assert (value == "/usr/local/lib");
 
118
        
 
119
        assert (p1.process("-ofile", name, value));
 
120
        assert (name == "output");
 
121
        assert (value == "file");
 
122
        
 
123
        assert (!p1.process("src/file.cpp", name, value));
 
124
        assert (!p1.process("/src/file.cpp", name, value));
 
125
        
 
126
        try
 
127
        {
 
128
                p1.process("--output:file", name, value);
 
129
                fail("duplicate - must throw");
 
130
        }
 
131
        catch (Poco::Util::DuplicateOptionException&)
 
132
        {
 
133
        }
 
134
        
 
135
        assert (p1.process("-g", name, value));
 
136
        assert (name == "debug");
 
137
        assert (value == "");
 
138
        
 
139
        try
 
140
        {
 
141
                p1.process("--optimize", name, value);
 
142
                fail("incompatible - must throw");
 
143
        }
 
144
        catch (Poco::Util::IncompatibleOptionsException&)
 
145
        {
 
146
        }
 
147
        
 
148
        try
 
149
        {
 
150
                p1.process("-x", name, value);
 
151
                fail("unknown option - must throw");
 
152
        }
 
153
        catch (Poco::Util::UnknownOptionException&)
 
154
        {
 
155
        }
 
156
 
 
157
        try
 
158
        {
 
159
                p1.process("--in", name, value);
 
160
                fail("ambiguous option - must throw");
 
161
        }
 
162
        catch (Poco::Util::AmbiguousOptionException&)
 
163
        {
 
164
        }
 
165
}
 
166
 
 
167
 
 
168
void OptionProcessorTest::testDefault()
 
169
{
 
170
        OptionSet set;
 
171
        set.addOption(
 
172
                Option("include-dir", "I", "specify a search path for locating header files")
 
173
                        .required(false)
 
174
                        .repeatable(true)
 
175
                        .argument("path"));
 
176
                        
 
177
        set.addOption(
 
178
                Option("library-dir", "L", "specify a search path for locating library files")
 
179
                        .required(false)
 
180
                        .repeatable(true)
 
181
                        .argument("path"));
 
182
 
 
183
        set.addOption(
 
184
                Option("output", "o", "specify the output file", true)
 
185
                        .argument("file", true));
 
186
 
 
187
        set.addOption(
 
188
                Option("verbose", "v")
 
189
                .description("enable verbose mode")
 
190
                .required(false)
 
191
                .repeatable(false));
 
192
                
 
193
        set.addOption(
 
194
                Option("optimize", "O")
 
195
                .description("enable optimization")
 
196
                .required(false)
 
197
                .repeatable(false)
 
198
                .argument("level", false)
 
199
                .group("mode"));
 
200
                
 
201
        set.addOption(
 
202
                Option("debug", "g")
 
203
                .description("generate debug information")
 
204
                .required(false)
 
205
                .repeatable(false)
 
206
                .group("mode"));
 
207
 
 
208
        set.addOption(
 
209
                Option("info", "i")
 
210
                .description("print information")
 
211
                .required(false)
 
212
                .repeatable(false));
 
213
 
 
214
        OptionProcessor p1(set);
 
215
        p1.setUnixStyle(false);
 
216
        std::string name;
 
217
        std::string value;
 
218
        
 
219
        assert (p1.process("/Inc:/usr/include", name, value));
 
220
        assert (name == "include-dir");
 
221
        assert (value == "/usr/include");
 
222
 
 
223
        assert (p1.process("/include:/usr/local/include", name, value));
 
224
        assert (name == "include-dir");
 
225
        assert (value == "/usr/local/include");
 
226
 
 
227
        assert (p1.process("/lib=/usr/local/lib", name, value));
 
228
        assert (name == "library-dir");
 
229
        assert (value == "/usr/local/lib");
 
230
        
 
231
        assert (p1.process("/out:file", name, value));
 
232
        assert (name == "output");
 
233
        assert (value == "file");
 
234
        
 
235
        assert (!p1.process("src/file.cpp", name, value));
 
236
        assert (!p1.process("\\src\\file.cpp", name, value));
 
237
        
 
238
        try
 
239
        {
 
240
                p1.process("/output:file", name, value);
 
241
                fail("duplicate - must throw");
 
242
        }
 
243
        catch (Poco::Util::DuplicateOptionException&)
 
244
        {
 
245
        }
 
246
        
 
247
        assert (p1.process("/debug", name, value));
 
248
        assert (name == "debug");
 
249
        assert (value == "");
 
250
        
 
251
        try
 
252
        {
 
253
                p1.process("/OPT", name, value);
 
254
                fail("incompatible - must throw");
 
255
        }
 
256
        catch (Poco::Util::IncompatibleOptionsException&)
 
257
        {
 
258
        }
 
259
        
 
260
        try
 
261
        {
 
262
                p1.process("/x", name, value);
 
263
                fail("unknown option - must throw");
 
264
        }
 
265
        catch (Poco::Util::UnknownOptionException&)
 
266
        {
 
267
        }
 
268
 
 
269
        try
 
270
        {
 
271
                p1.process("/in", name, value);
 
272
                fail("ambiguous option - must throw");
 
273
        }
 
274
        catch (Poco::Util::AmbiguousOptionException&)
 
275
        {
 
276
        }
 
277
}
 
278
 
 
279
 
 
280
void OptionProcessorTest::testRequired()
 
281
{
 
282
        OptionSet set;
 
283
        set.addOption(
 
284
                Option("option", "o")
 
285
                        .required(true)
 
286
                        .repeatable(true));
 
287
 
 
288
        OptionProcessor p1(set);
 
289
        std::string name;
 
290
        std::string value;
 
291
        
 
292
        try
 
293
        {
 
294
                p1.checkRequired();
 
295
                fail("no options specified - must throw");
 
296
        }
 
297
        catch (Poco::Util::MissingOptionException&)
 
298
        {
 
299
        }
 
300
        
 
301
        assert (p1.process("-o", name, value));
 
302
        p1.checkRequired();
 
303
}
 
304
 
 
305
 
 
306
void OptionProcessorTest::setUp()
 
307
{
 
308
}
 
309
 
 
310
 
 
311
void OptionProcessorTest::tearDown()
 
312
{
 
313
}
 
314
 
 
315
 
 
316
CppUnit::Test* OptionProcessorTest::suite()
 
317
{
 
318
        CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OptionProcessorTest");
 
319
 
 
320
        CppUnit_addTest(pSuite, OptionProcessorTest, testUnix);
 
321
        CppUnit_addTest(pSuite, OptionProcessorTest, testDefault);
 
322
        CppUnit_addTest(pSuite, OptionProcessorTest, testRequired);
 
323
 
 
324
        return pSuite;
 
325
}