root/trunk/examples/util/config/FunctionTest.d

Revision 39, 49.9 kB (checked in by aarti_pl, 2 months ago)

- updated licences
- database package updates

Line 
1 /*******************************************************************************
2
3     License:    Boost Software License, v. 1.0
4                 Academic Free License, v. 3.0
5                 BSD License
6
7     Authors:    Marcin Kuszczak, www.zapytajmnie.com (author's christian site)
8                 This software is inspired and partially based on Boost C++
9                 library named 'program_options' and created by Vladimir Prus.
10
11     Version:    0.9.1
12     Date:       30-Apr-2008
13
14     History:    0.9.0 (08-Oct-2007) -   initial public version
15
16     Description:
17                 Function test module
18
19  ******************************************************************************/
20
21 import std.stdio;
22 import std.string;
23 import std2.conv;
24
25 import doost.core.Any;
26 import doost.util.DUnit;
27 import doost.util.config.ProgramOptions;        //Always
28 import doost.util.config.CommandLineStorage;    //Only if backend is necessary
29 import doost.util.config.ConfigFileStorage;     //Only if backend is necessary
30 import doost.util.config.EnvironmentStorage;    //Only if backend is necessary
31 import doost.util.config.DbStorage;             //Only if backend is necessary
32 import doost.util.config.Formatter;             //Only for non-standard output
33                                                 //description and for user defined
34                                                 //formatters
35
36 //import doost.util.config.Value;
37
38 version(ddbi_v62) {
39     import dbi.Database;
40     import dbi.Row;
41     import dbi.sqlite.SqliteDatabase;
42 }
43
44 //------------------------------------------------------------------------------
45
46 class MyVal {
47     this(int v) {
48         this.v=v;
49     }
50     int opEquals(MyVal v) {
51         assert(v !is null);
52         return (v.v == this.v);
53     }
54     int v;
55 }
56
57 //------------------------------------------------------------------------------
58 MyVal parseMyValue(string s) {
59     int val;
60     try {
61         val=to!(int)(s);
62     } catch (Exception) {
63         throw new InvalidOptionValueException(s);
64     }
65     return new MyVal(val);
66 }
67
68 //------------------------------------------------------------------------------
69
70 unittest {
71     testSuite.begin("ProgramOptions");
72 }
73
74 //------------------------------------------------------------------------------
75
76 unittest { testCase.execute("Opt.Description - regular options", {
77     RegularOptions ro;
78
79     ro = new RegularOptions("Command line");
80     ro.options()
81         ("help,h", "produce help message")
82         ("doTheTest,d", "maketest")
83         ("doTheCoffee,f", "makecoffee")
84         ("doTheTea,a", define!(bool)("tak|ok|1|włÄ
85 cz"), "maketea")
86         ("turnItOn,r", boolSwitch, "makecoffee")
87         ("compression,c", define!(int), "set compression level")
88         ("title,t", define!(char[]).defaultValue("title"), "set title of window")
89         ("include,i", define!(char[][]).composing.defaultValue(["default1"[], "default2"]), "include paths")
90         ("myVal", define!(MyVal).parser(&parseMyValue), "include paths")
91         ("firstname", define!(char[][]).composing, "firstname")
92         ("secondname", define!(char[][]).composing, "secondname")
93         ("nickname,n", define!(char[][]).composing, "nickname")
94         ("date", define!(char[])(r"\d\d\d\d-\d?\d-\d?\d"), "date of birth")
95         ("regexp[ression]*,g", define!(char[]), "regular expression", new RegExpOption)
96         ("number\\d\\d", define!(byte), "following numbers", new RegExpOption)
97         ("int,p", define!(char[])(r"\d\d\d"), new RegExpOption)
98         ("string,s", define!(char[]), "text parameter")
99         ("other,o", define!(double), "other parameter")
100         //Only alias option
101         (",J", define!(char[][]), "import paths")
102     ;
103 });}
104
105 //------------------------------------------------------------------------------
106
107 unittest { testCase.execute("Opt.Description - adding groups", {
108     RegularOptions ro, ro1, ro2;
109     string real_desc, expc_desc;
110
111
112     ro = new RegularOptions("First group");
113     ro.options()
114         ("help,h", "produce help message")
115         ("compression,c", define!(uint), "compression level")
116         ("date", define!(char[]), "start date")
117     ;
118
119     ro1 = new RegularOptions("Second group");
120     ro1.options()
121         ("title,t", define!(char[]), "window title")
122         ("process,p", define!(uint), "process number")
123     ;
124
125     ro2 = new RegularOptions("All options");
126     ro2.add(ro).add(ro1);
127
128     ro2 = new RegularOptions("All options");
129     ro2.add(ro, ro1);
130
131     ro2 = new RegularOptions("All options");
132     ro2.options()
133             (ro)
134             (ro1)
135     ;
136
137     real_desc = ro2.toString;
138     //std.file.write("ro2.txt", cast(void[])real_desc);
139     expc_desc ="
140         All options
141
142         First group
143             help                  produce help message
144             compression arg       compression level
145             date arg              start date
146
147         Second group
148             title arg             window title
149             process arg           process number
150     ";
151
152     //writefln("\n", real_desc);
153     assert(removechars(real_desc, " \t\n\r") == removechars(expc_desc, " \t\n\r"));
154
155     ro2 = new RegularOptions("All options");
156     ro2.options()
157         ("firstname", define!(char[][]).composing, "firstname")
158         ("secondname", define!(char[][]).composing, "secondname")
159         (ro)
160         (ro1);
161
162     real_desc = ro2.toString;
163     //std.file.write("ro2.txt", cast(void[])real_desc);
164     expc_desc ="
165         All options
166             firstname arg         firstname
167             secondname arg        secondname
168
169         First group
170             help                  produce help message
171             compression arg       compression level
172             date arg              start date
173
174         Second group
175             title arg             window title
176             process arg           process number
177     ";
178
179     //writefln("\n", real_desc);
180     assert(removechars(real_desc, " \t\n\r") == removechars(expc_desc, " \t\n\r"));
181 });}
182
183 //------------------------------------------------------------------------------
184
185 unittest { testCase.execute("CL Opt.Description - special CL options", {
186     auto spc = new CommandLineOptions;
187     spc.options()
188         ("selfpath", new SelfPath)
189         ("selfdir", new SelfDir)
190         ("selfname", new SelfName)
191         ("firstname=2, secondname=2, nickname=*", new PositionalOption)
192     ;
193
194 });}
195
196 //------------------------------------------------------------------------------
197
198 unittest { testCase.execute("CL Opt.Description - options sanity", {
199     RegularOptions ro;
200     CommandLineOptions clo;
201
202     assert(checkAssert({
203         ro = new RegularOptions;
204         ro.options()
205             ("title,t", define!(char[]), "window title")
206             ("process,t", define!(uint), "process number")
207         ;
208     }));
209
210     assert(checkAssert({
211         ro = new RegularOptions;
212         ro.options()
213             ("title", define!(char[]), "window title")
214             ("title,t", define!(uint), "process number")
215         ;
216     }));
217
218     assert(checkAssert({
219         ro = new RegularOptions;
220         ro.options()
221             ("title", define!(char[]), "window title")
222             ("title,t", define!(uint), "process number")
223         ;
224     }));
225
226     assert(checkAssert({
227         ro = new RegularOptions;
228         ro.options()
229             (".*", define!(char[]), "all other options")
230             (".*", define!(char[]), "other other options")
231         ;
232     }));
233
234     assert(checkAssert({
235         clo = new CommandLineOptions;
236         clo.options()
237             ("selfpath", new SelfPath)
238             ("selfpath", new SelfPath)
239         ;
240     }));
241
242 });}
243
244 //------------------------------------------------------------------------------
245
246 unittest { testCase.execute("CL Storage - lists", {
247     char[][] args;
248     RegularOptions ro;
249     ProgramOptions po;
250
251     args = ["bin", "--help", "--include", "'a','b','c'"];
252
253     ro = new RegularOptions("Command line");
254     ro.options()
255         ("help,h", "produce help message")
256         ("include,i", define!(char[][]).composing.defaultValue(["default1"[], "default2"]), "include paths")
257         ("firstname", define!(char[][]).composing, "firstname")
258         ("secondname", define!(char[][]).composing, "secondname")
259         ("nickname,n", define!(char[][]).composing, "nickname")
260     ;
261
262     po = (new ProgramOptions).next(
263             new CommandLineStorage(args, ro)
264          );
265
266     po.connect;
267     assert(("help" in po) !is null);
268     assert(checkException!(UnknownOptionException)({"blah" in po; }));
269
270     assert(po["include"].as!(char[][]) == ["a", "b", "c"]);
271     po.disconnect;
272
273     args = ["bin", "--include", "a,b,c", "--firstname=Anita , 'Ewa Krystyna', Paulina",
274     "--secondname", "'Korwin\\tMikke'", "-n[Aarti, Bono, Bruce]"];
275     po.storage!(CommandLineStorage).args(args);
276
277     po.connect;
278
279     trace(po["include"].as!(char[][]));
280     assert(po["include"].as!(char[][]) == ["a", "b", "c"]);
281     assert(po["firstname"].as!(char[][]) == ["Anita", "Ewa Krystyna", "Paulina"]);
282     trace(po["secondname"].as!(char[][]));
283     assert(po["secondname"].as!(char[][]) == ["Korwin\tMikke"]);
284     trace(po["nickname"].as!(char[][]));
285     assert(po["nickname"].as!(char[][]) == ["Aarti", "Bono", "Bruce"]);
286
287     po.disconnect;
288 });}
289
290 //------------------------------------------------------------------------------
291
292 unittest { testCase.execute("CL Storage - long/short options", {
293     char[][] args;
294     RegularOptions ro;
295     ProgramOptions po;
296
297     args = ["bin", "-rh"];
298
299     ro = new RegularOptions("Command line");
300     ro.options()
301         ("help,h", "produce help message")
302         ("turnItOn,r", boolSwitch, "makecoffee")
303     ;
304
305     po = (new ProgramOptions).next(
306             new CommandLineStorage(args, ro)
307          );
308
309     po.connect;
310     assert(("help" in po) !is null);
311     assert(("turnItOn" in po) !is null);
312     po.disconnect;
313
314     po.connect;
315     po.disconnect;
316 });}
317
318 //------------------------------------------------------------------------------
319
320 unittest { testCase.execute("CL Storage - response files", {
321     void[] buffer;
322     char[][] args;
323     RegularOptions ro;
324     ProgramOptions po;
325
326     buffer = cast(void[]) "-r \n--date=1975-07-04 \r\n --firstname=Ala,Ola,Sylwia
327     --secondname \n[Atkinson, Bullock]";
328
329     std.file.write("options.rsp", buffer);
330     scope(exit) std.file.remove("options.rsp");
331
332     args = ["bin", "@options.rsp"];
333
334     ro = new RegularOptions("Command line");
335     ro.options()
336         ("turnItOn,r", boolSwitch, "makecoffee")
337         ("date", define!(char[])(r"\d\d\d\d-\d?\d-\d?\d"), "date of birth")
338         ("firstname", define!(char[][]).composing, "firstname")
339         ("secondname", define!(char[][]).composing, "secondname")
340     ;
341
342     po = (new ProgramOptions).next(
343             new CommandLineStorage(args, ro)
344          );
345
346     po.connect;
347
348     assert(("turnItOn" in po) !is null);
349     assert(po["date"].as!(char[]) == "1975-07-04");
350     assert(po["firstname"].as!(char[][]) == ["Ala", "Ola", "Sylwia"]);
351     assert(po["secondname"].as!(char[][]) == ["Atkinson", "Bullock"]);
352
353     po.disconnect;
354 });}
355
356 //------------------------------------------------------------------------------
357
358 unittest { testCase.execute("CL Storage - custom values", {
359     char[][] args;
360     RegularOptions ro;
361     ProgramOptions po;
362
363     ro = new RegularOptions("Command line");
364     ro.options()
365         ("help,h", "produce help message")
366         ("compression,c", define!(int), "set compression level")
367         ("title,t", define!(char[]).defaultValue("title"), "set title of window")
368         ("doTheTest,d", "maketest")
369         ("myVal", define!(MyVal).parser(&parseMyValue), "include paths")
370     ;
371
372     args = ["bin", "--compression=8", "--myVal=7", "--help"];
373     po = (new ProgramOptions).next(
374             new CommandLineStorage(args, ro)
375          );
376
377     po.connect;
378
379     assert(("compression" in po) !is null);
380     assert(("doTheTest" in po) is null);
381     assert(po["compression"].as!(int) == 8);
382     assert(("title" in po) !is null);
383     assert(po["title"].as!(char[]) == "title");
384     assert(po["myVal"].as!(MyVal) == new MyVal(7));
385     po.disconnect;
386 });}
387
388 //------------------------------------------------------------------------------
389
390 unittest { testCase.execute("CL Storage - self path", {
391     char[][] args;
392     string str1, str2, str3;
393     RegularOptions ro;
394     CommandLineOptions clo;
395     ProgramOptions po;
396
397     version(linux) {
398         args = ["/usr/local/bin/potest", "--compression=8"];
399         str1 = "/usr/local/bin/potest";
400         str2 = "/usr/local/bin";
401         str3 = "potest";
402     } else
403     version(Win32) {
404         args = [r"C:\directory\file.exe", "--compression=8"];
405         str1 = r"C:\directory\file.exe";
406         str2 = r"C:\directory";
407         str3 = r"file.exe";
408     }
409
410     ro = new RegularOptions("Command line");
411     ro.options()
412         ("help,h", "produce help message")
413         ("compression,c", define!(int), "set compression level")
414     ;
415
416     clo = new CommandLineOptions;
417     clo.options()
418         ("selfpath", new SelfPath)
419         ("selfdir", new SelfDir)
420         ("selfname", new SelfName)
421     ;
422
423     po = (new ProgramOptions).next(
424             (new CommandLineStorage(args))
425                 .options(ro, clo)
426          );
427
428     po.connect;
429     assert(("selfpath" in po) !is null);
430     trace(po["selfpath"].as!(char[]));
431     assert(po["selfpath"].as!(char[]) == str1);
432     assert(("selfdir" in po) !is null);
433     assert(po["selfdir"].as!(char[]) == str2);
434     assert(("selfname" in po) !is null);
435     assert(po["selfname"].as!(char[]) == str3);
436     po.disconnect;
437 });}
438
439 //------------------------------------------------------------------------------
440
441 unittest { testCase.execute("CL Storage - synchronization", {
442     char[][] args;
443     RegularOptions ro;
444     ProgramOptions po;
445
446     ro = new RegularOptions("Command line");
447     ro.options()
448         ("help,h", "produce help message")
449         ("compression,c", define!(int), "set compression level")
450         ("title,t", define!(char[]).defaultValue("title"), "set title of window")
451         ("doTheTest,d", "maketest")
452     ;
453
454     args = ["bin", "--compression=8"];
455     po = (new ProgramOptions).next(
456             new CommandLineStorage(args, ro)
457          );
458     po.connect;
459     assert(po.storage!(CommandLineStorage).syncPolicy == SyncPolicy.Cached);
460     assert(po.storage!(CommandLineStorage).defaultSyncPolicy == SyncPolicy.Cached);
461     po.disconnect;
462 });}
463
464 //------------------------------------------------------------------------------
465
466 unittest { testCase.execute("CL Storage - constraints", {
467     char[][] args;
468     RegularOptions ro;
469     ProgramOptions po;
470
471     args = ["bin", "-i/usr/local", "-i/usr/", "-i/bin/", "--compression=9", "--title='Alien'"];
472
473     ro = new RegularOptions("Command line");
474     ro.options()
475         ("compression,c", define!(int), "set compression level")
476         ("title,t", define!(char[]).defaultValue("title"), "set title of window")
477         ("doTheTest,d", "maketest")
478         ("include,i", define!(char[][]).composing.defaultValue(["default1"[], "default2"]), "include paths")
479         ("myVal", define!(MyVal).parser(&parseMyValue), "include paths")
480         ("int,p", define!(char[])(r"\d\d\d"), new RegExpOption)
481         ("string,s", define!(char[]), "text parameter")
482         ("firstname", define!(char[][]).composing, "firstname")
483         ("secondname", define!(char[][]).composing, "secondname")
484         ("nickname,n", define!(char[][]).composing, "nickname")
485     ;
486
487     po = (new ProgramOptions).next(
488             new CommandLineStorage(args, ro)
489          );
490
491     po.connect;
492
493     assert(checkException!(ConstraintViolationException)({obligatoryOptions(po, ["int"]);}));
494     assert(checkException!(ConstraintViolationException)({obligatoryOptions(po.storage!(CommandLineStorage), ["int"]);}));
495     assert(checkException!(ConstraintViolationException)({conflictingOptions(po.storage!(CommandLineStorage), "compression", "title");}));
496     conflictingOptions(po.storage!(CommandLineStorage), "compression", "myVal");
497     assert(checkException!(ConstraintViolationException)({dependantOptions(po.storage!(CommandLineStorage), "compression", "string");}));
498
499     po.disconnect;
500 });}
501
502 //------------------------------------------------------------------------------
503
504 //TODO: additional test case: connect - disconnect - connect again - disconnect
505
506 unittest { testCase.execute("CL Storage - input options composing", {
507     char[][] args;
508     RegularOptions ro;
509     ProgramOptions po;
510
511     ro = new RegularOptions("Command line");
512     ro.options()
513         ("compression,c", define!(int), "set compression level")
514         ("title,t", define!(char[]).defaultValue("title"), "set title of window")
515         ("doTheTest,d", "maketest")
516         ("include,i", define!(char[][]).composing.defaultValue(["default1"[], "default2"]), "include paths")
517         ("int,p", define!(char[])(r"\d\d\d"), new RegExpOption)
518         ("string,s", define!(char[]), "text parameter")
519         ("firstname", define!(char[][]).composing, "firstname")
520         ("secondname", define!(char[][]).composing, "secondname")
521         ("nickname,n", define!(char[][]).composing, "nickname")
522     ;
523
524     args = ["bin", "-i/usr/local", "-i/usr/", "-i/bin/", "--int=265"];
525
526     po = (new ProgramOptions).next(
527             new CommandLineStorage(args, ro)
528          );
529
530     po.storage!(CommandLineStorage).args(args);
531     po.connect;
532
533     // compression is not passed as arguments, so below exception should be thrown
534     assert(checkException!(OptionHasNoValueException)({po["compression"];}));
535     assert(("compression" in po.storage!(CommandLineStorage)) is null);
536     assert(checkException!(OptionHasNoValueException)({po.storage!(CommandLineStorage)["compression"];}));
537     trace(po["include"].as!(char[][]));
538     assert(po["include"].as!(char[][]) == ["/usr/local", "/usr/", "/bin/"]);
539     po.disconnect;
540 });}
541
542 //------------------------------------------------------------------------------
543
544 unittest { testCase.execute("CL Storage - positional options", {
545     char[][] args;
546     RegularOptions ro;
547     CommandLineOptions clo;
548     ProgramOptions po;
549
550     ro = new RegularOptions("Command line");
551     ro.options()
552         ("compression,c", define!(int), "set compression level")
553         ("title,t", define!(char[]).defaultValue("title"), "set title of window")
554         ("doTheTest,d", "maketest")
555         ("include,i", define!(char[][]).composing.defaultValue(["default1"[], "default2"]), "include paths")
556         ("int,p", define!(char[])(r"\d\d\d"), new RegExpOption)
557         ("string,s", define!(char[]), "text parameter")
558         ("firstname", define!(char[][]).composing, "firstname")
559         ("secondname", define!(char[][]).composing