View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         https://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
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  }