-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2270] Custom type converter for primitive
boolean
options should …
…not be ignored
- Loading branch information
Showing
4 changed files
with
83 additions
and
6 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
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,78 @@ | ||
package picocli; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import picocli.CommandLine.ITypeConverter; | ||
import picocli.CommandLine.Option; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
import static org.junit.Assert.*; | ||
|
||
public class Issue2270 { | ||
static class CmdObjectBoolean { | ||
@Option(names = "--test-Boolean", defaultValue = "dHJ1ZQ==" /* base64-encoded 'true' */) | ||
Boolean testBoolean; | ||
} | ||
static class CmdPrimitiveBoolean { | ||
@Option(names = "--test-boolean", defaultValue = "dHJ1ZQ==" /* base64-encoded 'true' */) | ||
boolean testboolean; | ||
} | ||
|
||
static class Base64BooleanTypeConverter implements ITypeConverter<Boolean> { | ||
|
||
static List<String> invocations = new ArrayList<>(); | ||
|
||
public Boolean convert(String value) { | ||
invocations.add(value); | ||
System.out.printf("converter invocation %s: called with value %s%n", | ||
invocations.size(), value); | ||
if (Boolean.parseBoolean(value)) { | ||
return true; | ||
} | ||
return Boolean.parseBoolean(new String(Base64.getDecoder().decode(value))); | ||
} | ||
} | ||
|
||
@Before | ||
public void beforeTests() { | ||
CommandLine.tracer().setLevel(CommandLine.TraceLevel.DEBUG); | ||
} | ||
|
||
@After | ||
public void afterTests() { | ||
CommandLine.tracer().setLevel(CommandLine.TraceLevel.WARN); | ||
} | ||
|
||
@Test | ||
public void testObjectBoolean() { | ||
Base64BooleanTypeConverter.invocations.clear(); | ||
CmdObjectBoolean cmd = new CmdObjectBoolean(); | ||
new CommandLine(cmd) | ||
.registerConverter(Boolean.class, new Base64BooleanTypeConverter()) | ||
.parseArgs(); | ||
assertThat(Base64BooleanTypeConverter.invocations.size(), greaterThanOrEqualTo(1)); | ||
assertEquals(Arrays.asList("dHJ1ZQ==", "true"), Base64BooleanTypeConverter.invocations); | ||
|
||
assertTrue(cmd.testBoolean); | ||
} | ||
|
||
@Test | ||
public void testPrimitiveBoolean() { | ||
Base64BooleanTypeConverter.invocations.clear(); | ||
CmdPrimitiveBoolean cmd = new CmdPrimitiveBoolean(); | ||
new CommandLine(cmd) | ||
.registerConverter(Boolean.TYPE, new Base64BooleanTypeConverter()) | ||
.parseArgs(); | ||
assertThat(Base64BooleanTypeConverter.invocations.size(), greaterThanOrEqualTo(1)); | ||
assertEquals(Arrays.asList("dHJ1ZQ==", "true"), Base64BooleanTypeConverter.invocations); | ||
|
||
assertTrue(cmd.testboolean); | ||
} | ||
} |
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