From: Otmar H. <otm...@us...> - 2006-02-18 22:30:27
|
Update of /cvsroot/jython/installer/test/java/org/apache/commons/cli In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18776/test/java/org/apache/commons/cli Added Files: PosixParserTest.java Log Message: fixed the flatten() method to handle short and long options the same way added tests for these cases --- NEW FILE: PosixParserTest.java --- package org.apache.commons.cli; import junit.framework.TestCase; public class PosixParserTest extends TestCase { private static final String TEST_SHORT = "t"; private static final String TEST_LONG = "test"; private static final String TEST_DESC = "test option"; private static final String TEST_SHORT_OPTION = "-t"; private static final String TEST_LONG_OPTION = "--test"; private static final String ARGUMENT = "argument"; private Options _options; private Parser _parser; protected void setUp() { _parser = new PosixParser(); _options = new Options(); Option testOption = new Option(TEST_SHORT, TEST_LONG, false, TEST_DESC); _options.addOption(testOption); } /** * test that an unknown single option and a double hyphen option (with or without argument) are treated the same */ public void testFlattenStop() { boolean stopAtNonOption = true; // means unallowed tokens should not be added String[] args; String[] expectedFlattened; // unknown single dash option args = new String[] { "-u" }; expectedFlattened = new String[0]; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "-u", TEST_SHORT_OPTION }; expectedFlattened = new String[] { TEST_SHORT_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); // unknown double dash option args = new String[] { "--unknown" }; expectedFlattened = new String[0]; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "--unknown", TEST_LONG_OPTION }; expectedFlattened = new String[] { TEST_LONG_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); // unknown double dash option with argument after = args = new String[] { "--unknown=" + ARGUMENT }; expectedFlattened = new String[0]; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "--unknown="+ARGUMENT, TEST_LONG_OPTION }; expectedFlattened = new String[] { TEST_LONG_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); // unknown double dash option with argument after ' ' args = new String[] { "--unknown", ARGUMENT }; expectedFlattened = new String[] { ARGUMENT }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "--unknown", ARGUMENT, TEST_LONG_OPTION }; expectedFlattened = new String[] { ARGUMENT, TEST_LONG_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); } /** * test that an unknown single option and a double hyphen option (with or without argument) are treated the same */ public void testFlattenNoStop() { boolean stopAtNonOption = false; // means every token should be added String[] args; String[] expectedFlattened; // unknown single dash option args = new String[] { "-u" }; expectedFlattened = new String[] { "-u" }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "-u", TEST_SHORT_OPTION }; expectedFlattened = new String[] { "-u", TEST_SHORT_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); // unknown double dash option args = new String[] { "--unknown" }; expectedFlattened = new String[] { "--unknown" }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "--unknown", TEST_LONG_OPTION }; expectedFlattened = new String[] { "--unknown", TEST_LONG_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); // unknown double dash option with argument after = args = new String[] { "--unknown=" + ARGUMENT }; expectedFlattened = new String[] { "--unknown", ARGUMENT }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "--unknown="+ ARGUMENT, TEST_LONG_OPTION }; expectedFlattened = new String[] { "--unknown", ARGUMENT, TEST_LONG_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); // unknown double dash option with argument after ' ' args = new String[] { "--unknown", ARGUMENT }; expectedFlattened = new String[] { "--unknown", ARGUMENT }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); args = new String[] { "--unknown", ARGUMENT, TEST_LONG_OPTION }; expectedFlattened = new String[] { "--unknown", ARGUMENT, TEST_LONG_OPTION }; assertEquals(expectedFlattened, _parser.flatten(_options, args, stopAtNonOption)); } // // private stuff // /** * Assert that the content of the specified object arrays is equal */ private void assertEquals(Object[] correct, Object[] tested) { assertEquals("different array lengths:", correct.length, tested.length); for (int i = 0; i < correct.length; i++) { assertEquals("position " + i + " of array differs", correct[i], tested[i]); } } } |