Skip to content

Commit

Permalink
#279 added tests; added release notes entry
Browse files Browse the repository at this point in the history
Closes #279
  • Loading branch information
remkop committed Feb 9, 2018
1 parent a4cdd1d commit 498d36d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ No features have been promoted in this picocli release.
- [#262] New Feature: new `showDefaultValue` attribute on `@Option` and `@Parameters` gives fine-grained control over which default values to show or hide. Thanks to [ymenager](https://github.com/ymenager) for the request.
- [#268] New Feature: new `helpCommand` attribute on `@Command`: if the command line arguments contain a subcommand annotated with `helpCommand`, the parser will not validate the required options or positional parameters of the parent command. Thanks to [ymenager](https://github.com/ymenager) for the request.
- [#277] New Feature: new `hidden` attribute on `@Command` to omit the specified subcommand from the usage help message command list of the parent command. Thanks to [pditommaso](https://github.com/pditommaso).
- [#279] Enhancement: assign empty String when String option was specified without value. Thanks to [pditommaso](https://github.com/pditommaso) for the request.

## <a name="3.0.0-alpha-1-deprecated"></a> Deprecations

Expand Down
24 changes: 18 additions & 6 deletions src/test/java/picocli/CommandLineArityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
import org.junit.Ignore;
import org.junit.Test;

import picocli.CommandLine.MissingParameterException;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.Range;
import picocli.CommandLine.UnmatchedArgumentException;
import picocli.CommandLine.*;

import static org.junit.Assert.*;
import static picocli.HelpTestUtil.setTraceLevel;
Expand Down Expand Up @@ -541,6 +537,22 @@ class App {
": java.lang.NumberFormatException: For input string: \"-boolean\"", ex.getMessage());
}
}
/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */
@Test
public void testSingleValueFieldWithOptionalParameter_279() {
@Command(name="sample")
class Sample {
@Option(names="--foo", arity="0..1") String foo;
}
Sample sample1 = CommandLine.populateCommand(new Sample()); // not specified
assertNull("optional option is null when option not specified", sample1.foo);

Sample sample2 = CommandLine.populateCommand(new Sample(), "--foo"); // no arguments
assertEquals("optional option is empty string when specified without args", "", sample2.foo);

Sample sample3 = CommandLine.populateCommand(new Sample(), "--foo", "value"); // no arguments
assertEquals("optional option has value when specified", "value", sample3.foo);
}

@Test
public void testIntOptionArity1_nConsumes1Argument() { // ignores varargs
Expand Down Expand Up @@ -863,7 +875,7 @@ class Arg {

@Test
public void test130MixPositionalParamsWithOptions() {
@CommandLine.Command(name = "test-command", description = "tests help from a command script")
@Command(name = "test-command", description = "tests help from a command script")
class Arg {

@Parameters(description = "some parameters")
Expand Down
32 changes: 25 additions & 7 deletions src/test/java/picocli/CommandLineModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,8 @@

import org.junit.Test;

import picocli.CommandLine.CommandSpec;
import picocli.CommandLine.*;
import picocli.CommandLine.Help.Ansi;
import picocli.CommandLine.ITypeConverter;
import picocli.CommandLine.InitializationException;
import picocli.CommandLine.OptionSpec;
import picocli.CommandLine.PositionalParamSpec;
import picocli.CommandLine.Range;
import picocli.CommandLine.UnmatchedArgumentException;

import static org.junit.Assert.*;
import static picocli.HelpTestUtil.setTraceLevel;
Expand Down Expand Up @@ -611,4 +605,28 @@ public void testOptionSpecRequiresAtLeastOneName() throws Exception {
public void testConversion() {
// TODO convertion with aux types (abstract field types, generic map with and without explicit type attribute etc)
}

/** see <a href="https://github.com/remkop/picocli/issues/279">issue #279</a> */
@Test
public void testSingleValueFieldWithOptionalParameter_279() {
@Command(name="sample")
class Sample {
@Option(names="--foo", arity="0..1") String foo;
}
List<CommandLine> parsed1 = new CommandLine(new Sample()).parse();// not specified
OptionSpec option1 = parsed1.get(0).getCommandSpec().optionsMap().get("--foo");
assertNull("optional option is null when option not specified", option1.getValue());
assertTrue("optional option has no raw string value when option not specified", option1.rawStringValues().isEmpty());

List<CommandLine> parsed2 = new CommandLine(new Sample()).parse("--foo");// specified without value
OptionSpec option2 = parsed2.get(0).getCommandSpec().optionsMap().get("--foo");
assertEquals("optional option is empty string when specified without args", "", option2.getValue());
assertEquals("optional option raw string value when specified without args", "", option2.rawStringValues().get(0));

List<CommandLine> parsed3 = new CommandLine(new Sample()).parse("--foo", "value");// specified with value
OptionSpec option3 = parsed3.get(0).getCommandSpec().optionsMap().get("--foo");
assertEquals("optional option is empty string when specified without args", "value", option3.getValue());
assertEquals("optional option raw string value when specified without args", "value", option3.rawStringValues().get(0));
}

}

0 comments on commit 498d36d

Please sign in to comment.