@Test public void testDeclarativelyAddSubcommandsSucceedsWithDefaultConstructorForDefaultFactory() { @Command(subcommands = {SubSub1_testDeclarativelyAddSubcommands.class}) class MainCommand {} CommandLine cmdLine = new CommandLine(new MainCommand()); assertEquals(SubSub1_testDeclarativelyAddSubcommands.class.getName(), cmdLine.getSubcommands().get("subsub1").getCommand().getClass().getName()); } @Test
@Test public void testMixinAnnotationCanBeRetrievedByFieldName() { @Command class MixMeIn {} @Command class Receiver { @Mixin MixMeIn mixMeIn; } CommandLine commandLine = new CommandLine(new Receiver(), new InnerClassFactory(this)); assertFalse("mixin was registered", commandLine.getMixins().isEmpty()); assertTrue(commandLine.getMixins().get("mixMeIn") instanceof MixMeIn); Receiver receiver = commandLine.getCommand(); assertNotNull(receiver.mixMeIn); assertSame(receiver.mixMeIn, commandLine.getMixins().get("mixMeIn")); assertSame(receiver.mixMeIn, commandLine.getCommandSpec().mixins().get("mixMeIn").userObject()); }
@Test public void testCommandListReturnsOnlyCommandsRegisteredOnInstance() { CommandLine commandLine = createNestedCommand(); Map<String, CommandLine> commandMap = commandLine.getSubcommands(); assertEquals(2, commandMap.size()); assertTrue("cmd1", commandMap.get("cmd1").getCommand() instanceof ChildCommand1); assertTrue("cmd2", commandMap.get("cmd2").getCommand() instanceof ChildCommand2); }
@Test public void test244SubcommandsNotParsed() { List<CommandLine> list = new CommandLine(new Top()).parse("-h", "sub"); assertEquals(2, list.size()); assertTrue(list.get(0).getCommand() instanceof Top); assertTrue(list.get(1).getCommand() instanceof Sub); assertTrue(((Top) list.get(0).getCommand()).isUsageHelpRequested); }
@Test public void testParentInjectedOnParseWhenConfiguredAsSubcommand() { List<CommandLine> result = new CommandLine(new Top()).parse("-d/tmp/blah", "sub", "3"); Top top = result.get(0).getCommand(); assertEquals(new File("/tmp/blah"), top.baseDirectory); Sub sub = result.get(1).getCommand(); assertEquals(3, sub.count); assertSame(top, sub.parent); } @Test
@Test public void testCommandListReturnsRegisteredCommands() { @Command class MainCommand {} @Command class Command1 {} @Command class Command2 {} CommandLine commandLine = new CommandLine(new MainCommand()); commandLine.addSubcommand("cmd1", new Command1()).addSubcommand("cmd2", new Command2()); Map<String, CommandLine> commandMap = commandLine.getSubcommands(); assertEquals(2, commandMap.size()); assertTrue("cmd1", commandMap.get("cmd1").getCommand() instanceof Command1); assertTrue("cmd2", commandMap.get("cmd2").getCommand() instanceof Command2); }
@Test public void testDeclarativelyAddSubcommandsSucceedsWithDefaultConstructor() { @Command(name = "sub1") class ABCD {} @Command(subcommands = {ABCD.class}) class MainCommand {} CommandLine cmdLine = new CommandLine(new MainCommand(), new InnerClassFactory(this)); assertEquals("picocli.SubcommandTests$1ABCD", cmdLine.getSubcommands().get("sub1").getCommand().getClass().getName()); } @Test
@Test public void testDeclarativelyAddSubcommands() { CommandLine main = new CommandLine(new MainCommand_testDeclarativelyAddSubcommands()); assertEquals(1, main.getSubcommands().size()); CommandLine sub1 = main.getSubcommands().get("sub1"); assertEquals(Sub1_testDeclarativelyAddSubcommands.class, sub1.getCommand().getClass()); assertEquals(1, sub1.getSubcommands().size()); CommandLine subsub1 = sub1.getSubcommands().get("subsub1"); assertEquals(SubSub1_testDeclarativelyAddSubcommands.class, subsub1.getCommand().getClass()); } @Test
@Test public void testInterfaceIsInstantiated() { CommandLine cmd = new CommandLine(Primitives.class); assertTrue(cmd.getCommand() instanceof Primitives); }
@Test public void testGetParentForDeclarativelyAddedSubcommands() { CommandLine main = new CommandLine(new MainCommand_testDeclarativelyAddSubcommands()); assertEquals(1, main.getSubcommands().size()); CommandLine sub1 = main.getSubcommands().get("sub1"); assertSame(main, sub1.getParent()); assertEquals(Sub1_testDeclarativelyAddSubcommands.class, sub1.getCommand().getClass()); assertEquals(1, sub1.getSubcommands().size()); CommandLine subsub1 = sub1.getSubcommands().get("subsub1"); assertSame(sub1, subsub1.getParent()); assertEquals(SubSub1_testDeclarativelyAddSubcommands.class, subsub1.getCommand().getClass()); } @Test
@Test public void testOverwrittenOptionSetsLastValueIfAllowed() { class App { @Option(names = {"-s", "--str"}) String string; @Option(names = {"-v", "--verbose"}) boolean bool; } setTraceLevel("OFF"); CommandLine commandLine = new CommandLine(new App()).setOverwrittenOptionsAllowed(true); commandLine.parse("-s", "1", "--str", "2"); assertEquals("2", ((App) commandLine.getCommand()).string); commandLine = new CommandLine(new App()).setOverwrittenOptionsAllowed(true); commandLine.parse("-v", "--verbose", "-v"); // F -> T -> F -> T assertEquals(true, ((App) commandLine.getCommand()).bool); }
@Test public void testParentNotInjectedWhenConfiguredAsTopLevelCommand() { List<CommandLine> result = new CommandLine(new Sub()).parse("3"); Sub sub = result.get(0).getCommand(); assertNull(sub.parent); assertEquals(3, sub.count); assertEquals(0, sub.result); } @Test
@Test public void testPositionalParamsUnknownArgumentMultiValueWithUnmatchedArgsAllowed() throws Exception { class SingleValue { @Parameters(index = "0..2") String[] str; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true); cmd.parse("val0", "val1", "val2", "val3"); assertArrayEquals(new String[]{"val0", "val1", "val2"}, ((SingleValue)cmd.getCommand()).str); assertEquals(Arrays.asList("val3"), cmd.getUnmatchedArguments()); }
@Test public void testPositionalParamsUnknownArgumentSingleValueWithUnmatchedArgsAllowed() throws Exception { class SingleValue { @Parameters(index = "0") String str; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new SingleValue()).setUnmatchedArgumentsAllowed(true); cmd.parse("val1", "val2"); assertEquals("val1", ((SingleValue)cmd.getCommand()).str); assertEquals(Arrays.asList("val2"), cmd.getUnmatchedArguments()); }
@Test public void testLastValueSelectedIfOptionSpecifiedMultipleTimes() { class App { @Option(names = "-f") String field = null; @Option(names = "-p") int primitive = 43; } setTraceLevel("OFF"); CommandLine cmd = new CommandLine(new App()).setOverwrittenOptionsAllowed(true); cmd.parse("-f", "111", "-f", "222"); App ff = cmd.getCommand(); assertEquals("222", ff.field); }
@Test public void testCommandDefaultProviderSetting() { CommandLine cmd = new CommandLine(App.class); cmd.setDefaultValueProvider(new TestDefaultProvider()); cmd.parse(); App app = cmd.getCommand(); // if no default defined on the option, command default provider should be used assertEquals("Default provider string value", app.optionStringFieldWithoutDefaultNorInitialValue); }
@Test public void testCall1DefaultExceptionHandlerRethrows() { try { CommandLine.call(new MyCallable(), "-x abc"); } catch (ExecutionException ex) { String cmd = ex.getCommandLine().getCommand().toString(); String msg = "Error while calling command (" + cmd + "): java.lang.IllegalStateException: this is a test"; assertEquals(msg, ex.getMessage()); } assertEquals("", systemErrRule.getLog()); assertEquals("", systemOutRule.getLog()); }
@Test public void testPrimitives() { CommandLine cmd = new CommandLine(Primitives.class); cmd.parse("-b -y1 -s2 -i3 -l4 -f5 -d6".split(" ")); Primitives primitives = cmd.getCommand(); assertTrue(primitives.aBoolean()); assertEquals(1, primitives.aByte()); assertEquals(2, primitives.aShort()); assertEquals(3, primitives.anInt()); assertEquals(4, primitives.aLong()); assertEquals(5, primitives.aFloat(), 0.0001); assertEquals(6, primitives.aDouble(), 0.0001); }
@Test public void testPrimitivesWithDefaultValues() { CommandLine cmd = new CommandLine(PrimitivesWithDefault.class); cmd.parse(); PrimitivesWithDefault primitives = cmd.getCommand(); assertTrue(primitives.aBoolean()); assertEquals(11, primitives.aByte()); assertEquals((short) 12, primitives.aShort()); assertEquals(13, primitives.anInt()); assertEquals(14, primitives.aLong()); assertEquals(15.5f, primitives.aFloat(), 0.0001); assertEquals(16.6d, primitives.aDouble(), 0.0001); }
@Test public void testPrimitiveWithoutDefaultValues() { CommandLine cmd = new CommandLine(Primitives.class); cmd.parse(); Primitives primitives = cmd.getCommand(); assertFalse(primitives.aBoolean()); assertEquals(0, primitives.aByte()); assertEquals((short) 0, primitives.aShort()); assertEquals(0, primitives.anInt()); assertEquals(0, primitives.aLong()); assertEquals(0, primitives.aFloat(), 0.0001); assertEquals(0, primitives.aDouble(), 0.0001); }