1 | // |
2 | // OptionSetTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "OptionSetTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Util/OptionSet.h" |
15 | #include "Poco/Util/Option.h" |
16 | #include "Poco/Util/OptionException.h" |
17 | |
18 | |
19 | using Poco::Util::OptionSet; |
20 | using Poco::Util::Option; |
21 | |
22 | |
23 | OptionSetTest::OptionSetTest(const std::string& name): CppUnit::TestCase(name) |
24 | { |
25 | } |
26 | |
27 | |
28 | OptionSetTest::~OptionSetTest() |
29 | { |
30 | } |
31 | |
32 | |
33 | void OptionSetTest::testOptionSet() |
34 | { |
35 | OptionSet set; |
36 | set.addOption( |
37 | Option("helper" , "H" , "start helper" ) |
38 | .required(false) |
39 | .repeatable(false)); |
40 | set.addOption( |
41 | Option("help" , "h" , "print help text" ) |
42 | .required(false) |
43 | .repeatable(false)); |
44 | set.addOption( |
45 | Option("include-dir" , "I" , "specify a search path for locating header files" ) |
46 | .required(false) |
47 | .repeatable(true) |
48 | .argument("path" )); |
49 | set.addOption( |
50 | Option("library-dir" , "L" , "specify a search path for locating library files" ) |
51 | .required(false) |
52 | .repeatable(true) |
53 | .argument("path" )); |
54 | set.addOption( |
55 | Option("insert" , "it" , "insert something" ) |
56 | .required(false) |
57 | .repeatable(true) |
58 | .argument("path" )); |
59 | set.addOption( |
60 | Option("item" , "" , "insert something" ) |
61 | .required(false) |
62 | .repeatable(true) |
63 | .argument("path" )); |
64 | set.addOption( |
65 | Option("include" , "J" , "specify a search path for locating header files" ) |
66 | .required(false) |
67 | .repeatable(true) |
68 | .argument("path" )); |
69 | |
70 | assertTrue (set.hasOption("include" , false)); |
71 | assertTrue (set.hasOption("I" , true)); |
72 | assertTrue (set.hasOption("Include" , true)); |
73 | assertTrue (set.hasOption("insert" , false)); |
74 | assertTrue (set.hasOption("it" , true)); |
75 | assertTrue (set.hasOption("Insert" , false)); |
76 | assertTrue (set.hasOption("item" , false)); |
77 | assertTrue (!set.hasOption("i" , false)); |
78 | assertTrue (!set.hasOption("in" , false)); |
79 | |
80 | assertTrue (set.hasOption("help" )); |
81 | assertTrue (set.hasOption("h" , true)); |
82 | assertTrue (set.hasOption("helper" )); |
83 | assertTrue (set.hasOption("H" , true)); |
84 | |
85 | const Option& opt1 = set.getOption("include" ); |
86 | assertTrue (opt1.fullName() == "include" ); |
87 | |
88 | const Option& opt2 = set.getOption("item" ); |
89 | assertTrue (opt2.fullName() == "item" ); |
90 | |
91 | const Option& opt3 = set.getOption("I" , true); |
92 | assertTrue (opt3.fullName() == "include-dir" ); |
93 | |
94 | const Option& opt4 = set.getOption("include-d" ); |
95 | assertTrue (opt4.fullName() == "include-dir" ); |
96 | |
97 | const Option& opt5 = set.getOption("help" ); |
98 | assertTrue (opt5.fullName() == "help" ); |
99 | |
100 | const Option& opt6 = set.getOption("helpe" ); |
101 | assertTrue (opt6.fullName() == "helper" ); |
102 | |
103 | try |
104 | { |
105 | set.getOption("in" ); |
106 | fail("ambiguous - must throw" ); |
107 | } |
108 | catch (Poco::Util::AmbiguousOptionException&) |
109 | { |
110 | } |
111 | |
112 | try |
113 | { |
114 | set.getOption("he" ); |
115 | fail("ambiguous - must throw" ); |
116 | } |
117 | catch (Poco::Util::AmbiguousOptionException&) |
118 | { |
119 | } |
120 | |
121 | try |
122 | { |
123 | set.getOption("i" ); |
124 | fail("ambiguous - must throw" ); |
125 | } |
126 | catch (Poco::Util::AmbiguousOptionException&) |
127 | { |
128 | } |
129 | } |
130 | |
131 | |
132 | void OptionSetTest::setUp() |
133 | { |
134 | } |
135 | |
136 | |
137 | void OptionSetTest::tearDown() |
138 | { |
139 | } |
140 | |
141 | |
142 | CppUnit::Test* OptionSetTest::suite() |
143 | { |
144 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("OptionSetTest" ); |
145 | |
146 | CppUnit_addTest(pSuite, OptionSetTest, testOptionSet); |
147 | |
148 | return pSuite; |
149 | } |
150 | |