-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathModelArgSpecTest.java
More file actions
467 lines (407 loc) · 19.4 KB
/
ModelArgSpecTest.java
File metadata and controls
467 lines (407 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package picocli;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.TestRule;
import picocli.CommandLine.IParameterConsumer;
import picocli.CommandLine.Model.ArgSpec;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Model.IGetter;
import picocli.CommandLine.Model.IScope;
import picocli.CommandLine.Model.ISetter;
import picocli.CommandLine.Model.ITypeInfo;
import picocli.CommandLine.Model.OptionSpec;
import picocli.CommandLine.Model.OptionSpec.Builder;
import picocli.CommandLine.Model.PositionalParamSpec;
import picocli.CommandLine.Model.RuntimeTypeInfo;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import static org.junit.Assert.*;
public class ModelArgSpecTest {
// allows tests to set any kind of properties they like, without having to individually roll them back
@Rule
public final TestRule restoreSystemProperties = new RestoreSystemProperties();
@Rule
public final ProvideSystemProperty ansiOFF = new ProvideSystemProperty("picocli.ansi", "false");
@Test
public void testArgSpecConstructorWithEmptyAuxTypes() {
PositionalParamSpec positional = PositionalParamSpec.builder().auxiliaryTypes(new Class[0]).build();
assertEquals(CommandLine.Range.valueOf("1"), positional.arity());
assertEquals(String.class, positional.type());
assertArrayEquals(new Class[] {String.class}, positional.auxiliaryTypes());
}
@Test
@Deprecated public void testArgSpecRenderedDescriptionInitial() {
PositionalParamSpec positional = PositionalParamSpec.builder().build();
assertArrayEquals(new String[0], positional.renderedDescription());
PositionalParamSpec positional2 = PositionalParamSpec.builder().description(new String[0]).build();
assertArrayEquals(new String[0], positional2.renderedDescription());
}
@Test
public void testArgSpecGetter() {
IGetter getter = new IGetter() {
public <T> T get() { return null; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build();
assertSame(getter, positional.getter());
}
@Test
public void testArgSpecGetterRethrowsPicocliException() {
final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom");
IGetter getter = new IGetter() {
public <T> T get() { throw expected; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build();
try {
positional.getValue();
} catch (CommandLine.PicocliException ex) {
assertSame(expected, ex);
}
}
@Test
public void testArgSpecGetterWrapNonPicocliException() {
final Exception expected = new Exception("boom");
IGetter getter = new IGetter() {
public <T> T get() throws Exception { throw expected; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().getter(getter).build();
try {
positional.getValue();
} catch (CommandLine.PicocliException ex) {
assertSame(expected, ex.getCause());
}
}
@Test
public void testArgSpecSetterRethrowsPicocliException() {
final CommandLine.PicocliException expected = new CommandLine.PicocliException("boom");
ISetter setter = new ISetter() {
public <T> T set(T value) throws Exception { throw expected; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
try {
positional.setValue("abc");
} catch (CommandLine.PicocliException ex) {
assertSame(expected, ex);
}
}
@Test
public void testArgSpecSetValueCallsSetter() {
final Object[] newVal = new Object[1];
ISetter setter = new ISetter() {
public <T> T set(T value) { newVal[0] = value; return null; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
positional.setValue("abc");
assertEquals("abc", newVal[0]);
}
@SuppressWarnings("deprecation")
@Test
public void testArgSpecSetValueWithCommandLineCallsSetter() {
final Object[] newVal = new Object[1];
ISetter setter = new ISetter() {
public <T> T set(T value) { newVal[0] = value; return null; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
positional.setValue("abc", new CommandLine(CommandSpec.create()));
assertEquals("abc", newVal[0]);
}
@Test
public void testArgSpecSetterWrapNonPicocliException() {
final Exception expected = new Exception("boom");
ISetter setter = new ISetter() {
public <T> T set(T value) throws Exception { throw expected; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
try {
positional.setValue("abc");
} catch (CommandLine.PicocliException ex) {
assertSame(expected, ex.getCause());
}
}
@Test
public void testArgSpecSetter2WrapNonPicocliException() {
final Exception expected = new Exception("boom");
ISetter setter = new ISetter() {
public <T> T set(T value) throws Exception { throw expected; }
};
PositionalParamSpec positional = PositionalParamSpec.builder().setter(setter).build();
try {
positional.setValue("abc");
} catch (CommandLine.PicocliException ex) {
assertSame(expected, ex.getCause());
}
}
@Test
public void testArgSpecEquals() {
PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
.arity("1")
.hideParamSyntax(true)
.required(true)
.splitRegex(";")
.description("desc")
.descriptionKey("key")
.type(Map.class)
.auxiliaryTypes(Integer.class, Double.class)
.inherited(true)
.scopeType(CommandLine.ScopeType.INHERIT);
PositionalParamSpec p1 = positional.build();
assertEquals(p1, positional.build());
assertNotEquals(p1, positional.arity("2").build());
assertNotEquals(p1, positional.arity("1").hideParamSyntax(false).build());
assertNotEquals(p1, positional.hideParamSyntax(true).required(false).build());
assertNotEquals(p1, positional.required(true).splitRegex(",").build());
assertNotEquals(p1, positional.splitRegex(";").description("xyz").build());
assertNotEquals(p1, positional.description("desc").descriptionKey("XX").build());
assertNotEquals(p1, positional.descriptionKey("key").type(List.class).build());
assertNotEquals(p1, positional.type(Map.class).auxiliaryTypes(Short.class).build());
assertEquals(p1, positional.auxiliaryTypes(Integer.class, Double.class).build());
assertNotEquals(p1, positional.inherited(false).build());
assertEquals(p1, positional.inherited(true).build());
assertNotEquals(p1, positional.scopeType(CommandLine.ScopeType.LOCAL).build());
assertEquals(p1, positional.scopeType(CommandLine.ScopeType.INHERIT).build());
}
@Test
public void testArgSpecBuilderDescriptionKey() {
PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
.descriptionKey("key");
assertEquals("key", positional.descriptionKey());
assertEquals("xxx", positional.descriptionKey("xxx").descriptionKey());
}
@Test
public void testArgSpecBuilderHideParamSyntax() {
PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
.hideParamSyntax(true);
assertEquals(true, positional.hideParamSyntax());
assertEquals(false, positional.hideParamSyntax(false).hideParamSyntax());
}
@Test
public void testArgSpecBuilderHasInitialValue() {
PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
.hasInitialValue(true);
assertEquals(true, positional.hasInitialValue());
assertEquals(false, positional.hasInitialValue(false).hasInitialValue());
}
@Test
public void testArgSpecBuilderCompletionCandidates() {
List<String> candidates = Arrays.asList("a", "b");
PositionalParamSpec.Builder positional = PositionalParamSpec.builder()
.completionCandidates(candidates);
assertEquals(candidates, positional.completionCandidates());
}
private static class AnnotatedImpl implements CommandLine.Model.IAnnotatedElement {
Object userObject;
String name;
ITypeInfo typeInfo;
AnnotatedImpl() {}
AnnotatedImpl(String name, ITypeInfo typeInfo) {
this(null, name, typeInfo);
}
AnnotatedImpl(Object userObject, String name, ITypeInfo typeInfo) {
this.userObject = userObject;
this.name = name;
this.typeInfo = typeInfo;
}
public Object userObject() { return userObject; }
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) { return false;}
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) { return null;}
public String getName() {return name;}
public String getMixinName() {return null;}
public String[] getOptionNameTransformations() {return null;}
public boolean isArgSpec() {return false;}
public boolean isOption() {return false;}
public boolean isParameter() {return false;}
public boolean isArgGroup() {return false;}
public boolean isMixin() {return false;}
public boolean isUnmatched() { return false;}
public boolean isSpec() {return false;}
public boolean isParentCommand() {return false;}
public boolean isMultiValue() {return false;}
public boolean isInteractive() {return false;}
public boolean hasInitialValue() {return false;}
public boolean isMethodParameter() {return false;}
public int getMethodParamPosition() {return 0;}
public IScope scope() {return null;}
public IGetter getter() {return null;}
public ISetter setter() {return null;}
public ITypeInfo getTypeInfo() {return typeInfo;}
public String getToString() {return null;}
}
@Test
public void testArgSpecBuilderInferLabel() throws Exception{
Method m = ArgSpec.Builder.class.getDeclaredMethod("inferLabel", String.class, CommandLine.Model.IAnnotatedElement.class);
m.setAccessible(true);
assertEquals("<String=String>", m.invoke(null, "", new AnnotatedImpl("fieldName", typeInfo(new Class[0]))));
assertEquals("<String=String>", m.invoke(null, "", new AnnotatedImpl("fieldName", typeInfo(new Class[]{Integer.class}))));
assertEquals("<String=String>", m.invoke(null, "", new AnnotatedImpl("fieldName", typeInfo(new Class[]{null, Integer.class}))));
assertEquals("<String=String>", m.invoke(null, "", new AnnotatedImpl("fieldName", typeInfo(new Class[]{Integer.class, null}))));
assertEquals("<Integer=Integer>", m.invoke(null, "", new AnnotatedImpl("fieldName", typeInfo(new Class[]{Integer.class, Integer.class}))));
assertEquals("<prefix>", m.invoke(null, null, new AnnotatedImpl(m,"prefix$postfix", new TypeInfoAdapter())));
}
private ITypeInfo typeInfo(final Class<?>[] aux) {
return new TypeInfoAdapter() {
public boolean isMap() { return true; }
public List<ITypeInfo> getAuxiliaryTypeInfos() {
List<ITypeInfo> result = new ArrayList<ITypeInfo>();
for (final Class<?> c : aux) {
if (c == null) { result.add(null); }
result.add(new TypeInfoAdapter() {
public String getClassSimpleName() { return c.getSimpleName(); }
});
}
return result;
}
};
}
static class TypeInfoAdapter implements ITypeInfo {
public boolean isMap() { return false; }
public List<ITypeInfo> getAuxiliaryTypeInfos() { return null; }
public List<String> getActualGenericTypeArguments() { return null; }
public boolean isBoolean() { return false; }
public boolean isMultiValue() { return false; }
public boolean isArray() { return false; }
public boolean isCollection() { return false; }
public boolean isOptional() { return false; }
public boolean isEnum() { return false; }
public List<String> getEnumConstantNames() { return null; }
public String getClassName() { return null; }
public String getClassSimpleName() { return null; }
public Class<?> getType() { return null; }
public Class<?>[] getAuxiliaryTypes() { return new Class[0]; }
}
@Test
public void testArgSpecUserObject() {
class App {
@CommandLine.Parameters
List<String> args;
@CommandLine.Option(names = "-x") String x;
}
CommandLine cmd = new CommandLine(new App());
Object x = cmd.getCommandSpec().findOption("x").userObject();
assertEquals(Field.class, x.getClass());
assertEquals("x", ((Field) x).getName());
Object args = cmd.getCommandSpec().positionalParameters().get(0).userObject();
assertEquals(Field.class, args.getClass());
assertEquals("args", ((Field) args).getName());
}
@Test
public void testArgSpecBuilderUserObject() {
assertNull(OptionSpec.builder("-x").userObject());
assertEquals("aaa", OptionSpec.builder("-x").userObject("aaa").userObject());
assertNull(PositionalParamSpec.builder().userObject());
assertEquals("aaa", PositionalParamSpec.builder().userObject("aaa").userObject());
}
@Test
public void testArgSpecBuilderTypeInfo() {
ITypeInfo typeInfo = OptionSpec.builder("-x").typeInfo();
assertNull(typeInfo);
}
@Test
public void testArgSpecBuilderSetTypeInfo() {
ITypeInfo typeInfo = new RuntimeTypeInfo(List.class, new Class[]{String.class}, Arrays.asList("boo"));
Builder builder = OptionSpec.builder("-x").typeInfo(typeInfo);
assertSame(typeInfo, builder.typeInfo());
assertEquals(List.class, builder.type());
assertArrayEquals(new Class[]{String.class}, builder.auxiliaryTypes());
try {
builder.typeInfo(null);
fail("Expected NPE");
} catch (NullPointerException ok) {
}
}
@Test
public void testArgSpecBuilderObjectBindingToString() {
Builder builder = OptionSpec.builder("-x");
assertEquals("ObjectBinding(value=null)", builder.getter().toString());
}
@Test(expected = NullPointerException.class)
public void testOptionSpecBuilderSplitRegexDisallowsNull() {
OptionSpec.builder("-x").splitRegex(null);
}
@Test(expected = NullPointerException.class)
public void testPositionalParamSpecBuilderSplitRegexDisallowsNull() {
PositionalParamSpec.builder().splitRegex(null);
}
@Test(expected = NullPointerException.class)
public void testOptionSpecBuilderSplitRegexSynopsisLabelDisallowsNull() {
OptionSpec.builder("-x").splitRegexSynopsisLabel(null);
}
@Test(expected = NullPointerException.class)
public void testPositionalParamSpecBuilderSplitRegexSynopsisLabelDisallowsNull() {
PositionalParamSpec.builder().splitRegexSynopsisLabel(null);
}
@Test
public void testArgSpecBuilderSplitRegex() {
Builder builder = OptionSpec.builder("-x").type(String[].class).splitRegex(";");
assertEquals("split regex", ";", builder.splitRegex());
assertEquals("split regex", ";", builder.build().splitRegex());
PositionalParamSpec.Builder pb = PositionalParamSpec.builder().type(String[].class).splitRegex(";");
assertEquals("split regex", ";", pb.splitRegex());
assertEquals("split regex", ";", pb.build().splitRegex());
}
@Test
public void testArgSpecBuilderSplitRegexSynopsisLabel() {
Builder builder = OptionSpec.builder("-x").splitRegexSynopsisLabel(";");
assertEquals("split regex synopsis label", ";", builder.splitRegexSynopsisLabel());
assertEquals("split regex synopsis label", ";", builder.build().splitRegexSynopsisLabel());
PositionalParamSpec.Builder pb = PositionalParamSpec.builder().splitRegexSynopsisLabel(";");
assertEquals("split regex synopsis label", ";", pb.splitRegexSynopsisLabel());
assertEquals("split regex synopsis label", ";", pb.build().splitRegexSynopsisLabel());
}
@Test
public void testArgSpecBuilderSplitRegexAndSplitRegexSynopsisLabel() {
Builder builder = OptionSpec.builder("-x").type(String[].class).splitRegex("#").splitRegexSynopsisLabel(";");
assertEquals("split regex synopsis label", ";", builder.splitRegexSynopsisLabel());
assertEquals("split regex synopsis label", ";", builder.build().splitRegexSynopsisLabel());
PositionalParamSpec.Builder pb = PositionalParamSpec.builder().type(String[].class).splitRegex("#").splitRegexSynopsisLabel(";");
assertEquals("split regex synopsis label", ";", pb.splitRegexSynopsisLabel());
assertEquals("split regex synopsis label", ";", pb.build().splitRegexSynopsisLabel());
}
@Test
public void testPositionalParamSpecBuilderCapacity() {
PositionalParamSpec.Builder builder = PositionalParamSpec.builder();
assertNull(builder.capacity());
CommandLine.Range value = CommandLine.Range.valueOf("1..2");
builder.capacity(value);
assertSame(value, builder.capacity());
}
@Test
public void testArgSpec_defaultValueString() {
PositionalParamSpec spec = PositionalParamSpec.builder().defaultValue("Hi ${user.name}").build();
assertEquals("Hi ${user.name}", spec.defaultValueString());
}
@Test
public void testArgSpecBuilder_parameterConsumerGetter() {
PositionalParamSpec.Builder builder = PositionalParamSpec.builder();
assertNull(builder.parameterConsumer());
}
@Test
public void testArgSpecBuilder_parameterConsumerSetter() {
IParameterConsumer consumer = new IParameterConsumer() {
public void consumeParameters(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) {
}
};
PositionalParamSpec.Builder builder = PositionalParamSpec.builder().parameterConsumer(consumer);
assertSame(consumer, builder.parameterConsumer());
}
@Test
public void testArgSpecBuilder_scopeGetter() {
PositionalParamSpec.Builder builder = PositionalParamSpec.builder();
assertNotNull(builder.scope());
}
@Test
public void testArgSpecBuilder_scopeSetter() {
IScope scope = new IScope() {
public <T> T get() { return null; }
public <T> T set(T value) { return null; }
};
PositionalParamSpec.Builder builder = PositionalParamSpec.builder().scope(scope);
assertSame(scope, builder.scope());
}
}