Skip to content

Commit

Permalink
Fixes remkop#2035 - include mapFallbackValue for inherited opts
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Ziemba committed Jun 1, 2023
1 parent 7c7c8b9 commit 3d5cf22
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/picocli/CommandLine.java
Expand Up @@ -9526,6 +9526,7 @@ abstract static class Builder<T extends Builder<T>> {
setter = original.setter;
scope = original.scope;
scopeType = original.scopeType;
mapFallbackValue = original.mapFallbackValue;
originalDefaultValue = original.originalDefaultValue;
originalMapFallbackValue = original.originalMapFallbackValue;
}
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/picocli/MapOptionsTest.java
Expand Up @@ -5,13 +5,15 @@
import org.junit.contrib.java.lang.system.ProvideSystemProperty;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.rules.TestRule;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.Parameters;

import java.util.Map;

import static org.junit.Assert.*;
import static picocli.CommandLine.ScopeType.INHERIT;

/**
* Verifies https://github.com/remkop/picocli/issues/1214
Expand Down Expand Up @@ -75,6 +77,40 @@ class App {
assertEquals(null, app.map.get("key"));
}

@Test
public void testInheritedOptionMapFallbackValueEmptyString() {
class App {
@Option(names = "-D", mapFallbackValue = "", scope = INHERIT) Map<String, String> map;
}
@Command(name = "sub")
class Sub {
}
App app = new App();
Sub sub = new Sub();
new CommandLine(app)
.addSubcommand(sub)
.parseArgs("sub", "-Dkey");
assertEquals(1, app.map.size());
assertEquals("", app.map.get("key"));
}

@Test
public void testInheritedOptionMapFallbackValueNull() {
class App {
@Option(names = "-D", mapFallbackValue = Option.NULL_VALUE, scope = INHERIT) Map<String, String> map;
}
@Command(name = "sub")
class Sub {
}
App app = new App();
Sub sub = new Sub();
new CommandLine(app)
.addSubcommand(sub)
.parseArgs("sub", "-Dkey");
assertEquals(1, app.map.size());
assertEquals(null, app.map.get("key"));
}

@Test
public void testPositionalMapFallbackValueNull() {
class App {
Expand All @@ -85,6 +121,23 @@ class App {
assertEquals(null, app.map.get("key"));
}

@Test
public void testInheritedPositionalMapFallbackValueNull() {
class App {
@Parameters(mapFallbackValue = Option.NULL_VALUE, scope = INHERIT) Map<String, String> map;
}
@Command(name = "sub")
class Sub {
}
App app = new App();
Sub sub = new Sub();
new CommandLine(app)
.addSubcommand(sub)
.parseArgs("sub", "key");
assertEquals(1, app.map.size());
assertEquals(null, app.map.get("key"));
}

@Test
public void testMapFallbackValueEmptyStringMultiple() {
class App {
Expand Down

0 comments on commit 3d5cf22

Please sign in to comment.