1+ /*
2+ * Copyright 2026-present the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package org .springframework .shell .core .command ;
17+
18+ import org .junit .jupiter .api .BeforeEach ;
19+ import org .junit .jupiter .params .ParameterizedTest ;
20+ import org .junit .jupiter .params .provider .CsvSource ;
21+ import org .springframework .shell .core .InputReader ;
22+
23+ import java .io .PrintWriter ;
24+
25+ import static org .junit .jupiter .api .Assertions .assertEquals ;
26+ import static org .mockito .Mockito .mock ;
27+
28+ class CommandContextTests {
29+
30+ private CommandContext context ;
31+
32+ @ BeforeEach
33+ void setUp () {
34+ ParsedInput parsedInput = ParsedInput .builder ()
35+ .addOption (CommandOption .with ().longName ("aWrong" ).description ("command1" ).build ())
36+ .addOption (CommandOption .with ().longName ("intendedA" ).shortName ('a' ).description ("command2" ).build ())
37+ .addOption (CommandOption .with ().shortName ('b' ).description ("command3" ).build ())
38+ .addOption (CommandOption .with ().longName ("x" ).description ("command4" ).build ())
39+ .addOption (CommandOption .with ().shortName ('x' ).description ("command5" ).build ())
40+ .addOption (CommandOption .with ().shortName ('y' ).description ("command6" ).build ())
41+ .addOption (CommandOption .with ().longName ("y" ).description ("command7" ).build ())
42+ .build ();
43+ context = new CommandContext (parsedInput , mock (CommandRegistry .class ), mock (PrintWriter .class ),
44+ mock (InputReader .class ));
45+ }
46+
47+ @ ParameterizedTest
48+ @ CsvSource ({ "aWrong, command1" , "intendedA, command2" , "noSuchOption, null" , "x, command4" , "y, command7" })
49+ void testGetOptionByLongName (String longName , String description ) {
50+ // when
51+ CommandOption commandOption = context .getOptionByLongName (longName );
52+
53+ // then
54+ String result = commandOption == null ? "null" : commandOption .description ();
55+ assertEquals (description , result );
56+ }
57+
58+ @ ParameterizedTest
59+ @ CsvSource ({ "a, command2" , "b, command3" , "n, null" , "x, command5" , "y, command6" })
60+ void testGetOptionByShortName (char shortName , String description ) {
61+ // when
62+ CommandOption commandOption = context .getOptionByShortName (shortName );
63+
64+ // then
65+ String result = commandOption == null ? "null" : commandOption .description ();
66+ assertEquals (description , result );
67+ }
68+
69+ @ ParameterizedTest
70+ @ CsvSource ({ "aWrong, command1" , "intendedA, command2" , "noSuchOption, null" , "a, command2" , "b, command3" ,
71+ "n, null" , "x, command4" , "y, command7" })
72+ void testGetOptionByName (String optionName , String description ) {
73+ // when
74+ CommandOption commandOption = context .getOptionByName (optionName );
75+
76+ // then
77+ String result = commandOption == null ? "null" : commandOption .description ();
78+ assertEquals (description , result );
79+ }
80+
81+ }
0 commit comments