23
23
import be .seeseemelk .mockbukkit .ServerMock ;
24
24
import be .seeseemelk .mockbukkit .entity .PlayerMock ;
25
25
import me .despical .commandframework .*;
26
+ import me .despical .commandframework .annotations .Command ;
27
+ import me .despical .commandframework .annotations .Completer ;
28
+ import me .despical .commandframework .annotations .Cooldown ;
29
+ import me .despical .commandframework .options .Option ;
26
30
import org .bukkit .command .CommandSender ;
27
31
import org .jetbrains .annotations .NotNull ;
28
32
import org .junit .jupiter .api .AfterEach ;
33
+ import org .junit .jupiter .api .BeforeAll ;
29
34
import org .junit .jupiter .api .BeforeEach ;
30
35
import org .junit .jupiter .api .Test ;
31
36
32
37
import java .util .Arrays ;
33
38
import java .util .List ;
34
- import java .util .logging .Logger ;
35
39
36
40
import static org .junit .jupiter .api .Assertions .assertEquals ;
37
41
38
42
/**
39
43
* Command registration and usage test class.
40
44
* <p>
41
- * Implemented with <a href="https://github.com/MockBukkit/MockBukkit">MockBukkit</a>
45
+ * Implemented with <a href="https://github.com/MockBukkit/MockBukkit">MockBukkit</a>
42
46
* </p>
47
+ *
43
48
* @author gamerover98
49
+ * @author Despical
44
50
*/
45
51
class CommandRegistrationTest {
46
- // TODO - Fix outdated tests.
47
52
48
53
/**
49
54
* The {@link org.bukkit.Server} mocked instance.
@@ -55,8 +60,14 @@ class CommandRegistrationTest {
55
60
*/
56
61
private MockPlugin plugin ;
57
62
63
+ @ BeforeAll
64
+ static void beforeAll () {
65
+ System .setProperty ("commandframework.suppressrelocation" , "true" );
66
+ System .setProperty ("commandframework.suppress_initialization" , "true" );
67
+ }
68
+
58
69
@ BeforeEach
59
- public void setUp () {
70
+ void setUp () {
60
71
server = MockBukkit .mock ();
61
72
plugin = MockBukkit .createMockPlugin ();
62
73
}
@@ -67,7 +78,7 @@ public void setUp() {
67
78
@ Test
68
79
void testCommandRegistration () {
69
80
CommandFramework commandFramework = createCommandFramework ();
70
- assertEquals (3 , commandFramework .getCommands ().size ());
81
+ assertEquals (12 , commandFramework .getCommands ().size ());
71
82
}
72
83
73
84
/**
@@ -82,8 +93,8 @@ void testCommandExecutionByPlayer() {
82
93
player .setOp (true );
83
94
84
95
// no params
85
- // player.performCommand("example");
86
- // player.assertSaid(CommandFramework.SHORT_ARG_SIZE );
96
+ player .performCommand ("example" );
97
+ player .assertSaid ("/example" );
87
98
88
99
// one param
89
100
player .performCommand ("example firstParam" );
@@ -94,19 +105,27 @@ void testCommandExecutionByPlayer() {
94
105
player .assertSaid ("/example" );
95
106
96
107
// first alias
97
- // player.performCommand("firstAlias");
98
- // player.assertSaid(CommandFramework.SHORT_ARG_SIZE );
108
+ player .performCommand ("firstAlias" );
109
+ player .assertSaid ("/example" );
99
110
100
111
// second alias
101
- // player.performCommand("secondAlias");
102
- // player.assertSaid(CommandFramework.SHORT_ARG_SIZE );
112
+ player .performCommand ("secondAlias" );
113
+ player .assertSaid ("/example" );
103
114
104
115
// no command arguments
105
116
player .performCommand ("nocommandargs" );
117
+ server .getConsoleSender ().assertSaid ("This command is running without any parameters." );
106
118
107
119
// custom parameters
108
120
player .performCommand ("customargs test" );
109
- player .assertSaid ("First parameter is test" );
121
+ player .assertSaid ("First parameter is test." );
122
+
123
+ // cooldown command
124
+ player .performCommand ("cooldown" );
125
+ player .assertSaid ("Cooldown command message." );
126
+
127
+ player .performCommand ("cooldown" );
128
+ player .assertSaid ("§cYou have to wait before using this command again!" );
110
129
}
111
130
112
131
@ AfterEach
@@ -122,14 +141,15 @@ private CommandFramework createCommandFramework() {
122
141
CommandFramework commandFramework = new CommandFrameworkMock (plugin );
123
142
commandFramework .registerCommands (new ExampleCommand ());
124
143
commandFramework .addCustomParameter ("String" , arguments -> arguments .getArgument (0 ));
144
+ commandFramework .enableOption (Option .CUSTOM_COOLDOWN_CHECKER );
125
145
return commandFramework ;
126
146
}
127
147
128
148
/**
129
149
* Example command class like the
130
150
* <a href="https://github.com/Despical/CommandFramework/wiki/Command-examples#example-usage">wiki one</a>
131
151
*/
132
- public static class ExampleCommand {
152
+ public class ExampleCommand {
133
153
134
154
@ Command (
135
155
name = "example" ,
@@ -150,25 +170,33 @@ public void exampleCommandMethod(CommandArguments arguments) {
150
170
name = "nocommandargs"
151
171
)
152
172
public void noCommandArgsTest () {
153
- Logger . getLogger ( this . getClass (). getSimpleName ()). info ("This command is running without any parameters." );
173
+ server . getConsoleSender (). sendMessage ("This command is running without any parameters." );
154
174
}
155
175
156
176
@ Command (
157
177
name = "customargs" ,
158
178
min = 1
159
179
)
160
180
public void customParamCommand (String firstParameter , CommandArguments arguments ) {
161
- CommandSender sender = arguments .getSender ();
162
- // Check if arguments are empty; otherwise, firstParameter will return null.
163
- // CommandArguments parameter can be added to anywhere in method as a parameter.
164
- sender .sendMessage ("First parameter is " + firstParameter );
181
+ arguments .sendMessage ("First parameter is " + firstParameter + "." );
182
+ }
183
+
184
+ @ Command (
185
+ name = "cooldown"
186
+ )
187
+ @ Cooldown (
188
+ cooldown = 5
189
+ )
190
+ public void cooldownTest (CommandArguments arguments ) {
191
+ arguments .checkCooldown ();
192
+ arguments .sendMessage ("Cooldown command message." );
165
193
}
166
194
167
195
@ Completer (
168
196
name = "example" ,
169
197
aliases = {"firstAlias" , "secondAlias" }
170
198
)
171
- public List <String > exampleCommandCompletion (/*CommandArguments arguments*/ /*no need to use in this case which is also supported*/ ) {
199
+ public List <String > exampleCommandCompletion () {
172
200
return Arrays .asList ("first" , "second" , "third" );
173
201
}
174
202
}
0 commit comments