1package com.beust.jcommander.dynamic;
2
3import com.beust.jcommander.JCommander;
4import com.beust.jcommander.ParameterException;
5import com.beust.jcommander.internal.Maps;
6
7import org.testng.Assert;
8import org.testng.annotations.Test;
9
10@Test
11public class DynamicParameterTest {
12
13  @Test(expectedExceptions = ParameterException.class)
14  public void nonMapShouldThrow() {
15    new JCommander(new DSimpleBad()).parse("-D", "a=b", "-D", "c=d");
16  }
17
18  @Test(expectedExceptions = ParameterException.class)
19  public void wrongSeparatorShouldThrow() {
20    DSimple ds = new DSimple();
21    new JCommander(ds).parse("-D", "a:b", "-D", "c=d");
22  }
23
24  private void simple(String... parameters) {
25    DSimple ds = new DSimple();
26    new JCommander(ds).parse(parameters);
27    Assert.assertEquals(ds.params, Maps.newHashMap("a", "b", "c", "d"));
28  }
29
30  public void simpleWithSpaces() {
31    simple("-D", "a=b", "-D", "c=d");
32  }
33
34  public void simpleWithoutSpaces() {
35    simple("-Da=b", "-Dc=d");
36  }
37
38  public void usage() {
39    DSimple ds = new DSimple();
40    new JCommander(ds).usage(new StringBuilder());
41  }
42
43  public void differentAssignment() {
44    DSimple ds = new DSimple();
45    new JCommander(ds).parse("-D", "a=b", "-A", "c@d");
46    Assert.assertEquals(ds.params, Maps.newHashMap("a", "b"));
47    Assert.assertEquals(ds.params2, Maps.newHashMap("c", "d"));
48  }
49
50  @Test(enabled = false)
51  public static void main(String[] args) {
52    DynamicParameterTest dpt = new DynamicParameterTest();
53    dpt.simpleWithSpaces();
54//    dpt.nonMapShouldThrow();
55//    dpt.wrongSeparatorShouldThrow();
56//    dpt.differentAssignment();
57//    dpt.arity0();
58//    dpt.usage();
59  }
60}
61