001: /*
002: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
003: Licensed under the Academic Free License version 3.0
004: */
005:
006: package joptsimple;
007:
008: import java.util.Arrays;
009: import java.util.Collections;
010:
011: import junit.framework.TestCase;
012:
013: /**
014: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
015: * @version $Id: InterleavedArgumentsTest.java,v 1.4 2007/04/10 20:06:26 pholser Exp $
016: */
017: public class InterleavedArgumentsTest extends TestCase {
018: public void testOnlyAppearingToHaveOptionArguments() {
019: OptionParser parser = new OptionParser("c");
020: OptionSet options = parser.parse(new String[] { "-c", "a",
021: "-c", "b", "-c", "c", "-c", "d" });
022:
023: assertTrue(options.wasDetected("c"));
024: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("c"));
025: assertEquals(
026: Arrays.asList(new String[] { "a", "b", "c", "d" }),
027: options.nonOptionArguments());
028: }
029:
030: public void testOnlyAppearingToHaveOptionArgumentsButPosixlyCorrect() {
031: OptionParser parser = new OptionParser("+c");
032: OptionSet options = parser.parse(new String[] { "-c", "a",
033: "-c", "b", "-c", "c", "-c", "d" });
034:
035: assertTrue(options.wasDetected("c"));
036: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("c"));
037: assertEquals(Arrays.asList(new String[] { "a", "-c", "b", "-c",
038: "c", "-c", "d" }), options.nonOptionArguments());
039: }
040:
041: public void testRequiredArgument() {
042: OptionParser parser = new OptionParser("c:");
043: OptionSet options = parser.parse(new String[] { "-c", "a",
044: "-c", "b", "-c", "c", "-c", "d" });
045:
046: assertTrue(options.wasDetected("c"));
047: assertEquals(
048: Arrays.asList(new String[] { "a", "b", "c", "d" }),
049: options.argumentsOf("c"));
050: assertEquals(Collections.EMPTY_LIST, options
051: .nonOptionArguments());
052: }
053:
054: public void testRequiredArgumentAndPosixlyCorrect() {
055: OptionParser parser = new OptionParser("+c:");
056: OptionSet options = parser.parse(new String[] { "-c", "a",
057: "-c", "b", "-c", "c", "-c", "d" });
058:
059: assertTrue(options.wasDetected("c"));
060: assertEquals(
061: Arrays.asList(new String[] { "a", "b", "c", "d" }),
062: options.argumentsOf("c"));
063: assertEquals(Collections.EMPTY_LIST, options
064: .nonOptionArguments());
065: }
066:
067: public void testOptionalArgument() {
068: OptionParser parser = new OptionParser("c::");
069: OptionSet options = parser.parse(new String[] { "-c", "a",
070: "-c", "b", "-c", "c", "-c", "d" });
071:
072: assertTrue(options.wasDetected("c"));
073: assertEquals(
074: Arrays.asList(new String[] { "a", "b", "c", "d" }),
075: options.argumentsOf("c"));
076: assertEquals(Collections.EMPTY_LIST, options
077: .nonOptionArguments());
078: }
079:
080: public void testOptionalArgumentAndPosixlyCorrect() {
081: OptionParser parser = new OptionParser("+c::");
082: OptionSet options = parser.parse(new String[] { "-c", "a",
083: "-c", "b", "-c", "c", "-c", "d" });
084:
085: assertTrue(options.wasDetected("c"));
086: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("c"));
087: assertEquals(Arrays.asList(new String[] { "a", "-c", "b", "-c",
088: "c", "-c", "d" }), options.nonOptionArguments());
089: }
090:
091: public void testLeadingNonOptionCausesPosixlyCorrectToIgnoreRemainder() {
092: OptionParser parser = new OptionParser("+c:");
093: String[] args = { "boo", "-c", "a", "-c", "b", "-c", "c", "-c",
094: "d" };
095: OptionSet options = parser.parse(args);
096:
097: assertFalse(options.wasDetected("c"));
098: assertEquals(Collections.EMPTY_LIST, options.argumentsOf("c"));
099: assertEquals(Arrays.asList(args), options.nonOptionArguments());
100: }
101:
102: public void testOptionalAbuttedArgumentVersusPoxislyCorrect() {
103: OptionParser parser = new OptionParser("+c::");
104: OptionSet options = parser.parse(new String[] { "-ca", "-cb",
105: "-c", "c", "-c", "d" });
106:
107: assertTrue(options.wasDetected("c"));
108: assertEquals(Arrays.asList(new String[] { "a", "b" }), options
109: .argumentsOf("c"));
110: assertEquals(Arrays.asList(new String[] { "c", "-c", "d" }),
111: options.nonOptionArguments());
112: }
113:
114: public void testOptionalKeyValuePairArgumentVersusPoxislyCorrect() {
115: OptionParser parser = new OptionParser("+c::");
116: OptionSet options = parser.parse(new String[] { "-c=a", "-c=b",
117: "-c", "c", "-c", "d" });
118:
119: assertTrue(options.wasDetected("c"));
120: assertEquals(Arrays.asList(new String[] { "a", "b" }), options
121: .argumentsOf("c"));
122: assertEquals(Arrays.asList(new String[] { "c", "-c", "d" }),
123: options.nonOptionArguments());
124: }
125: }
|