@Test public void testMultiValuePositionalParamWithMapWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1=1.0", "2=2.0", "3=3.0"); Map<String, String> expected = new LinkedHashMap<String, String>(); expected.put("1", "1.0"); expected.put("2", "2.0"); expected.put("3", "3.0"); assertEquals(expected, spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithMapAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).auxiliaryTypes(Integer.class, Double.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1=1.0", "2=2.0", "3=3.0"); Map<Integer, Double> expected = new LinkedHashMap<Integer, Double>(); expected.put(1, 1.0); expected.put(2, 2.0); expected.put(3, 3.0); assertEquals(expected, spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamArityAloneIsInsufficient() throws Exception { CommandLine.Model.CommandSpec spec = CommandLine.Model.CommandSpec.create(); CommandLine.Model.PositionalParamSpec positional = CommandLine.Model.PositionalParamSpec.builder().index("0").arity("3").type(int.class).build(); assertFalse(positional.isMultiValue()); spec.addPositional(positional); spec.parser().collectErrors(true); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(1, commandLine.getParseResult().errors().size()); assertEquals("Unmatched arguments: 2, 3", commandLine.getParseResult().errors().get(0).getMessage()); } @Test
@Test public void testMultiValuePositionalParamWithMapAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).auxiliaryTypes(Integer.class, Double.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1=1.0", "2=2.0", "3=3.0"); Map<Integer, Double> expected = new LinkedHashMap<Integer, Double>(); expected.put(1, 1.0); expected.put(2, 2.0); expected.put(3, 3.0); assertEquals(expected, spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamArityAloneIsInsufficient() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int.class).build(); assertFalse(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); try { commandLine.parse("1", "2", "3"); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unmatched arguments: 2, 3", ex.getMessage()); } }
@Test public void testMultiValuePositionalParamWithMapWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(Map.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1=1.0", "2=2.0", "3=3.0"); Map<String, String> expected = new LinkedHashMap<String, String>(); expected.put("1", "1.0"); expected.put("2", "2.0"); expected.put("3", "3.0"); assertEquals(expected, spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamArityAloneIsInsufficient() throws Exception { CommandLine.Model.CommandSpec spec = CommandLine.Model.CommandSpec.create(); CommandLine.Model.PositionalParamSpec positional = CommandLine.Model.PositionalParamSpec.builder().index("0").arity("3").type(int.class).build(); assertFalse(positional.isMultiValue()); spec.addPositional(positional); spec.parser().collectErrors(true); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(1, commandLine.getParseResult().errors().size()); assertEquals("Unmatched arguments: 2, 3", commandLine.getParseResult().errors().get(0).getMessage()); } @Test
@Test public void testMultiValuePositionalParamWithListWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList("1", "2", "3"), spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithArray() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int[].class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithListAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).auxiliaryTypes(Integer.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList(1, 2, 3), spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamArityAloneIsInsufficient() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int.class).build(); assertFalse(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); try { commandLine.parse("1", "2", "3"); fail("Expected exception"); } catch (UnmatchedArgumentException ex) { assertEquals("Unmatched arguments: 2, 3", ex.getMessage()); } }
@Test public void testMultiValuePositionalParamWithListAndAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).auxiliaryTypes(Integer.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList(1, 2, 3), spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithArray() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(int[].class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertArrayEquals(new int[] {1, 2, 3}, (int[]) spec.positionalParameters().get(0).getValue()); }
@Test public void testMultiValuePositionalParamWithListWithoutAuxTypes() { CommandSpec spec = CommandSpec.create(); PositionalParamSpec positional = PositionalParamSpec.builder().index("0").arity("3").type(List.class).build(); assertTrue(positional.isMultiValue()); spec.addPositional(positional); CommandLine commandLine = new CommandLine(spec); commandLine.parse("1", "2", "3"); assertEquals(Arrays.asList("1", "2", "3"), spec.positionalParameters().get(0).getValue()); }