1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli2.option;
18
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.ListIterator;
22 import java.util.Set;
23
24 import org.apache.commons.cli2.DisplaySetting;
25 import org.apache.commons.cli2.Option;
26 import org.apache.commons.cli2.OptionException;
27 import org.apache.commons.cli2.Parent;
28 import org.apache.commons.cli2.WriteableCommandLine;
29 import org.apache.commons.cli2.commandline.WriteableCommandLineImpl;
30
31
32
33
34
35
36
37 public class DefaultOptionTest extends ParentTestCase {
38
39 public static DefaultOption buildHelpOption() {
40 final Set aliases = new HashSet(list("-h", "-?"));
41 return new DefaultOption(
42 "-",
43 "--",
44 true,
45 "--help",
46 "Displays the help",
47 aliases,
48 aliases,
49 false,
50 null,
51 null,
52 'h');
53 }
54
55 public static DefaultOption buildXOption() {
56 return new DefaultOption(
57 "-",
58 "--",
59 true,
60 "-X",
61 "This is needed",
62 null,
63 null,
64 true,
65 null,
66 null,
67 'X');
68 }
69
70
71
72
73
74
75 public void testProcessParent() throws OptionException {
76 final DefaultOption option = buildHelpOption();
77 final List args = list("--help");
78 final WriteableCommandLine commandLine = commandLine(option, args);
79 final ListIterator iterator = args.listIterator();
80 option.processParent(commandLine, iterator);
81
82 assertFalse(iterator.hasNext());
83 assertTrue(commandLine.hasOption(option));
84 assertTrue(commandLine.hasOption("--help"));
85 assertTrue(commandLine.hasOption("-?"));
86 assertTrue(commandLine.getValues(option).isEmpty());
87 }
88
89 public void testProcessParent_Burst() throws OptionException {
90 final DefaultOption option = buildHelpOption();
91 final List args = list("-help");
92 final WriteableCommandLine commandLine = commandLine(option, args);
93 final ListIterator iterator = args.listIterator();
94 option.processParent(commandLine, iterator);
95
96 assertEquals("-elp", iterator.next());
97 assertFalse(iterator.hasNext());
98 assertTrue(commandLine.hasOption(option));
99 assertTrue(commandLine.hasOption("--help"));
100 assertTrue(commandLine.hasOption("-?"));
101 assertTrue(commandLine.getValues(option).isEmpty());
102 }
103
104
105
106
107
108
109 public void testCanProcess() {
110 final DefaultOption option = buildHelpOption();
111 assertTrue(option.canProcess(new WriteableCommandLineImpl(option,null), "-?"));
112 }
113
114 public void testCanProcess_BadMatch() {
115 final DefaultOption option = buildHelpOption();
116 assertFalse(option.canProcess(new WriteableCommandLineImpl(option,null), "-H"));
117 }
118
119
120
121
122
123
124 public void testPrefixes() {
125 final DefaultOption option = buildHelpOption();
126 assertContentsEqual(list("-", "--"), option.getPrefixes());
127 }
128
129
130
131
132
133
134 public void testProcess() {
135
136
137 }
138
139
140
141
142
143
144 public void testTriggers() {
145 final DefaultOption option = buildHelpOption();
146 assertContentsEqual(list("-?", "-h", "--help"), option.getTriggers());
147 }
148
149
150
151
152
153
154 public void testValidate() {
155 final Parent option = buildXOption();
156 final WriteableCommandLine commandLine = commandLine(option, list());
157
158 try {
159 option.validate(commandLine);
160 fail("Missing an option");
161 }
162 catch (OptionException moe) {
163 assertSame(option, moe.getOption());
164 }
165 }
166
167
168
169
170
171
172 public void testAppendUsage() {
173 final Option option = buildHelpOption();
174 final StringBuffer buffer = new StringBuffer();
175 option.appendUsage(buffer, DisplaySetting.ALL, null);
176
177 assertEquals("[--help (-?,-h)]", buffer.toString());
178 }
179
180 public void testAppendUsage_NoOptional() {
181 final Option option = buildHelpOption();
182 final StringBuffer buffer = new StringBuffer();
183 final Set settings = new HashSet(DisplaySetting.ALL);
184 settings.remove(DisplaySetting.DISPLAY_OPTIONAL);
185 option.appendUsage(buffer, settings, null);
186
187 assertEquals("--help (-?,-h)", buffer.toString());
188 }
189
190 public void testAppendUsage_NoAlias() {
191 final Option option = buildHelpOption();
192 final StringBuffer buffer = new StringBuffer();
193 final Set settings = new HashSet(DisplaySetting.ALL);
194 settings.remove(DisplaySetting.DISPLAY_ALIASES);
195 option.appendUsage(buffer, settings, null);
196
197 assertEquals("[--help]", buffer.toString());
198 }
199
200
201
202
203
204
205 public void testGetPreferredName() {
206 final Option option = buildHelpOption();
207 assertEquals("--help", option.getPreferredName());
208 }
209
210
211
212
213
214
215 public void testGetDescription() {
216 final Option option = buildHelpOption();
217 assertEquals("Displays the help", option.getDescription());
218 }
219
220
221
222
223
224 public void testHelpLines() {
225
226 }
227 }