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.NoSuchElementException;
009:
010: import junit.framework.TestCase;
011:
012: /**
013: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
014: * @version $Id: OptionSpecTokenizerTest.java,v 1.11 2007/04/10 20:06:26 pholser Exp $
015: */
016: public class OptionSpecTokenizerTest extends TestCase {
017: public void testTokenizeEmpty() {
018: assertNoMoreTokens(new OptionSpecTokenizer(""));
019: }
020:
021: public void testTokenizeOptionsWithoutArguments() {
022: OptionSpecTokenizer lexer = new OptionSpecTokenizer("ab");
023: assertNextTokenTakesNoArgument(lexer, 'a');
024: assertNextTokenTakesNoArgument(lexer, 'b');
025: assertNoMoreTokens(lexer);
026: }
027:
028: public void testTokenizeOptionsWithRequiredArguments() {
029: OptionSpecTokenizer lexer = new OptionSpecTokenizer("c:d:");
030: assertNextTokenRequiresAnArgument(lexer, 'c');
031: assertNextTokenRequiresAnArgument(lexer, 'd');
032: assertNoMoreTokens(lexer);
033: }
034:
035: public void testTokenizeOptionsWithOptionalArguments() {
036: OptionSpecTokenizer lexer = new OptionSpecTokenizer("e::f::");
037: assertNextTokenTakesAnOptionalArgument(lexer, 'e');
038: assertNextTokenTakesAnOptionalArgument(lexer, 'f');
039: assertNoMoreTokens(lexer);
040: }
041:
042: public void testTokenizeOptionsWithMixtureOfSpecTypes() {
043: OptionSpecTokenizer lexer = new OptionSpecTokenizer("gh:i::j");
044: assertNextTokenTakesNoArgument(lexer, 'g');
045: assertNextTokenRequiresAnArgument(lexer, 'h');
046: assertNextTokenTakesAnOptionalArgument(lexer, 'i');
047: assertNextTokenTakesNoArgument(lexer, 'j');
048: assertNoMoreTokens(lexer);
049: }
050:
051: public void testWByItself() {
052: OptionSpecTokenizer lexer = new OptionSpecTokenizer("W");
053: assertNextTokenTakesNoArgument(lexer, 'W');
054: assertNoMoreTokens(lexer);
055: }
056:
057: public void testWRequiredArg() {
058: OptionSpecTokenizer lexer = new OptionSpecTokenizer("W:");
059: assertNextTokenRequiresAnArgument(lexer, 'W');
060: assertNoMoreTokens(lexer);
061: }
062:
063: public void testWOptionalArg() {
064: OptionSpecTokenizer lexer = new OptionSpecTokenizer("W::");
065: assertNextTokenTakesAnOptionalArgument(lexer, 'W');
066: assertNoMoreTokens(lexer);
067: }
068:
069: public void testAlternativeLongOptionsMarker() {
070: OptionSpecTokenizer lexer = new OptionSpecTokenizer("W;");
071: assertNextTokenRequiresAnArgument(lexer, 'W');
072: assertNoMoreTokens(lexer);
073: }
074:
075: private static void assertNoMoreTokens(OptionSpecTokenizer lexer) {
076: assertFalse(lexer.hasMore());
077:
078: try {
079: lexer.next();
080: fail();
081: } catch (NoSuchElementException expected) {
082: assertTrue(expected.getMessage(), true);
083: }
084: }
085:
086: private static void assertNextTokenTakesNoArgument(
087: OptionSpecTokenizer lexer, char option) {
088:
089: assertNextToken(lexer, option, false, false);
090: }
091:
092: private static void assertNextTokenRequiresAnArgument(
093: OptionSpecTokenizer lexer, char option) {
094:
095: assertNextToken(lexer, option, true, true);
096: }
097:
098: private static void assertNextTokenTakesAnOptionalArgument(
099: OptionSpecTokenizer lexer, char option) {
100:
101: assertNextToken(lexer, option, true, false);
102: }
103:
104: private static void assertNextToken(OptionSpecTokenizer lexer,
105: char option, boolean acceptsArguments,
106: boolean requiresArgument) {
107:
108: assertTrue("no more tokens?", lexer.hasMore());
109: OptionSpec spec = lexer.next();
110: assertEquals("option?", String.valueOf(option), spec.option());
111: assertEquals("accepts args?", acceptsArguments, spec
112: .acceptsArguments());
113: assertEquals("requires arg?", requiresArgument, spec
114: .requiresArgument());
115: }
116: }
|