Skip to content

Commit

Permalink
#321 added unit tests for subcommand propagation of the default value…
Browse files Browse the repository at this point in the history
… provider
  • Loading branch information
NicolasMassart committed Aug 31, 2018
1 parent 08bf1ed commit f41ffae
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/test/java/picocli/CommandLineDefaultProviderTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package picocli;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -40,10 +41,30 @@ static class App {
void setString(String val) { stringForSetterDefault = val; }
}

@Command(name = "sub")
static class Sub {
@Option(names = "-a")
private String optionStringFieldWithoutDefaultNorInitialValue;
@Option(names = "-b", defaultValue = "Annotated default value")
private String optionStringFieldWithAnnotatedDefault;
@Option(names = "-c")
private String optionStringFieldWithInitDefault = "Initial default value";

@Parameters(arity = "0..1")
private String paramStringFieldWithoutDefaultNorInitialValue;
@Parameters(arity = "0..1", defaultValue = "Annotated default value")
private String paramStringFieldWithAnnotatedDefault;
@Parameters(arity = "0..1")
private String paramStringFieldWithInitDefault = "Initial default value";

private String stringForSetterDefault;
@Option(names = "-d", defaultValue = "Annotated setter default value")
void setString(String val) { stringForSetterDefault = val; }
}


@Test
public void testCommandDefaultProviderByAnnotation() {

CommandLine cmd = new CommandLine(App.class);
cmd.parse();

Expand All @@ -60,12 +81,19 @@ public void testCommandDefaultProviderByAnnotation() {
assertEquals("Initial default value",app.paramStringFieldWithInitDefault);

assertEquals("Annotated setter default value",app.stringForSetterDefault);

}

static class AppWithoutAnnotation {
@Option(names = "-a")
private String stringFieldWithoutDefaultNorInitialValue;
@Test
public void testDefaultProviderPropagatedToSubCommand() {
CommandLine cmd = new CommandLine(App.class);

cmd.addSubcommand("sub", new CommandLine(Sub.class));

CommandLine subCommandLine = cmd.getSubcommands().get("sub");
cmd.setDefaultValueProvider(new TestDefaultProvider());

assertNotNull(subCommandLine.getCommandSpec().defaultValueProvider());
assertEquals(TestDefaultProvider.class, subCommandLine.getCommandSpec().defaultValueProvider().getClass());
}

@Test
Expand Down

0 comments on commit f41ffae

Please sign in to comment.