Skip to content

Commit

Permalink
Migrate some unit tests to Junit Jupiter
Browse files Browse the repository at this point in the history
I really like `assertLinesMatch()` for long text output.
  • Loading branch information
jeantessier committed Jan 8, 2025
1 parent 88b820d commit 5d836ee
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 386 deletions.
17 changes: 6 additions & 11 deletions lib/src/test/java/com/jeantessier/commandline/TestAliasSwitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void testIsPresentWithNoSwitches() {
}

@Test
void testParseNull() throws CommandLineException {
void testParseNull() {
aliasSwitch = new AliasSwitch(SWITCH_NAME, switch2);
int step = aliasSwitch.parse(null);
assertEquals(1, step, "step");
Expand All @@ -95,32 +95,27 @@ void testParseNull() throws CommandLineException {
}

@Test
void testParseNullWithError() throws CommandLineException {
try {
aliasSwitch.parse(null);
fail("Alias with SingleValueSwitch parsed null");
} catch (CommandLineException e) {
// Expected
}
void testParseNullWithError() {
assertThrows(CommandLineException.class, () -> aliasSwitch.parse(null));
}

@Test
void testParseValue() throws CommandLineException {
void testParseValue() {
int step = aliasSwitch.parse(SWITCH_VALUE);
assertEquals(2, step, "step");
assertEquals(SWITCH_VALUE, switch1.getValue(), "Switch1 not new value");
assertEquals(SWITCH_VALUE, switch2.getValue(), "Switch2 not new value");
}

@Test
void testParseNullWithNoSwitches() throws CommandLineException {
void testParseNullWithNoSwitches() {
aliasSwitch = new AliasSwitch(SWITCH_NAME);
int step = aliasSwitch.parse(null);
assertEquals(1, step, "step");
}

@Test
void testParseValueWithNoSwitches() throws CommandLineException {
void testParseValueWithNoSwitches() {
aliasSwitch = new AliasSwitch(SWITCH_NAME);
int step = aliasSwitch.parse(SWITCH_VALUE);
assertEquals(1, step, "step");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,28 @@

package com.jeantessier.commandline;

import junit.framework.*;
import org.junit.jupiter.api.*;

public class TestAtLeastParameterStrategy extends TestCase {
private ParameterStrategy strategy;
import static org.junit.jupiter.api.Assertions.*;

protected void setUp() throws Exception {
super.setUp();
public class TestAtLeastParameterStrategy {
private final ParameterStrategy strategy = new AtLeastParameterStrategy(2);

strategy = new AtLeastParameterStrategy(2);
}

public void testAccept() throws CommandLineException {
@Test
void testAccept() {
assertEquals(1, strategy.accept("value"));
}

public void testValidate() throws CommandLineException {
try {
strategy.validate();
fail("Strategy accepted too many values");
} catch (CommandLineException e) {
// Expected
}
@Test
void testValidate() {
assertThrows(CommandLineException.class, strategy::validate);

strategy.accept("value1");
try {
strategy.validate();
fail("Strategy accepted too many values");
} catch (CommandLineException e) {
// Expected
}
assertThrows(CommandLineException.class, strategy::validate);

strategy.accept("value2");
strategy.validate();

strategy.accept("value3");
strategy.validate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,27 @@

package com.jeantessier.commandline;

import junit.framework.*;
import org.junit.jupiter.api.*;

public class TestAtMostParameterStrategy extends TestCase {
private ParameterStrategy strategy;
import static org.junit.jupiter.api.Assertions.*;

protected void setUp() throws Exception {
super.setUp();
public class TestAtMostParameterStrategy {
private final ParameterStrategy strategy = new AtMostParameterStrategy(1);

strategy = new AtMostParameterStrategy(1);
}

public void testAccept() throws CommandLineException {
@Test
void testAccept() {
assertEquals(1, strategy.accept("value"));
}

public void testOverload() throws CommandLineException {
@Test
void testOverload() {
strategy.accept("value1");
try {
strategy.accept("value2");
fail("Strategy accepted too many values");
} catch (CommandLineException ex) {
// Expected
}

assertThrows(CommandLineException.class, () -> strategy.accept("value2"));
}

public void testValidate() throws CommandLineException {
@Test
void testValidate() {
strategy.validate();
strategy.accept("value1");
strategy.validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,25 @@

package com.jeantessier.commandline;

import junit.framework.*;
import org.junit.jupiter.api.*;

public class TestCollectingParameterStrategy extends TestCase {
private ParameterStrategy strategy;
import static org.junit.jupiter.api.Assertions.*;

protected void setUp() throws Exception {
super.setUp();
public class TestCollectingParameterStrategy {
private final ParameterStrategy strategy = new CollectingParameterStrategy();

strategy = new CollectingParameterStrategy();
}

public void testAccept() throws CommandLineException {
@Test
void testAccept() {
String value1 = "value1";
String value2 = "value2";
assertEquals(1, strategy.accept(value1));
assertEquals(1, strategy.accept(value2));
assertSame("Returned different value", value1, strategy.getParameters().get(0));
assertSame("Returned different value", value2, strategy.getParameters().get(1));
assertSame(value1, strategy.getParameters().get(0), "Returned different value");
assertSame(value2, strategy.getParameters().get(1), "Returned different value");
}

public void testValidate() throws CommandLineException {
@Test
void testValidate() {
strategy.validate();
strategy.accept("value");
strategy.validate();
Expand Down
84 changes: 42 additions & 42 deletions lib/src/test/java/com/jeantessier/commandline/TestCommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,92 +32,92 @@

package com.jeantessier.commandline;

import junit.framework.*;
import org.junit.jupiter.api.*;

public class TestCommandLine extends TestCase {
import static org.junit.jupiter.api.Assertions.*;

public class TestCommandLine {
private static final String SWITCH_NAME = "switch";
private static final String SWITCH_DEFAULT_VALUE = "default value";
private static final String SWITCH_VALUE = "value";

private CommandLine commandLine;

protected void setUp() throws Exception {
super.setUp();

commandLine = new CommandLine();
}
private final CommandLine commandLine = new CommandLine();

public void testAddToggleSwitch() {
@Test
void testAddToggleSwitch() {
CommandLineSwitch cls = commandLine.addToggleSwitch(SWITCH_NAME);
assertTrue("Wrong type", cls instanceof ToggleSwitch);
assertEquals("name", SWITCH_NAME, cls.getName());
assertFalse("is present", cls.isPresent());
assertInstanceOf(ToggleSwitch.class, cls, "Wrong type");
assertEquals(SWITCH_NAME, cls.getName(), "name");
assertFalse(cls.isPresent(), "is present");
}

public void addAliasSwitch() throws CommandLineException {
@Test
void addAliasSwitch() {
ToggleSwitch toggle1 = commandLine.addToggleSwitch("toggle1");
ToggleSwitch toggle2 = commandLine.addToggleSwitch("toggle2");

AliasSwitch aliasSwitch = commandLine.addAliasSwitch(SWITCH_NAME, "toogle1", "toggle2");
assertEquals("Nb switches", 2, aliasSwitch.getSwitches().size());
assertTrue("Missing toggle1", aliasSwitch.getSwitches().contains(toggle1));
assertTrue("Missing toggle2", aliasSwitch.getSwitches().contains(toggle2));
AliasSwitch aliasSwitch = commandLine.addAliasSwitch(SWITCH_NAME, "toggle1", "toggle2");
assertEquals(2, aliasSwitch.getSwitches().size(), "Nb switches");
assertTrue(aliasSwitch.getSwitches().contains(toggle1), "Missing toggle1");
assertTrue(aliasSwitch.getSwitches().contains(toggle2), "Missing toggle2");
}

public void testAddAliasForNonExistingSwitch() {
try {
commandLine.addAliasSwitch(SWITCH_NAME, "foobar");
fail("Added alias to non-existing switch");
} catch (IllegalArgumentException e) {
// Expected
}
@Test
void testAddAliasForNonExistingSwitch() {
assertThrows(IllegalArgumentException.class, () -> commandLine.addAliasSwitch(SWITCH_NAME, "foobar"));
}

public void testParseToggleSwitchTwice() throws CommandLineException {
@Test
void testParseToggleSwitchTwice() {
commandLine.addToggleSwitch(SWITCH_NAME);
String cls = "-" + SWITCH_NAME;
commandLine.parse(new String[] {cls, cls});
assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
assertTrue(commandLine.isPresent(SWITCH_NAME), "Missing switch");
}

public void testParseSingleValueSwitchTwice() throws CommandLineException {
@Test
void testParseSingleValueSwitchTwice() {
commandLine.addSingleValueSwitch(SWITCH_NAME);
String cls = "-" + SWITCH_NAME;
commandLine.parse(new String[] {cls, SWITCH_VALUE, cls, SWITCH_VALUE});
assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
assertEquals("Single value switch value", SWITCH_VALUE, commandLine.getSingleSwitch(SWITCH_NAME));
assertTrue(commandLine.isPresent(SWITCH_NAME), "Missing switch");
assertEquals(SWITCH_VALUE, commandLine.getSingleSwitch(SWITCH_NAME), "Single value switch value");
}

public void testParseOptionalValueSwitchTwice() throws CommandLineException {
@Test
void testParseOptionalValueSwitchTwice() {
commandLine.addOptionalValueSwitch(SWITCH_NAME, SWITCH_DEFAULT_VALUE);
String cls = "-" + SWITCH_NAME;
commandLine.parse(new String[] {cls, SWITCH_VALUE, cls});
assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
assertEquals("Optional value switch value", SWITCH_DEFAULT_VALUE, commandLine.getOptionalSwitch(SWITCH_NAME));
assertTrue(commandLine.isPresent(SWITCH_NAME), "Missing switch");
assertEquals(SWITCH_DEFAULT_VALUE, commandLine.getOptionalSwitch(SWITCH_NAME), "Optional value switch value");
}

public void testParseMultipleValueSwitchTwice() throws CommandLineException {
@Test
void testParseMultipleValueSwitchTwice() {
commandLine.addMultipleValuesSwitch(SWITCH_NAME, SWITCH_DEFAULT_VALUE);
String cls = "-" + SWITCH_NAME;
commandLine.parse(new String[] {cls, SWITCH_VALUE, cls, SWITCH_VALUE});
assertTrue("Missing switch", commandLine.isPresent(SWITCH_NAME));
assertEquals("Multiple value switch value size", 2, commandLine.getMultipleSwitch(SWITCH_NAME).size());
assertEquals("Multiple value switch value 0", SWITCH_VALUE, commandLine.getMultipleSwitch(SWITCH_NAME).get(0));
assertEquals("Multiple value switch value 1", SWITCH_VALUE, commandLine.getMultipleSwitch(SWITCH_NAME).get(1));
assertTrue(commandLine.isPresent(SWITCH_NAME), "Missing switch");
assertEquals(2, commandLine.getMultipleSwitch(SWITCH_NAME).size(), "Multiple value switch value size");
assertEquals(SWITCH_VALUE, commandLine.getMultipleSwitch(SWITCH_NAME).get(0), "Multiple value switch value 0");
assertEquals(SWITCH_VALUE, commandLine.getMultipleSwitch(SWITCH_NAME).get(1), "Multiple value switch value 1");
}

public void testParseAliasSwitch() throws CommandLineException {
@Test
void testParseAliasSwitch() {
commandLine.addToggleSwitch("toggle1");
commandLine.addToggleSwitch("toggle2");
commandLine.addAliasSwitch(SWITCH_NAME, "toggle1", "toggle2");

String cls = "-" + SWITCH_NAME;
commandLine.parse(new String[] {cls});
assertTrue("Missing toggle1", commandLine.isPresent("toggle1"));
assertTrue("Missing toggle2", commandLine.isPresent("toggle2"));
assertTrue(commandLine.isPresent("toggle1"), "Missing toggle1");
assertTrue(commandLine.isPresent("toggle2"), "Missing toggle2");
}

public void testParseMissingValueFollowedBySwitch() throws CommandLineException {
@Test
void testParseMissingValueFollowedBySwitch() {
commandLine.addSingleValueSwitch(SWITCH_NAME);

String cls = "-" + SWITCH_NAME;
Expand Down
Loading

0 comments on commit 5d836ee

Please sign in to comment.