-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2058] Bugfix: superfluous defaultValue for options with IParameterC…
…onsumer * fix: `defaultValue` should not be applied in addition to user-specified value for options with a custom `IParameterConsumer` * added test * update RELEASE-NOTES.md Closes #2058
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/test/java/picocli/Issue2058DefaultValForParamConsumer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package picocli; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Stack; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import picocli.CommandLine.IParameterConsumer; | ||
import picocli.CommandLine.ITypeConverter; | ||
import picocli.CommandLine.Model.ArgSpec; | ||
import picocli.CommandLine.Model.CommandSpec; | ||
import picocli.CommandLine.Option; | ||
|
||
public class Issue2058DefaultValForParamConsumer { | ||
|
||
// Toy custom class | ||
static class CustomType { | ||
private final String lowerCase; | ||
|
||
public CustomType(String value){ | ||
lowerCase = value.toLowerCase(Locale.ENGLISH); | ||
} | ||
|
||
// Check equals in the test-case | ||
public String toString(){ | ||
return lowerCase; | ||
} | ||
} | ||
|
||
// Custom converter version | ||
static class MyTypeConverter implements ITypeConverter<CustomType> { | ||
public CustomType convert(String value) { | ||
return new CustomType(value); | ||
} | ||
} | ||
|
||
static class CmdWithTypeConverter { | ||
|
||
@Option(names = {"-f"}, | ||
defaultValue = "defaultArg", | ||
converter = MyTypeConverter.class, | ||
split = "\\s") | ||
public List<CustomType> arguments = new ArrayList<CustomType>(); | ||
} | ||
|
||
// Consumer version | ||
static class MyParamConsumer implements IParameterConsumer { | ||
|
||
public void consumeParameters(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) { | ||
List<CustomType> list = argSpec.getValue(); | ||
|
||
// Simple example, all remaining input are of this type | ||
while (!args.empty()){ | ||
list.add(new CustomType(args.pop())); | ||
} | ||
} | ||
} | ||
|
||
static class CmdWithParamConsumer { | ||
|
||
@Option(names = {"-f"}, | ||
defaultValue = "defaultArg", | ||
parameterConsumer = MyParamConsumer.class, | ||
split = "\\s") | ||
public List<CustomType> arguments = new ArrayList<CustomType>(); | ||
} | ||
|
||
@Test | ||
public void testDefaultValueUsedInCmdWithTypeConverter() { | ||
// Converter implementation - no argument given | ||
CmdWithTypeConverter program = new CmdWithTypeConverter(); | ||
new CommandLine(program).parseArgs(); | ||
Assert.assertEquals(1, program.arguments.size()); | ||
Assert.assertEquals("defaultarg", program.arguments.get(0).toString()); | ||
} | ||
|
||
@Test | ||
public void testSpecifiedValueUsedInCmdWithTypeConverter() { | ||
// Converter implementation - when giving an expecit argument (works as well) | ||
CmdWithTypeConverter program = new CmdWithTypeConverter(); | ||
new CommandLine(program).parseArgs("-f", "explicitArg"); | ||
Assert.assertEquals(1, program.arguments.size()); | ||
Assert.assertEquals("explicitarg",program.arguments.get(0).toString()); | ||
} | ||
|
||
@Test | ||
public void testDefaultValueUsedInCmdWithParamConsumer() { | ||
// Test consumer - no argument | ||
CmdWithParamConsumer cmd = new CmdWithParamConsumer(); | ||
new CommandLine(cmd).parseArgs(); | ||
Assert.assertEquals(1, cmd.arguments.size()); | ||
Assert.assertEquals("defaultarg",cmd.arguments.get(0).toString()); | ||
} | ||
|
||
@Test | ||
public void testOnlySpecifiedValueUsedInCmdWithParamConsumer() { | ||
// Consumer with explicit arg | ||
CmdWithParamConsumer cmd = new CmdWithParamConsumer(); | ||
new CommandLine(cmd).parseArgs("-f", "explicitArgument"); | ||
Assert.assertEquals(1, cmd.arguments.size()); // fails, cmd.arguments.size == 2 (explicit argument and "defaultarg") | ||
Assert.assertEquals("explicitargument", cmd.arguments.get(0).toString()); | ||
} | ||
} |