static Optional<String> execute(Experiment.Builder builder, String[] args) { return createMenu(builder).execute(args); }
static Optional<String> execute(FileProvider.Builder builder, String[] args) { return createDefaultMenu(builder).execute(args); }
static Optional<String> execute(Experiment.Builder builder, String[] args) { return createMenu(builder).execute(args); }
static Optional<String> execute(FileProvider.Builder builder, String[] args) { return createDefaultMenu(builder).execute(args); }
/** * Same as {@link #execute(String...)} but catches all thrown * {@link CliException}s. If an exception is thrown it's message will be added * to the returned error message. * @param args The arguments to parse. * @return A string containing an error message or {@link Optional#absent()} * if no error occurred. */ public Optional<String> safeExecute(String... args) { try { return execute(args); } catch (final CliException e) { return Optional.of(Joiner.on("\n").join(e.getMessage(), printHelp())); } }
/** * Tests for boolean arguments. */ @Test public void testBooleanArgType() { menu.execute("-happy", "T"); menu.execute("-happy", "f"); menu.execute("-happy", "1"); menu.execute("-happy", "0"); menu.execute("-happy", "True"); menu.execute("-happy", "fAlse"); assertEquals(asList(true, false, true, false, true, false), list); testFail(menu, "happy", CauseType.INVALID_ARG_FORMAT, "-happy", "FF"); testFail(menu, "happy", CauseType.INVALID_ARG_FORMAT, "-happy", "truth"); }
/** * Tests the whether parsing of longs is correct. */ @Test public void testLongArgType() { assertTrue(list.isEmpty()); assertFalse(menu.execute("-a", "234").isPresent()); assertEquals(asList(234L), list); assertFalse(menu.execute("-a", "-1").isPresent()); assertEquals(asList(234L, -1L), list); assertFalse(menu.execute("--add-all", "10,100", "-a", "-10").isPresent()); assertEquals(asList(234L, -1L, 10L, 100L, -10L), list); list.clear(); assertFalse(menu.execute("-a", "-10", "--add-all", "10,100").isPresent()); assertEquals(asList(-10L, 10L, 100L), list); }
/** * Test the warmup option. */ @Test public void testWarmup() { assertThat(menu.execute("-w", "123")).isAbsent(); assertThat(builder.warmupPeriodMs).isEqualTo(123); testFail(menu, "w", CauseType.HANDLER_FAILURE, "-w", "-9"); } }
/** * Tests whether string parsing is correct. */ @Test public void testStringArgType() { assertTrue(list.isEmpty()); assertFalse(menu.execute("-asl", "hello world", "bye").isPresent()); assertEquals(asList("hello world", "bye"), list); testFail("asl", CauseType.MISSING_ARG, "-asl"); assertEquals(asList("hello world", "bye"), list); assertFalse(menu.execute("-as", "hello again").isPresent()); assertEquals(asList("hello world", "bye", "hello again"), list); testFail("as", CauseType.MISSING_ARG, "-as"); }
/** * Test the correct detection of a missing argument. */ @Test public void testMissingArg() { testFail("a", CauseType.MISSING_ARG, "-a"); assertTrue(menu.safeExecute("-a").isPresent()); // -aa has an optional argument, so this is valid assertFalse(menu.execute("-aa").isPresent()); }
/** * Tests whether the specified args will fail. * @param m The menu to use. * @param causeType The cause of the error. * @param args The options to execute. */ public static void testFail(Menu m, CauseType causeType, String... args) { try { m.execute(args); } catch (final CliException e) { assertEquals(causeType, e .getCauseType()); return; } fail("No exception occured."); }
/** * Test correct applying of order. */ @Test public void testOrdering() { testFail(menu, "o", CauseType.HANDLER_FAILURE, "-o", "CONFIG,SCENARIO"); testFail(menu, "o", CauseType.HANDLER_FAILURE, "-o", "SEED_REPS,REPS,SCENARIO,CONFIG,SEED_REPS"); assertThat(builder.experimentOrdering) .isEqualTo(Experiment.Builder.DEFAULT_EXPERIMENT_ORDERING); menu.execute("-o", "SEED_REPS,REPS,SCENARIO,CONFIG"); assertThat(builder.experimentOrdering) .containsExactly( SimulationProperty.SEED_REPS, SimulationProperty.REPS, SimulationProperty.SCENARIO, SimulationProperty.CONFIG) .inOrder(); }
.build(); m.execute("-e", "A,B,C"); assertThat(consumer) .containsExactly(asList(TestEnum.A, TestEnum.B, TestEnum.C)) .inOrder(); consumer.clear(); m.execute("-e", "C,B,A"); assertThat(consumer) .containsExactly(asList(TestEnum.C, TestEnum.B, TestEnum.A)) consumer.clear(); m.execute("-e", "B"); assertThat(consumer) .containsExactly(asList(TestEnum.B));
/** * Tests correct parsing of enum. */ @Test public void testEnum() { final List<TestEnum> consumer = new ArrayList<>(); final Menu m = Menu.builder() .addHelpOption("h", "help", "Hi") .add(Option.builder("e", ArgumentParser.enumParser("testenum", TestEnum.class)).build(), consumer, new AddToListHandler<TestEnum>()) .build(); m.execute("-e", "A"); assertThat(consumer) .containsExactly(TestEnum.A); consumer.clear(); m.execute("-e", "B"); assertThat(consumer) .containsExactly(TestEnum.B); consumer.clear(); testFail(m, "e", CauseType.INVALID_ARG_FORMAT, "-e", "a"); testFail(m, "e", CauseType.INVALID_ARG_FORMAT, "-e", "NON_EXISTING_ENUM"); assertThat(consumer).isEmpty(); }
/** * Tests whether the specified args will fail. * @param m The menu to use. * @param failingOptionName The name of the failing option. * @param causeType The cause of the error. * @param args The options to execute. */ public static void testFail(Menu m, String failingOptionName, CauseType causeType, String... args) { try { m.execute(args); } catch (final CliException e) { assertEquals(failingOptionName, e .getMenuOption().get().getShortName()); assertEquals(causeType, e .getCauseType()); return; } fail("No exception occured."); }
/** * Tests whether the specified args will fail. * @param m The menu to use. * @param failingOptionName The name of the failing option. * @param causeType The cause of the error. * @param rootCause The type of the throwable which is the root cause of the * failure. * @param args The options to execute. */ public static void testFail(Menu m, String failingOptionName, CauseType causeType, Class<? extends Throwable> rootCause, String... args) { try { m.execute(args); } catch (final CliException e) { assertEquals(failingOptionName, e.getMenuOption().get().getShortName()); assertEquals(causeType, e.getCauseType()); assertEquals(rootCause, e.getCause().getClass()); return; } fail(); }
/** * Constructs a menu wilt multiple groups and tests the behavior. */ @Test public void multipleGroups() { final Menu m = Menu.builder() .openGroup() .add(Option.builder("a").build(), list, dummyHandler()) .add(Option.builder("b").build(), list, dummyHandler()) .openGroup() .add(Option.builder("c").build(), list, dummyHandler()) .add(Option.builder("d").build(), list, dummyHandler()) .add(Option.builder("e").build(), list, dummyHandler()) .closeGroup() .addHelpOption("h", "help", "Print me") .build(); assertFalse(m.execute("-a", "-c").isPresent()); assertFalse(m.execute("-b", "-d").isPresent()); testFail(m, "a", CauseType.ALREADY_SELECTED, "-b", "-a"); testFail(m, "b", CauseType.ALREADY_SELECTED, "-d", "-a", "-b"); testFail(m, "c", CauseType.ALREADY_SELECTED, "-d", "-a", "-c"); }