1+ package cz .foresttech .commandapi .shared ;
2+
3+ import org .junit .jupiter .api .BeforeEach ;
4+ import org .junit .jupiter .api .Test ;
5+ import org .mockito .Mockito ;
6+
7+ import java .util .List ;
8+
9+ import static org .junit .jupiter .api .Assertions .assertEquals ;
10+ import static org .junit .jupiter .api .Assertions .assertTrue ;
11+ import static org .mockito .ArgumentMatchers .any ;
12+ import static org .mockito .Mockito .*;
13+
14+ class OnCommandTest {
15+
16+ private TestCommandAPI testCommandAPI ;
17+ private TestCommand testCommand ;
18+ private TestCommandSenderWrapper testCommandSenderWrapper ;
19+
20+ @ BeforeEach
21+ public void setUp () {
22+ testCommandAPI = new TestCommandAPI ();
23+ testCommand = Mockito .mock (TestCommand .class );
24+ testCommandSenderWrapper = new TestCommandSenderWrapper ("apik007" );
25+ assertTrue (testCommandAPI .registerCommand (testCommand ));
26+ }
27+
28+ @ Test
29+ void onNonExistingCommand () {
30+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "non_exist_cmd" , new String []{}));
31+ verify (testCommand , never ()).defaultSubCmd (any ());
32+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
33+ verify (testCommand , never ()).testSubCmd (any ());
34+ verify (testCommand , never ()).testSubCmdAlias (any ());
35+ verify (testCommand , never ()).argSubCmd (any (), any ());
36+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
37+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
38+ }
39+
40+ @ Test
41+ void onDefaultSubCommand () {
42+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{}));
43+ verify (testCommand ).defaultSubCmd ("apik007" );
44+ reset (testCommand );
45+
46+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"rng" }));
47+ verify (testCommand , never ()).defaultSubCmd (any ());
48+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
49+ reset (testCommand );
50+
51+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"10.5" , "rng" }));
52+ verify (testCommand , never ()).defaultSubCmd (any ());
53+ verify (testCommand ).defaultSubCmd ("apik007" , 10.5 , "rng" );
54+ reset (testCommand );
55+
56+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"10.5" , "rng" , "message" }));
57+ verify (testCommand , never ()).defaultSubCmd (any ());
58+ verify (testCommand ).defaultSubCmd ("apik007" , 10.5 , "rng message" );
59+ }
60+
61+ @ Test
62+ void parsing () {
63+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"10.5s" , "rng" , "message" }));
64+ verify (testCommand , never ()).defaultSubCmd (any ());
65+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
66+ verify (testCommand , never ()).testSubCmd (any ());
67+ verify (testCommand , never ()).testSubCmdAlias (any ());
68+ verify (testCommand , never ()).argSubCmd (any (), any ());
69+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
70+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
71+
72+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"-100.5" , "rng" , "message" }));
73+ verify (testCommand , never ()).defaultSubCmd (any ());
74+ verify (testCommand , never ()).testSubCmd (any ());
75+ verify (testCommand , never ()).testSubCmdAlias (any ());
76+ verify (testCommand , never ()).argSubCmd (any (), any ());
77+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
78+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
79+ verify (testCommand ).defaultSubCmd ("apik007" , -100.5 , "rng message" );
80+
81+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"-100" , "rng" , "message" }));
82+ verify (testCommand , never ()).defaultSubCmd (any ());
83+ verify (testCommand , never ()).testSubCmd (any ());
84+ verify (testCommand , never ()).testSubCmdAlias (any ());
85+ verify (testCommand , never ()).argSubCmd (any (), any ());
86+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
87+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
88+ verify (testCommand ).defaultSubCmd ("apik007" , -100.0 , "rng message" );
89+ }
90+
91+ @ Test
92+ void onSubCommandNoArgs () {
93+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"test" }));
94+ verify (testCommand , never ()).defaultSubCmd (any ());
95+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
96+ verify (testCommand ).testSubCmd ("apik007" );
97+ reset (testCommand );
98+
99+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"test_alias" }));
100+ verify (testCommand , never ()).defaultSubCmd (any ());
101+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
102+ verify (testCommand , never ()).testSubCmd (any ());
103+ verify (testCommand ).testSubCmdAlias ("apik007" );
104+ reset (testCommand );
105+
106+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"test_alias2" }));
107+ verify (testCommand , never ()).defaultSubCmd (any ());
108+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
109+ verify (testCommand , never ()).testSubCmd (any ());
110+ verify (testCommand ).testSubCmdAlias ("apik007" );
111+ }
112+
113+ @ Test
114+ void onSubCommandArgs () {
115+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"arg" }));
116+ verify (testCommand , never ()).defaultSubCmd (any ());
117+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
118+ verify (testCommand , never ()).testSubCmd (any ());
119+ verify (testCommand , never ()).testSubCmdAlias (any ());
120+ verify (testCommand , never ()).argSubCmd (any (), any ());
121+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
122+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
123+ reset (testCommand );
124+
125+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"arg" , "rng" }));
126+ verify (testCommand , never ()).defaultSubCmd (any ());
127+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
128+ verify (testCommand , never ()).testSubCmd (any ());
129+ verify (testCommand , never ()).testSubCmdAlias (any ());
130+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
131+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
132+ verify (testCommand ).argSubCmd ("apik007" , "rng" );
133+ reset (testCommand );
134+
135+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"non-required" , "rng" }));
136+ verify (testCommand , never ()).defaultSubCmd (any ());
137+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
138+ verify (testCommand , never ()).testSubCmd (any ());
139+ verify (testCommand , never ()).testSubCmdAlias (any ());
140+ verify (testCommand , never ()).argSubCmd (any (), any ());
141+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
142+ verify (testCommand ).argSubCmd ("apik007" , "rng" , null );
143+ reset (testCommand );
144+
145+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"non-required" , "rng" , "unneeded" }));
146+ verify (testCommand , never ()).defaultSubCmd (any ());
147+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
148+ verify (testCommand , never ()).testSubCmd (any ());
149+ verify (testCommand , never ()).testSubCmdAlias (any ());
150+ verify (testCommand , never ()).argSubCmd (any (), any ());
151+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
152+ verify (testCommand ).argSubCmd ("apik007" , "rng" , "unneeded" );
153+ reset (testCommand );
154+
155+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"required" , "rng" }));
156+ verify (testCommand , never ()).defaultSubCmd (any ());
157+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
158+ verify (testCommand , never ()).testSubCmd (any ());
159+ verify (testCommand , never ()).testSubCmdAlias (any ());
160+ verify (testCommand , never ()).argSubCmd (any (), any ());
161+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
162+ verify (testCommand , never ()).argSubCmdReq (any (), any (), any ());
163+ reset (testCommand );
164+
165+ assertTrue (testCommandAPI .onCommand (testCommandSenderWrapper , "cmd" , new String []{"required" , "rng" , "needed" }));
166+ verify (testCommand , never ()).defaultSubCmd (any ());
167+ verify (testCommand , never ()).defaultSubCmd (any (), any (), any ());
168+ verify (testCommand , never ()).testSubCmd (any ());
169+ verify (testCommand , never ()).testSubCmdAlias (any ());
170+ verify (testCommand , never ()).argSubCmd (any (), any ());
171+ verify (testCommand , never ()).argSubCmd (any (), any (), any ());
172+ verify (testCommand ).argSubCmdReq ("apik007" , "rng" , "needed" );
173+ reset (testCommand );
174+ }
175+
176+ @ Test
177+ void tabComplete () {
178+ List <String > autoComplete = testCommandAPI .tabComplete (testCommandSenderWrapper , "cmd" , new String []{});
179+ assertTrue (autoComplete .contains ("test" ));
180+ assertTrue (autoComplete .contains ("arg" ));
181+ assertTrue (autoComplete .contains ("non-required" ));
182+ assertTrue (autoComplete .contains ("required" ));
183+ assertTrue (autoComplete .contains ("test_alias" ));
184+ assertTrue (autoComplete .contains ("test_alias2" ));
185+ assertEquals (6 , autoComplete .size ());
186+
187+ autoComplete = testCommandAPI .tabComplete (testCommandSenderWrapper , "cmd" , new String []{"req" });
188+ assertTrue (autoComplete .contains ("required" ));
189+ assertEquals (1 , autoComplete .size ());
190+
191+ autoComplete = testCommandAPI .tabComplete (testCommandSenderWrapper , "cmd" , new String []{"required" , "test" , "t" });
192+ assertTrue (autoComplete .contains ("test2" ));
193+ assertEquals (1 , autoComplete .size ());
194+ }
195+ }
0 commit comments