Skip to content

Commit 753622b

Browse files
committed
Fixed broken tests
1 parent cb0ad1b commit 753622b

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

src/test/java/me/despical/commandframework/test/CommandFrameworkMock.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
/**
2626
* Mocked class of {@link CommandFramework}.
27+
*
2728
* @author gamerover98
29+
* @author Despical
2830
*/
2931
public class CommandFrameworkMock extends CommandFramework {
3032

@@ -35,6 +37,6 @@ public CommandFrameworkMock(@NotNull Plugin plugin) {
3537
* There is no SimplePluginManager class in MockBukkit,
3638
* so the commandMap can be easily obtained from the ServerMock instance.
3739
*/
38-
this.commandMap = plugin.getServer().getCommandMap();
40+
super.setCommandMap(plugin.getServer().getCommandMap());
3941
}
4042
}

src/test/java/me/despical/commandframework/test/CommandRegistrationTest.java

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,32 @@
2323
import be.seeseemelk.mockbukkit.ServerMock;
2424
import be.seeseemelk.mockbukkit.entity.PlayerMock;
2525
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;
2630
import org.bukkit.command.CommandSender;
2731
import org.jetbrains.annotations.NotNull;
2832
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeAll;
2934
import org.junit.jupiter.api.BeforeEach;
3035
import org.junit.jupiter.api.Test;
3136

3237
import java.util.Arrays;
3338
import java.util.List;
34-
import java.util.logging.Logger;
3539

3640
import static org.junit.jupiter.api.Assertions.assertEquals;
3741

3842
/**
3943
* Command registration and usage test class.
4044
* <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>
4246
* </p>
47+
*
4348
* @author gamerover98
49+
* @author Despical
4450
*/
4551
class CommandRegistrationTest {
46-
// TODO - Fix outdated tests.
4752

4853
/**
4954
* The {@link org.bukkit.Server} mocked instance.
@@ -55,8 +60,14 @@ class CommandRegistrationTest {
5560
*/
5661
private MockPlugin plugin;
5762

63+
@BeforeAll
64+
static void beforeAll() {
65+
System.setProperty("commandframework.suppressrelocation", "true");
66+
System.setProperty("commandframework.suppress_initialization", "true");
67+
}
68+
5869
@BeforeEach
59-
public void setUp() {
70+
void setUp() {
6071
server = MockBukkit.mock();
6172
plugin = MockBukkit.createMockPlugin();
6273
}
@@ -67,7 +78,7 @@ public void setUp() {
6778
@Test
6879
void testCommandRegistration() {
6980
CommandFramework commandFramework = createCommandFramework();
70-
assertEquals(3, commandFramework.getCommands().size());
81+
assertEquals(12, commandFramework.getCommands().size());
7182
}
7283

7384
/**
@@ -82,8 +93,8 @@ void testCommandExecutionByPlayer() {
8293
player.setOp(true);
8394

8495
// no params
85-
// player.performCommand("example");
86-
// player.assertSaid(CommandFramework.SHORT_ARG_SIZE);
96+
player.performCommand("example");
97+
player.assertSaid("/example");
8798

8899
// one param
89100
player.performCommand("example firstParam");
@@ -94,19 +105,27 @@ void testCommandExecutionByPlayer() {
94105
player.assertSaid("/example");
95106

96107
// first alias
97-
// player.performCommand("firstAlias");
98-
// player.assertSaid(CommandFramework.SHORT_ARG_SIZE);
108+
player.performCommand("firstAlias");
109+
player.assertSaid("/example");
99110

100111
// second alias
101-
// player.performCommand("secondAlias");
102-
// player.assertSaid(CommandFramework.SHORT_ARG_SIZE);
112+
player.performCommand("secondAlias");
113+
player.assertSaid("/example");
103114

104115
// no command arguments
105116
player.performCommand("nocommandargs");
117+
server.getConsoleSender().assertSaid("This command is running without any parameters.");
106118

107119
// custom parameters
108120
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!");
110129
}
111130

112131
@AfterEach
@@ -122,14 +141,15 @@ private CommandFramework createCommandFramework() {
122141
CommandFramework commandFramework = new CommandFrameworkMock(plugin);
123142
commandFramework.registerCommands(new ExampleCommand());
124143
commandFramework.addCustomParameter("String", arguments -> arguments.getArgument(0));
144+
commandFramework.enableOption(Option.CUSTOM_COOLDOWN_CHECKER);
125145
return commandFramework;
126146
}
127147

128148
/**
129149
* Example command class like the
130150
* <a href="https://github.com/Despical/CommandFramework/wiki/Command-examples#example-usage">wiki one</a>
131151
*/
132-
public static class ExampleCommand {
152+
public class ExampleCommand {
133153

134154
@Command(
135155
name = "example",
@@ -150,25 +170,33 @@ public void exampleCommandMethod(CommandArguments arguments) {
150170
name = "nocommandargs"
151171
)
152172
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.");
154174
}
155175

156176
@Command(
157177
name = "customargs",
158178
min = 1
159179
)
160180
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.");
165193
}
166194

167195
@Completer(
168196
name = "example",
169197
aliases = {"firstAlias", "secondAlias"}
170198
)
171-
public List<String> exampleCommandCompletion(/*CommandArguments arguments*/ /*no need to use in this case which is also supported*/) {
199+
public List<String> exampleCommandCompletion() {
172200
return Arrays.asList("first", "second", "third");
173201
}
174202
}

0 commit comments

Comments
 (0)