1+ package org .springframework .shell .core .command ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .params .ParameterizedTest ;
5+ import org .junit .jupiter .params .provider .CsvSource ;
6+ import org .springframework .shell .core .InputReader ;
7+
8+ import java .io .PrintWriter ;
9+
10+ import static org .junit .jupiter .api .Assertions .assertEquals ;
11+ import static org .mockito .Mockito .mock ;
12+
13+ class CommandContextTests {
14+
15+ private CommandContext context ;
16+
17+ @ BeforeEach
18+ void setUp () {
19+ ParsedInput parsedInput = ParsedInput .builder ()
20+ .addOption (CommandOption .with ().longName ("aWrong" ).description ("command1" ).build ())
21+ .addOption (CommandOption .with ().longName ("intendedA" ).shortName ('a' ).description ("command2" ).build ())
22+ .addOption (CommandOption .with ().shortName ('b' ).description ("command3" ).build ())
23+ .addOption (CommandOption .with ().longName ("x" ).description ("command4" ).build ())
24+ .addOption (CommandOption .with ().shortName ('x' ).description ("command5" ).build ())
25+ .addOption (CommandOption .with ().shortName ('y' ).description ("command6" ).build ())
26+ .addOption (CommandOption .with ().longName ("y" ).description ("command7" ).build ())
27+ .build ();
28+ context = new CommandContext (parsedInput , mock (CommandRegistry .class ), mock (PrintWriter .class ),
29+ mock (InputReader .class ));
30+ }
31+
32+ @ ParameterizedTest
33+ @ CsvSource ({ "aWrong, command1" , "intendedA, command2" , "noSuchOption, null" , "x, command4" , "y, command7" })
34+ void testGetOptionByLongName (String longName , String description ) {
35+ // when
36+ CommandOption commandOption = context .getOptionByLongName (longName );
37+
38+ // then
39+ String result = commandOption == null ? "null" : commandOption .description ();
40+ assertEquals (description , result );
41+ }
42+
43+ @ ParameterizedTest
44+ @ CsvSource ({ "a, command2" , "b, command3" , "n, null" , "x, command5" , "y, command6" })
45+ void testGetOptionByShortName (char shortName , String description ) {
46+ // when
47+ CommandOption commandOption = context .getOptionByShortName (shortName );
48+
49+ // then
50+ String result = commandOption == null ? "null" : commandOption .description ();
51+ assertEquals (description , result );
52+ }
53+
54+ @ ParameterizedTest
55+ @ CsvSource ({ "aWrong, command1" , "intendedA, command2" , "noSuchOption, null" , "a, command2" , "b, command3" ,
56+ "n, null" , "x, command4" , "y, command7" })
57+ void testGetOptionByName (String optionName , String description ) {
58+ // when
59+ CommandOption commandOption = context .getOptionByName (optionName );
60+
61+ // then
62+ String result = commandOption == null ? "null" : commandOption .description ();
63+ assertEquals (description , result );
64+ }
65+
66+ }
0 commit comments