diff options
author | Luis Fernando Pino Duque <lpino@google.com> | 2016-02-24 13:43:37 +0000 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2016-02-24 18:01:11 +0000 |
commit | dafdc2854bc74bb8bf09abe9932160dd4a5bb1fb (patch) | |
tree | 676f24dddaee8e68592e6630be29b739c4a8b55c /src/test/java | |
parent | d6347a971e75eda3fb5029cd8beba67464c7af61 (diff) |
Refactoring of the OptionsParser implementation to allow overriding the value of an option with null.
Currently it returns the original default value if the new value is null.
--
MOS_MIGRATED_REVID=115442253
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/com/google/devtools/common/options/OptionsParserTest.java | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java index 659f77f690..0448471a2f 100644 --- a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java +++ b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java @@ -86,6 +86,16 @@ public class OptionsParserTest { public String baz; } + /** + * Example with empty to null string converter + */ + public static class ExampleBoom extends OptionsBase { + @Option(name = "boom", + defaultValue = "defaultBoom", + converter = EmptyToNullStringConverter.class) + public String boom; + } + public static class StringConverter implements Converter<String> { @Override public String convert(String input) { @@ -97,6 +107,16 @@ public class OptionsParserTest { } } + /** + * A converter that defaults to null if the input is the empty string + */ + public static class EmptyToNullStringConverter extends StringConverter { + @Override + public String convert(String input) { + return input.isEmpty() ? null : input; + } + } + @Test public void parseWithMultipleOptionsInterfaces() throws OptionsParsingException { @@ -170,6 +190,17 @@ public class OptionsParserTest { } } + @Test + public void parseAndOverrideWithEmptyStringToObtainNullValueInOption() + throws OptionsParsingException { + OptionsParser parser = newOptionsParser(ExampleBoom.class); + // Override --boom value to the empty string + parser.parse("--boom="); + ExampleBoom boom = parser.getOptions(ExampleBoom.class); + // The converted value is intentionally null since boom uses the EmptyToNullStringConverter + assertNull(boom.boom); + } + public static class CategoryTest extends OptionsBase { @Option(name = "swiss_bank_account_number", category = "undocumented", // Not printed in usage messages! @@ -1029,11 +1060,11 @@ public class OptionsParserTest { assertEquals(100, result.longval); } - public static class OldNameExample extends OptionsBase { + public static class OldNameExample extends OptionsBase { @Option(name = "new_name", oldName = "old_name", defaultValue = "defaultValue") - public String flag; + public String flag; } @Test @@ -1057,13 +1088,13 @@ public class OptionsParserTest { assertEquals( Arrays.asList("--new_name=foo"), canonicalize(OldNameExample.class, "--old_name=foo")); } - - public static class OldNameConflictExample extends OptionsBase { + + public static class OldNameConflictExample extends OptionsBase { @Option(name = "new_name", oldName = "old_name", defaultValue = "defaultValue") - public String flag1; - + public String flag1; + @Option(name = "old_name", defaultValue = "defaultValue") public String flag2; @@ -1078,8 +1109,8 @@ public class OptionsParserTest { // expected } } - - public static class WrapperOptionExample extends OptionsBase { + + public static class WrapperOptionExample extends OptionsBase { @Option(name = "wrapper", defaultValue = "null", wrapperOption = true) @@ -1094,7 +1125,7 @@ public class OptionsParserTest { @Option(name = "flag3", defaultValue = "foo") public String flag3; } - + @Test public void testWrapperOption() throws OptionsParsingException { OptionsParser parser = newOptionsParser(WrapperOptionExample.class); |