1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.cli;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import org.junit.jupiter.api.Test;
23
24 class OptionCountTest {
25 private static final Option VERBOSITY = new Option("v", "verbosity: use multiple times for more");
26 private static final Options OPTIONS = new Options().addOption(VERBOSITY);
27
28 @Test
29 void testFiveSwitchesMixed() throws ParseException {
30 final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-v", "-vvv", "-v"});
31 assertEquals(5, cmdLine.getOptionCount(VERBOSITY));
32 }
33
34 @Test
35 void testNoSwitch() throws ParseException {
36 final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{});
37 assertEquals(0, cmdLine.getOptionCount(VERBOSITY));
38 }
39
40 @Test
41 void testOneSwitch() throws ParseException {
42 final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-v"});
43 assertEquals(1, cmdLine.getOptionCount(VERBOSITY));
44 assertEquals(1, cmdLine.getOptionCount("v"));
45 assertEquals(1, cmdLine.getOptionCount('v'));
46 }
47
48 @Test
49 void testThreeSwitches() throws ParseException {
50 final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-v", "-v", "-v"});
51 assertEquals(3, cmdLine.getOptionCount(VERBOSITY));
52 }
53
54 @Test
55 void testThreeSwitchesCompact() throws ParseException {
56 final CommandLine cmdLine = new DefaultParser().parse(OPTIONS, new String[]{"-vvv"});
57 assertEquals(3, cmdLine.getOptionCount(VERBOSITY));
58 }
59 }