01: /*
02: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
03: Licensed under the Academic Free License version 3.0
04: */
05:
06: package joptsimple;
07:
08: import java.util.Collections;
09:
10: /**
11: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
12: * @version $Id: ShortOptionsOptionalArgumentTest.java,v 1.14 2007/04/10 20:06:27 pholser Exp $
13: */
14: public class ShortOptionsOptionalArgumentTest extends
15: AbstractOptionParserFixture {
16: protected void setUp() throws Exception {
17: super .setUp();
18:
19: parser.accepts("f").withOptionalArg();
20: parser.accepts("g").withOptionalArg();
21: parser.accepts("bar").withOptionalArg();
22: }
23:
24: public void testOptionWithOptionalArgumentNotPresent() {
25: OptionSet options = parser.parse(new String[] { "-f" });
26: assertOptionDetected(options, "f");
27: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("f"));
28: assertEquals(Collections.EMPTY_LIST, options
29: .nonOptionArguments());
30: }
31:
32: public void testOptionWithOptionalArgumentPresent() {
33: OptionSet options = parser.parse(new String[] { "-f", "bar" });
34: assertOptionDetected(options, "f");
35: assertEquals(Collections.singletonList("bar"), options
36: .argumentsOf("f"));
37: assertEquals(Collections.EMPTY_LIST, options
38: .nonOptionArguments());
39: }
40:
41: public void testOptionWithOptionalArgumentThatLooksLikeAnInvalidOption() {
42: try {
43: parser.parse(new String[] { "-f", "--biz" });
44: fail();
45: } catch (UnrecognizedOptionException expected) {
46: assertEquals("biz", expected.option());
47: }
48: }
49:
50: public void testOptionWithOptionalArgumentThatLooksLikeAValidOption() {
51: OptionSet options = parser
52: .parse(new String[] { "-f", "--bar" });
53: assertOptionDetected(options, "f");
54: assertOptionDetected(options, "bar");
55: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("f"));
56: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("bar"));
57: assertEquals(Collections.EMPTY_LIST, options
58: .nonOptionArguments());
59: }
60:
61: public void testOptionWithOptionalArgumentFollowedByLegalOption() {
62: OptionSet options = parser.parse(new String[] { "-f", "-g" });
63: assertOptionDetected(options, "f");
64: assertOptionDetected(options, "g");
65: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("f"));
66: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("g"));
67: assertEquals(Collections.EMPTY_LIST, options
68: .nonOptionArguments());
69: }
70: }
|