Skip to content

Commit 94f50c0

Browse files
committed
More tests for the partial option matching
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@779605 13f79535-47bb-0310-9956-ffa450edef68
1 parent aa9f4de commit 94f50c0

4 files changed

Lines changed: 134 additions & 6 deletions

File tree

src/test/org/apache/commons/cli/BasicParserTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void testShortWithoutEqual() throws Exception
4444
// not supported by the BasicParser
4545
}
4646

47-
public void testLongWithEqual() throws Exception
47+
public void testLongWithEqualDoubleDash() throws Exception
4848
{
4949
// not supported by the BasicParser
5050
}
@@ -64,6 +64,16 @@ public void testUnambiguousPartialLongOption2() throws Exception
6464
// not supported by the BasicParser
6565
}
6666

67+
public void testUnambiguousPartialLongOption3() throws Exception
68+
{
69+
// not supported by the BasicParser
70+
}
71+
72+
public void testUnambiguousPartialLongOption4() throws Exception
73+
{
74+
// not supported by the BasicParser
75+
}
76+
6777
public void testAmbiguousPartialLongOption1() throws Exception
6878
{
6979
// not supported by the BasicParser
@@ -74,7 +84,17 @@ public void testAmbiguousPartialLongOption2() throws Exception
7484
// not supported by the BasicParser
7585
}
7686

77-
public void testPartialLongOptionWithShort() throws Exception
87+
public void testAmbiguousPartialLongOption3() throws Exception
88+
{
89+
// not supported by the BasicParser
90+
}
91+
92+
public void testAmbiguousPartialLongOption4() throws Exception
93+
{
94+
// not supported by the BasicParser
95+
}
96+
97+
public void testPartialLongOptionSingleDash() throws Exception
7898
{
7999
// not supported by the BasicParser
80100
}

src/test/org/apache/commons/cli/GnuParserTest.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public void testUnambiguousPartialLongOption2() throws Exception
3535
// not supported by the GnuParser
3636
}
3737

38+
public void testUnambiguousPartialLongOption3() throws Exception
39+
{
40+
// not supported by the GnuParser
41+
}
42+
43+
public void testUnambiguousPartialLongOption4() throws Exception
44+
{
45+
// not supported by the GnuParser
46+
}
47+
3848
public void testAmbiguousPartialLongOption1() throws Exception
3949
{
4050
// not supported by the GnuParser
@@ -45,7 +55,17 @@ public void testAmbiguousPartialLongOption2() throws Exception
4555
// not supported by the GnuParser
4656
}
4757

48-
public void testPartialLongOptionWithShort() throws Exception
58+
public void testAmbiguousPartialLongOption3() throws Exception
59+
{
60+
// not supported by the GnuParser
61+
}
62+
63+
public void testAmbiguousPartialLongOption4() throws Exception
64+
{
65+
// not supported by the GnuParser
66+
}
67+
68+
public void testPartialLongOptionSingleDash() throws Exception
4969
{
5070
// not supported by the GnuParser
5171
}

src/test/org/apache/commons/cli/ParserTestCase.java

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void testUnrecognizedOption() throws Exception
122122
{
123123
assertEquals("-d", e.getOption());
124124
}
125-
}
125+
}
126126

127127
public void testMissingArg() throws Exception
128128
{
@@ -259,7 +259,7 @@ public void testShortWithoutEqual() throws Exception
259259
assertEquals("bar", cl.getOptionValue("foo"));
260260
}
261261

262-
public void testLongWithEqual() throws Exception
262+
public void testLongWithEqualDoubleDash() throws Exception
263263
{
264264
String[] args = new String[] { "--foo=bar" };
265265

@@ -330,6 +330,34 @@ public void testUnambiguousPartialLongOption2() throws Exception
330330
assertTrue("Confirm --version is set", cl.hasOption("version"));
331331
}
332332

333+
public void testUnambiguousPartialLongOption3() throws Exception
334+
{
335+
String[] args = new String[] { "--ver=1" };
336+
337+
Options options = new Options();
338+
options.addOption(OptionBuilder.withLongOpt("verbose").hasOptionalArg().create());
339+
options.addOption(OptionBuilder.withLongOpt("help").create());
340+
341+
CommandLine cl = parser.parse(options, args);
342+
343+
assertTrue("Confirm --verbose is set", cl.hasOption("verbose"));
344+
assertEquals("1", cl.getOptionValue("verbose"));
345+
}
346+
347+
public void testUnambiguousPartialLongOption4() throws Exception
348+
{
349+
String[] args = new String[] { "-ver=1" };
350+
351+
Options options = new Options();
352+
options.addOption(OptionBuilder.withLongOpt("verbose").hasOptionalArg().create());
353+
options.addOption(OptionBuilder.withLongOpt("help").create());
354+
355+
CommandLine cl = parser.parse(options, args);
356+
357+
assertTrue("Confirm --verbose is set", cl.hasOption("verbose"));
358+
assertEquals("1", cl.getOptionValue("verbose"));
359+
}
360+
333361
public void testAmbiguousPartialLongOption1() throws Exception
334362
{
335363
String[] args = new String[] { "--ver" };
@@ -379,8 +407,58 @@ public void testAmbiguousPartialLongOption2() throws Exception
379407

380408
assertTrue( "Confirm MissingArgumentException caught", caught );
381409
}
410+
411+
public void testAmbiguousPartialLongOption3() throws Exception
412+
{
413+
String[] args = new String[] { "--ver=1" };
414+
415+
Options options = new Options();
416+
options.addOption(OptionBuilder.withLongOpt("version").create());
417+
options.addOption(OptionBuilder.withLongOpt("verbose").hasOptionalArg().create());
418+
419+
boolean caught = false;
420+
421+
try
422+
{
423+
parser.parse(options, args);
424+
}
425+
catch (AmbiguousOptionException e)
426+
{
427+
caught = true;
428+
assertEquals("Partial option", "--ver", e.getOption());
429+
assertNotNull("Matching options null", e.getMatchingOptions());
430+
assertEquals("Matching options size", 2, e.getMatchingOptions().size());
431+
}
432+
433+
assertTrue( "Confirm MissingArgumentException caught", caught );
434+
}
435+
436+
public void testAmbiguousPartialLongOption4() throws Exception
437+
{
438+
String[] args = new String[] { "-ver=1" };
439+
440+
Options options = new Options();
441+
options.addOption(OptionBuilder.withLongOpt("version").create());
442+
options.addOption(OptionBuilder.withLongOpt("verbose").hasOptionalArg().create());
443+
444+
boolean caught = false;
445+
446+
try
447+
{
448+
parser.parse(options, args);
449+
}
450+
catch (AmbiguousOptionException e)
451+
{
452+
caught = true;
453+
assertEquals("Partial option", "-ver", e.getOption());
454+
assertNotNull("Matching options null", e.getMatchingOptions());
455+
assertEquals("Matching options size", 2, e.getMatchingOptions().size());
456+
}
457+
458+
assertTrue( "Confirm MissingArgumentException caught", caught );
459+
}
382460

383-
public void testPartialLongOptionWithShort() throws Exception
461+
public void testPartialLongOptionSingleDash() throws Exception
384462
{
385463
String[] args = new String[] { "-ver" };
386464

src/test/org/apache/commons/cli/PosixParserTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,14 @@ public void testShortWithEqual() throws Exception
185185
{
186186
// not supported by the PosixParser
187187
}
188+
189+
public void testUnambiguousPartialLongOption4() throws Exception
190+
{
191+
// not supported by the PosixParser
192+
}
193+
194+
public void testAmbiguousPartialLongOption4() throws Exception
195+
{
196+
// not supported by the PosixParser
197+
}
188198
}

0 commit comments

Comments
 (0)