ShortOptionsOptionalArgumentTest.java :  » Development » JOpt-Simple » joptsimple » Java Open Source

Java Open Source » Development » JOpt Simple 
JOpt Simple » joptsimple » ShortOptionsOptionalArgumentTest.java
/*
 Copyright 2004-2007 Paul R. Holser, Jr.  All rights reserved.
 Licensed under the Academic Free License version 3.0
 */

package joptsimple;

import java.util.Collections;

/**
 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 * @version $Id: ShortOptionsOptionalArgumentTest.java,v 1.14 2007/04/10 20:06:27 pholser Exp $
 */
public class ShortOptionsOptionalArgumentTest extends AbstractOptionParserFixture {
    protected void setUp() throws Exception {
        super.setUp();

        parser.accepts( "f" ).withOptionalArg();
        parser.accepts( "g" ).withOptionalArg();
        parser.accepts( "bar" ).withOptionalArg();
    }

    public void testOptionWithOptionalArgumentNotPresent() {
        OptionSet options = parser.parse( new String[] { "-f" } );
        assertOptionDetected( options, "f" );
        assertEquals( Collections.EMPTY_LIST, options.argumentsOf( "f" ) );
        assertEquals( Collections.EMPTY_LIST, options.nonOptionArguments() );
    }

    public void testOptionWithOptionalArgumentPresent() {
        OptionSet options = parser.parse( new String[] { "-f", "bar" } );
        assertOptionDetected( options, "f" );
        assertEquals( Collections.singletonList( "bar" ), options.argumentsOf( "f" ) );
        assertEquals( Collections.EMPTY_LIST, options.nonOptionArguments() );
    }

    public void testOptionWithOptionalArgumentThatLooksLikeAnInvalidOption() {
        try {
            parser.parse( new String[] { "-f", "--biz" } );
            fail();
        }
        catch ( UnrecognizedOptionException expected ) {
            assertEquals( "biz", expected.option() );
        }
    }

    public void testOptionWithOptionalArgumentThatLooksLikeAValidOption() {
        OptionSet options = parser.parse( new String[] { "-f", "--bar" } );
        assertOptionDetected( options, "f" );
        assertOptionDetected( options, "bar" );
        assertEquals( Collections.EMPTY_LIST, options.argumentsOf( "f" ) );
        assertEquals( Collections.EMPTY_LIST, options.argumentsOf( "bar" ) );
        assertEquals( Collections.EMPTY_LIST, options.nonOptionArguments() );
    }

    public void testOptionWithOptionalArgumentFollowedByLegalOption() {
        OptionSet options = parser.parse( new String[] { "-f", "-g" } );
        assertOptionDetected( options, "f" );
        assertOptionDetected( options, "g" );
        assertEquals( Collections.EMPTY_LIST, options.argumentsOf( "f" ) );
        assertEquals( Collections.EMPTY_LIST, options.argumentsOf( "g" ) );
        assertEquals( Collections.EMPTY_LIST, options.nonOptionArguments() );
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.