MCPcopy
hub / github.com/remkop/picocli / SubcommandTests

Class SubcommandTests

src/test/java/picocli/SubcommandTests.java:48–3054  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46import static picocli.TestUtil.setTraceLevel;
47
48public class SubcommandTests {
49 @Rule
50 public final SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();
51
52 @Rule
53 public final SystemOutRule systemOutRule = new SystemOutRule().enableLog().muteForSuccessfulTests();
54
55 // allows tests to set any kind of properties they like, without having to individually roll them back
56 @Rule
57 public final TestRule restoreSystemProperties = new RestoreSystemProperties();
58
59 @Rule
60 public final ProvideSystemProperty ansiOFF = new ProvideSystemProperty("picocli.ansi", "false");
61
62 @Command(name = "top")
63 static class MainCommand { @Option(names = "-a") boolean a; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
64 static class ChildCommand1 { @Option(names = "-b") boolean b; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
65 static class ChildCommand2 { @Option(names = "-c") boolean c; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
66 static class GrandChild1Command1 { @Option(names = "-d") boolean d; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
67 static class GrandChild1Command2 { @Option(names = "-e") CustomType e; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
68 static class GrandChild2Command1 { @Option(names = "-f") boolean f; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
69 static class GrandChild2Command2 { @Option(names = "-g") boolean g; public boolean equals(Object o) { return getClass().equals(o.getClass()); }}
70 static class GreatGrandChild2Command2_1 {
71 @Option(names = "-h") boolean h;
72 @Option(names = {"-t", "--type"}) CustomType customType;
73 public boolean equals(Object o) { return getClass().equals(o.getClass()); }
74 }
75
76 static class CustomType implements ITypeConverter<CustomType> {
77 private final String val;
78 private CustomType(String val) { this.val = val; }
79 public CustomType convert(String value) { return new CustomType(value); }
80 }
81
82 @Test
83 public void testAddSubcommandWithoutNameRequiresAnnotationName() {
84 CommandLine cmd = new CommandLine(new MainCommand());
85 try {
86 cmd.addSubcommand(new ChildCommand1());
87 fail("Expected exception");
88 } catch (InitializationException ex) {
89 assertEquals("Cannot add subcommand with null name to top", ex.getMessage());
90 }
91 }
92
93 @Command(name = "annotationName")
94 static class SubcommandWithAnnotationName {}
95
96 @Test
97 public void testAddSubcommandWithoutNameUsesInstanceAnnotationName() {
98 CommandLine cmd = new CommandLine(new MainCommand());
99 Object userObject = new SubcommandWithAnnotationName();
100 cmd.addSubcommand(userObject);
101 assertTrue(cmd.getSubcommands().containsKey("annotationName"));
102 assertSame(userObject, cmd.getSubcommands().get("annotationName").getCommand());
103 }
104
105 @Test

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…