aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/jopt-simple/src/test/java/joptsimple/OptionSetAsMapTest.java
blob: 0a8c9a67d62c892f5a7b3422bf2f1705787e1639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package joptsimple;

import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.util.Arrays.asList;
import static java.util.Collections.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
 * @author <a href="mailto:binkley@alumni.rice.edu">B. K. Oxley (binkley)</a>
 */
public class OptionSetAsMapTest extends AbstractOptionParserFixture {
    @Test
    public void gives() {
        final OptionSpec<Void> a = parser.accepts( "a" );
        final OptionSpec<String> b = parser.accepts( "b" ).withRequiredArg();
        final OptionSpec<String> c = parser.accepts( "c" ).withOptionalArg();
        final OptionSpec<String> d = parser.accepts( "d" ).withRequiredArg().defaultsTo( "1" );
        final OptionSpec<String> e = parser.accepts( "e" ).withOptionalArg().defaultsTo( "2" );
        final OptionSpec<String> f = parser.accepts( "f" ).withRequiredArg().defaultsTo( "3" );
        final OptionSpec<String> g = parser.accepts( "g" ).withOptionalArg().defaultsTo( "4" );
        final OptionSpec<Void> h = parser.accepts( "h" );

        OptionSet options = parser.parse( "-a", "-e", "-c", "5", "-d", "6", "-b", "4", "-d", "7", "-e", "8" );

        Map<OptionSpec<?>, List<?>> expected = new HashMap<OptionSpec<?>, List<?>>() {
            private static final long serialVersionUID = Long.MIN_VALUE;

            {
                put( a, emptyList() );
                put( b, asList( "4" ) );
                put( c, asList( "5" ) );
                put( d, asList( "6", "7" ) );
                put( e, asList( "8" ) );
                put( f, asList( "3" ) );
                put( g, asList( "4" ) );
                put( h, emptyList() );
            }
        };

        assertThat( options.asMap(), is( equalTo( expected ) ) );
    }
}