Skip to content

Commit

Permalink
added test superclass bean and child class bean where child class fie…
Browse files Browse the repository at this point in the history
…ld shadows super class

Closes #18
  • Loading branch information
remkop committed Feb 7, 2017
1 parent a861c98 commit 8a672fa
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/test/java/picocli/CommandLineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1231,4 +1231,48 @@ class TextParams {
TextParams opt = CommandLine.parse(new TextParams(), "\"a text\"", "\"another text\"", "\"x z\"");
assertArrayEquals(new String[]{"a text", "another text", "x z"}, opt.text);
}

@Test
public void testSubclassedOptions() {
class ParentOption {
@CommandLine.Option(names = "-p") String path;
}
class ChildOption extends ParentOption {
@CommandLine.Option(names = "-t") String text;
}
ChildOption opt = CommandLine.parse(new ChildOption(), "-p", "somePath", "-t", "\"a text\"");
assertEquals("somePath", opt.path);
assertEquals("a text", opt.text);
}

@Test
public void testSubclassedOptionsWithShadowedOptionNameThrowsDuplicateOptionAnnotationsException() {
class ParentOption {
@CommandLine.Option(names = "-p") String path;
}
class ChildOption extends ParentOption {
@CommandLine.Option(names = "-p") String text;
}
try {
CommandLine.parse(new ChildOption(), "");
fail("expected CommandLine$DuplicateOptionAnnotationsException");
} catch (DuplicateOptionAnnotationsException ex) {
assertEquals("Option name '-p' is used in both path and text", ex.getMessage());
}
}

@Test
public void testSubclassedOptionsWithShadowedFieldInitializesChildField() {
class ParentOption {
@CommandLine.Option(names = "-parentPath") String path;
}
class ChildOption extends ParentOption {
@CommandLine.Option(names = "-childPath") String path;
}
ChildOption opt = CommandLine.parse(new ChildOption(), "-childPath", "somePath");
assertEquals("somePath", opt.path);

opt = CommandLine.parse(new ChildOption(), "-parentPath", "somePath");
assertNull(opt.path);
}
}

0 comments on commit 8a672fa

Please sign in to comment.