Skip to content

Commit

Permalink
Port to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Oct 19, 2024
1 parent 7fabac6 commit ae92da8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<!-- FIX -->
<action type="add" issue="CLI-339" dev="ggregory" due-to="Gary Gregory">Deprecate CommandLine.Builder() in favor of CommandLine.builder().</action>
<action type="add" issue="CLI-339" dev="ggregory" due-to="Gary Gregory">Deprecate DeprecatedAttributes.Builder() in favor of DeprecatedAttributes.builder().</action>
<action type="add" issue="CLI-339" dev="ggregory" due-to="Dávid Szigecsán">Refactor default parser test #294.</action>
<action type="add" issue="CLI-339" dev="ggregory" due-to="Dávid Szigecsán">Refactor default parser test #294.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Port to JUnit 5.</action>
<!-- ADD -->
<action type="add" issue="CLI-339" dev="ggregory" due-to="Claude Warren, Gary Gregory">Help formatter extension in the new package #314.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">CommandLine.Builder implements Supplier&lt;CommandLine&gt;.</action>
Expand Down
22 changes: 4 additions & 18 deletions src/test/java/org/apache/commons/cli/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -241,10 +240,8 @@ public void testHelpOptions() {
@Test
public void testLong() {
final Options options = new Options();

options.addOption("a", "--a", false, "toggle -a");
options.addOption("b", "--b", true, "set -b");

assertTrue(options.hasOption("a"));
assertTrue(options.hasOption("b"));
}
Expand All @@ -254,12 +251,8 @@ public void testMissingOptionException() throws ParseException {
final Options options = new Options();
OptionBuilder.isRequired();
options.addOption(OptionBuilder.create("f"));
try {
new PosixParser().parse(options, new String[0]);
fail("Expected MissingOptionException to be thrown");
} catch (final MissingOptionException e) {
assertEquals("Missing required option: f", e.getMessage());
}
final MissingOptionException e = assertThrows(MissingOptionException.class, () -> new PosixParser().parse(options, new String[0]));
assertEquals("Missing required option: f", e.getMessage());
}

@Test
Expand All @@ -269,21 +262,15 @@ public void testMissingOptionsException() throws ParseException {
options.addOption(OptionBuilder.create("f"));
OptionBuilder.isRequired();
options.addOption(OptionBuilder.create("x"));
try {
new PosixParser().parse(options, new String[0]);
fail("Expected MissingOptionException to be thrown");
} catch (final MissingOptionException e) {
assertEquals("Missing required options: f, x", e.getMessage());
}
final MissingOptionException e = assertThrows(MissingOptionException.class, () -> new PosixParser().parse(options, new String[0]));
assertEquals("Missing required options: f, x", e.getMessage());
}

@Test
public void testSimple() {
final Options options = new Options();

options.addOption("a", false, "toggle -a");
options.addOption("b", true, "toggle -b");

assertTrue(options.hasOption("a"));
assertTrue(options.hasOption("b"));
}
Expand All @@ -293,7 +280,6 @@ public void testToString() {
final Options options = new Options();
options.addOption("f", "foo", true, "Foo");
options.addOption("b", "bar", false, "Bar");

final String s = options.toString();
assertNotNull(s, "null string returned");
assertTrue(s.toLowerCase().contains("foo"), "foo option missing");
Expand Down

0 comments on commit ae92da8

Please sign in to comment.