@Test public void testUnmatchedArgsBinding_forStringCollectionSupplier_exceptionsRethrownAsPicocliException() { class ThrowingGetter implements IGetter { public <T> T get() { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringCollectionSupplier(new ThrowingGetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not add unmatched argument array '[]' to collection returned by getter (")); assertTrue(ex.getMessage(), ex.getMessage().endsWith("): java.lang.RuntimeException: test")); } }
@Test public void testUnmatchedArgsBinding_forStringArrayConsumer_exceptionsRethrownAsPicocliException() { class ThrowingSetter implements ISetter { public <T> T set(T value) { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringArrayConsumer(new ThrowingSetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not invoke setter ")); assertTrue(ex.getMessage(), ex.getMessage().contains(") with unmatched argument array '[]': java.lang.RuntimeException: test")); } } }
@Test public void testUnmatchedArgsBinding_forStringCollectionSupplier_exceptionsRethrownAsPicocliException() { class ThrowingGetter implements IGetter { public <T> T get() { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringCollectionSupplier(new ThrowingGetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not add unmatched argument array '[]' to collection returned by getter (")); assertTrue(ex.getMessage(), ex.getMessage().endsWith("): java.lang.RuntimeException: test")); } }
private boolean canConsumeOneArgument(ArgSpec argSpec, Range arity, int consumed, String arg, Class<?> type, String argDescription) { ITypeConverter<?> converter = getTypeConverter(type, argSpec, 0); try { String[] values = argSpec.splitValue(trim(arg), commandSpec.parser(), arity, consumed); // if (!argSpec.acceptsValues(values.length, commandSpec.parser())) { // tracer.debug("$s would split into %s values but %s cannot accept that many values.%n", arg, values.length, argDescription); // return false; // } for (String value : values) { tryConvert(argSpec, -1, converter, value, type); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", arg, argDescription, ex.getMessage()); return false; } }
private boolean canConsumeOneMapArgument(ArgSpec argSpec, Range arity, int consumed, String raw, Class<?>[] classes, ITypeConverter<?> keyConverter, ITypeConverter<?> valueConverter, String argDescription) { String[] values = argSpec.splitValue(raw, commandSpec.parser(), arity, consumed); try { for (String value : values) { String[] keyValue = splitKeyValue(argSpec, value); tryConvert(argSpec, -1, keyConverter, keyValue[0], classes[0]); tryConvert(argSpec, -1, valueConverter, keyValue[1], classes[1]); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", raw, argDescription, ex.getMessage()); return false; } }
@Test public void testUnmatchedArgsBinding_forStringArrayConsumer_exceptionsRethrownAsPicocliException() { class ThrowingSetter implements ISetter { public <T> T set(T value) { throw new RuntimeException("test"); } } try { UnmatchedArgsBinding.forStringArrayConsumer(new ThrowingSetter()).addAll(new String[0]); fail("Expected exception"); } catch (PicocliException ex) { assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not invoke setter ")); assertTrue(ex.getMessage(), ex.getMessage().contains(") with unmatched argument array '[]': java.lang.RuntimeException: test")); } } }
private boolean canConsumeOneArgument(ArgSpec argSpec, Range arity, int consumed, String arg, Class<?> type, String argDescription) { ITypeConverter<?> converter = getTypeConverter(type, argSpec, 0); try { String[] values = argSpec.splitValue(trim(arg), commandSpec.parser(), arity, consumed); // if (!argSpec.acceptsValues(values.length, commandSpec.parser())) { // tracer.debug("$s would split into %s values but %s cannot accept that many values.%n", arg, values.length, argDescription); // return false; // } for (String value : values) { tryConvert(argSpec, -1, converter, value, type); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", arg, argDescription, ex.getMessage()); return false; } }
private boolean canConsumeOneMapArgument(ArgSpec argSpec, Range arity, int consumed, String raw, Class<?>[] classes, ITypeConverter<?> keyConverter, ITypeConverter<?> valueConverter, String argDescription) { String[] values = argSpec.splitValue(raw, commandSpec.parser(), arity, consumed); try { for (String value : values) { String[] keyValue = splitKeyValue(argSpec, value); tryConvert(argSpec, -1, keyConverter, keyValue[0], classes[0]); tryConvert(argSpec, -1, valueConverter, keyValue[1], classes[1]); } return true; } catch (PicocliException ex) { tracer.debug("$s cannot be assigned to %s: type conversion fails: %s.%n", raw, argDescription, ex.getMessage()); return false; } }