@Test public void testArgumentInjection() throws CLIException { CommandForArgumentInjectionTest command = new CommandForArgumentInjectionTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get()).isEqualTo("foo"); }
@Test public void testArgumentInjectionWithSeveralArguments() throws CLIException { CommandForMultipleArgumentTest command = new CommandForMultipleArgumentTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo", "1"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(1); }
@Test public void testArgumentWithDefaultValue() throws CLIException { CommandWithDefaultValueOnArgument command = new CommandWithDefaultValueOnArgument(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "foo"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.x.get()).isEqualTo("foo"); assertThat(command.y.get()).isEqualTo(25); }
@Test public void testInjectionOfString() throws CLIException { HelloClI command = new HelloClI(); CLI cli = CLIConfigurator.define(HelloClI.class); CommandLine evaluatedCLI = cli.parse(Arrays.asList("--name", "vert.x")); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.run()).isEqualToIgnoringCase("Hello vert.x"); assertThat(command.name).isEqualToIgnoringCase("vert.x"); }
@Test public void testArgumentInjectionWithConvertedByAndDefaultValue() throws CLIException { CommandForConvertedValueTest command = new CommandForConvertedValueTest(); CLI cli = CLIConfigurator.define(command.getClass()).setName("test"); CommandLine evaluatedCLI = parse(cli, "Bob,Morane"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bob"); assertThat(command.reference.get().last).isEqualTo("Morane"); evaluatedCLI = parse(cli); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.reference.get().first).isEqualTo("Bill"); assertThat(command.reference.get().last).isEqualTo("Balantine"); }
@Test public void testTypeExtraction() { CLI command = CLIConfigurator.define(CommandForTypeExtractTest.class); assertThat(command.getOptions()).hasSize(6); TypedOption model = (TypedOption) find(command.getOptions(), "list"); assertThat(model.getType()).isEqualTo(String.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "set"); assertThat(model.getType()).isEqualTo(Character.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "collection"); assertThat(model.getType()).isEqualTo(Integer.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "tree"); assertThat(model.getType()).isEqualTo(String.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "al"); assertThat(model.getType()).isEqualTo(String.class); assertThat(model.isMultiValued()).isTrue(); model = (TypedOption) find(command.getOptions(), "array"); assertThat(model.getType()).isEqualTo(Integer.TYPE); assertThat(model.isMultiValued()).isTrue(); }
@Test public void testOptionsWithDescription() { CLI cli = CLIConfigurator.define(CommandForDescriptionTest.class); assertThat(cli.getOptions()).hasSize(1); assertThat(find(cli.getOptions(), "option").getDescription()) .isEqualTo("This option is awesome"); }
@Test public void testOptionsWithDefaultValue() { CLI cli = CLIConfigurator.define(CommandForDefaultValueTest.class); assertThat(cli.getOptions()).hasSize(1); assertThat(find(cli.getOptions(), "option").getDefaultValue()).isEqualTo("bar"); assertThat(find(cli.getOptions(), "option").getName()).isEqualTo("option"); }
@Test public void testMultiValuesInjection() throws CLIException { CLIWithMultipleValues command = new CLIWithMultipleValues(); CLI cli = CLIConfigurator.define(command.getClass()); CommandLine evaluatedCLI = parse(cli, "--persons=x", "--persons", "y", "z"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons).hasSize(3); evaluatedCLI = parse(cli, "--persons2=x", "--persons2", "y", "z"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons2).hasSize(3); evaluatedCLI = parse(cli, "--persons3=x", "--persons3", "y", "z"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons3).hasSize(3); evaluatedCLI = parse(cli, "--persons4=x:y:z"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.persons4).hasSize(3); evaluatedCLI = parse(cli, "--states=NEW", "--states", "BLOCKED", "RUNNABLE"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.states) .hasSize(3) .containsExactly(Thread.State.NEW, Thread.State.BLOCKED, Thread.State.RUNNABLE); evaluatedCLI = parse(cli, "--ints=1", "--ints", "2", "3"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.ints).hasSize(3).containsExactly(1, 2, 3); evaluatedCLI = parse(cli, "--shorts=1", "--shorts", "2", "3"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.shorts).hasSize(3).containsExactly((short) 1, (short) 2, (short) 3); evaluatedCLI = parse(cli, "--strings=a"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.strings).hasSize(1).containsExactly("a"); evaluatedCLI = parse(cli, "--doubles=1", "--doubles", "2.2", "3.3"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.doubles).hasSize(3).containsExactly(1.0, 2.2, 3.3); }
@Test public void testOptionsParsedAsList() { CLI command = CLIConfigurator.define(CommandForParsedAsList.class); assertThat(command.getOptions()).hasSize(1); assertThat(((TypedOption) find(command.getOptions(), "option")).getListSeparator()) .isEqualTo(":"); assertThat(find(command.getOptions(), "option").isMultiValued()).isTrue(); assertThat(((TypedOption) find(command.getOptions(), "option")).getType()) .isEqualTo(String.class); }
@Test public void testUsage() { CLI command = CLIConfigurator.define(HelloClI.class); StringBuilder builder = new StringBuilder(); command.usage(builder); assertThat(builder) .containsIgnoringCase("Usage: hello -n <name>") .containsIgnoringCase("A command saying hello.") .containsIgnoringCase("A simple cli to wish you a good day. Pass your name with `--name`") .containsIgnoringCase(" -n,--name <name> your name"); }
@Test public void testHelloCLIFromClass() { CLI command = CLIConfigurator.define(HelloClI.class); assertThat(command.getOptions()).hasSize(1); TypedOption option = (TypedOption) find(command.getOptions(), "name"); assertThat(option.getLongName()).isEqualToIgnoringCase("name"); assertThat(option.getShortName()).isEqualToIgnoringCase("n"); assertThat(option.getType()).isEqualTo(String.class); assertThat(option.getArgName()).isEqualTo("name"); assertThat(option.getDescription()).isEqualToIgnoringCase("your name"); assertThat(option.getDefaultValue()).isNull(); assertThat(option.acceptValue()).isTrue(); assertThat(option.isMultiValued()).isFalse(); assertThat(option.isRequired()).isTrue(); }
@Test public void testSingleValueInjection() throws CLIException { CLIWithSingleValue command = new CLIWithSingleValue(); CLI cli = CLIConfigurator.define(command.getClass()); CommandLine evaluatedCLI = parse( cli, "--boolean", "--short=1", "--byte=1", "--int=1", "--long=1", "--double=1.1", "--float=1.1", "--char=c", "--string=hello"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aBoolean).isTrue(); assertThat(command.aShort).isEqualTo((short) 1); assertThat(command.aByte).isEqualTo((byte) 1); assertThat(command.anInt).isEqualTo(1); assertThat(command.aLong).isEqualTo(1l); assertThat(command.aDouble).isEqualTo(1.1d); assertThat(command.aFloat).isEqualTo(1.1f); assertThat(command.aChar).isEqualTo('c'); assertThat(command.aString).isEqualTo("hello"); evaluatedCLI = parse( cli, "--boolean2", "--short2=1", "--byte2=1", "--int2=1", "--long2=1", "--double2=1.1", "--float2=1.1", "--char2=c", "--string=hello"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.anotherBoolean).isTrue(); assertThat(command.anotherShort).isEqualTo((short) 1); assertThat(command.anotherByte).isEqualTo((byte) 1); assertThat(command.anotherInt).isEqualTo(1); assertThat(command.anotherLong).isEqualTo(1l); assertThat(command.anotherDouble).isEqualTo(1.1d); assertThat(command.anotherFloat).isEqualTo(1.1f); assertThat(command.anotherChar).isEqualTo('c'); assertThat(command.aString).isEqualTo("hello"); evaluatedCLI = parse(cli, "--state=NEW"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aState).isEqualTo(Thread.State.NEW); evaluatedCLI = parse(cli, "--person=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aPerson.name).isEqualTo("vert.x"); evaluatedCLI = parse(cli, "--person2=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.anotherPerson.name).isEqualTo("vert.x"); evaluatedCLI = parse(cli, "--person3=vert.x"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aThirdPerson.name).isEqualTo("vert.x"); evaluatedCLI = parse(cli, "--person4=bob,morane"); CLIConfigurator.inject(evaluatedCLI, command); assertThat(command.aFourthPerson.first).isEqualTo("bob"); assertThat(command.aFourthPerson.last).isEqualTo("morane"); }