@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 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 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 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"); }