Skip to content

Commit c53e451

Browse files
authored
Merge pull request CommandAPI#575 from JorelAli/dev/simple-test-suite
NMS independent test toolkit to allow developers to test their commands with MockBukkit
2 parents b38936a + e000a4b commit c53e451

File tree

63 files changed

+6002
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+6002
-13
lines changed

commandapi-codecov/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<version>${project.version}</version>
5959
<scope>test</scope>
6060
</dependency>
61+
<dependency>
62+
<groupId>dev.jorel</groupId>
63+
<artifactId>commandapi-bukkit-test-toolkit</artifactId>
64+
<version>${project.version}</version>
65+
<!-- This should not have the test scope because it contains classes whose coverage we want to analyze -->
66+
</dependency>
6167

6268
<!-- From commandapi-bukkit-nms-dependency-mojang-mapped -->
6369

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ public CommandAPIPlatform<Argument, CommandSender, Source> getPlatform() {
185185
* @param executor code to be ran when the command is executed
186186
* @param converted True if this command is being converted from another plugin, and false otherwise
187187
* @return a brigadier command which is registered internally
188-
* @throws CommandSyntaxException if an error occurs when the command is ran
189188
*/
190-
Command<Source> generateCommand(Argument[] args, CommandAPIExecutor<CommandSender, AbstractCommandSender<? extends CommandSender>> executor, boolean converted) {
189+
public Command<Source> generateCommand(Argument[] args, CommandAPIExecutor<CommandSender, AbstractCommandSender<? extends CommandSender>> executor, boolean converted) {
191190

192191
// Generate our command from executor
193192
return cmdCtx -> {

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIPlatform.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ public interface CommandAPIPlatform<Argument
136136
public abstract void createDispatcherFile(File file, CommandDispatcher<Source> dispatcher) throws IOException;
137137

138138
/**
139-
* @return A new default Logger meant for the CommandAPI to use
139+
* @return A new default Logger meant for the CommandAPI to use. This logger will be used by the CommandAPI
140+
* if one is not set using {@link CommandAPI#setLogger(CommandAPILogger)}.
140141
*/
141142
public default CommandAPILogger getLogger() {
142143
return new CommandAPILogger() {

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* file within the commandapi-core module is NOT used at run time. Instead, the platform modules
66
* replace this class with their own version that handles loads the correct class for their version.
77
*/
8-
public interface CommandAPIVersionHandler {
8+
public abstract class CommandAPIVersionHandler {
99

1010
/**
1111
* Returns an instance of the version's implementation of CommandAPIPlatform.

commandapi-documentation-code/pom.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
<groupId>net.kyori</groupId>
4949
<artifactId>adventure-platform-bukkit</artifactId>
5050
<version>4.2.0</version>
51-
<scope>test</scope>
5251
</dependency>
5352
<dependency>
5453
<groupId>io.papermc.paper</groupId>
@@ -57,6 +56,25 @@
5756
<scope>provided</scope>
5857
</dependency>
5958

59+
<!-- Testing examples dependencies -->
60+
<dependency>
61+
<groupId>com.github.seeseemelk</groupId>
62+
<artifactId>MockBukkit-v1.20</artifactId>
63+
<version>3.9.0</version>
64+
</dependency>
65+
<dependency>
66+
<!-- Note: This is before the other CommandAPI dependencies so example `testLoadMockCommandAPI2`
67+
can resolve the correct instance of the CommandAPIVersionHandler class -->
68+
<groupId>dev.jorel</groupId>
69+
<artifactId>commandapi-bukkit-test-toolkit</artifactId>
70+
<version>${project.version}</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.junit.jupiter</groupId>
74+
<artifactId>junit-jupiter-engine</artifactId>
75+
<version>5.8.2</version>
76+
</dependency>
77+
6078
<!-- CommandAPI -->
6179
<dependency>
6280
<groupId>dev.jorel</groupId>

commandapi-documentation-code/src/main/java/dev/jorel/commandapi/examples/java/Examples.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
*******************************************************************************/
2222

23+
import be.seeseemelk.mockbukkit.MockBukkit;
24+
import be.seeseemelk.mockbukkit.ServerMock;
2325
import com.mojang.brigadier.LiteralMessage;
2426
import com.mojang.brigadier.Message;
2527
import com.mojang.brigadier.ParseResults;
@@ -74,6 +76,8 @@
7476
import org.bukkit.scoreboard.Scoreboard;
7577
import org.bukkit.scoreboard.Team;
7678
import org.bukkit.util.EulerAngle;
79+
import org.junit.jupiter.api.AfterEach;
80+
import org.junit.jupiter.api.BeforeEach;
7781

7882
import java.util.*;
7983
import java.util.Map.Entry;
@@ -2519,6 +2523,60 @@ void subcommands() {
25192523
/* ANCHOR_END: subcommands4 */
25202524
}
25212525

2526+
class test {
2527+
class Main extends JavaPlugin {
2528+
2529+
}
2530+
2531+
class example1 {
2532+
/* ANCHOR: testLoadMockCommandAPI1 */
2533+
@BeforeEach
2534+
public void setUp() {
2535+
// Set up MockBukkit server
2536+
ServerMock server = MockBukkit.mock();
2537+
2538+
// Load the CommandAPI plugin
2539+
MockCommandAPIPlugin.load(config -> config
2540+
.missingExecutorImplementationMessage("This command cannot be run by %S")
2541+
);
2542+
2543+
// Load our plugin
2544+
MockBukkit.load(Main.class);
2545+
}
2546+
2547+
@AfterEach
2548+
public void tearDown() {
2549+
// Reset for a clean slate next test
2550+
MockBukkit.unmock();
2551+
}
2552+
/* ANCHOR_END: testLoadMockCommandAPI1 */
2553+
}
2554+
2555+
class example2 {
2556+
/* ANCHOR: testLoadMockCommandAPI2 */
2557+
public class CustomMockCommandAPIBukkit extends MockCommandAPIBukkit {
2558+
// Implement a method that usually throws `UnimplementedMethodException`
2559+
@Override
2560+
public void reloadDataPacks() {
2561+
CommandAPI.logInfo("Simulating data pack reload");
2562+
// Further logic
2563+
}
2564+
}
2565+
2566+
@BeforeEach
2567+
public void setUp() {
2568+
// Set up MockBukkit server
2569+
MockBukkit.mock();
2570+
2571+
// Tell the CommandAPI to use your custom platform implementation
2572+
CommandAPIVersionHandler.usePlatformImplementation(new CustomMockCommandAPIBukkit());
2573+
2574+
// Load CommandAPI and your plugin as mentioned above...
2575+
}
2576+
/* ANCHOR_END: testLoadMockCommandAPI2 */
2577+
}
2578+
}
2579+
25222580
class tooltips {
25232581
{
25242582
/* ANCHOR: tooltips1 */
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>dev.jorel</groupId>
8+
<artifactId>commandapi-bukkit</artifactId>
9+
<version>9.6.2-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>commandapi-bukkit-test-toolkit</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>21</maven.compiler.source>
16+
<maven.compiler.target>21</maven.compiler.target>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
</properties>
19+
20+
<repositories>
21+
<repository>
22+
<id>minecraft-libraries</id>
23+
<name>Minecraft Libraries</name>
24+
<url>https://libraries.minecraft.net</url>
25+
</repository>
26+
<repository>
27+
<id>papermc</id>
28+
<url>https://repo.papermc.io/repository/maven-public/</url>
29+
</repository>
30+
</repositories>
31+
32+
<dependencies>
33+
<!-- Compatibility with MockBukkit API -->
34+
<!-- This provides transitive access to spigot-api and paper-api
35+
If we listed our own dependency for those, we could put a different version, which often causes problems
36+
Since we aren't just using MockBukkit for tests, we can rely on this dependency for the Spigot/Paper api -->
37+
<dependency>
38+
<groupId>com.github.seeseemelk</groupId>
39+
<artifactId>MockBukkit-v1.21</artifactId>
40+
<version>3.133.2</version>
41+
<scope>provided</scope>
42+
</dependency>
43+
44+
<!-- Compatibility with CommandAPI Bukkit API -->
45+
<dependency>
46+
<groupId>dev.jorel</groupId>
47+
<artifactId>commandapi-bukkit-core</artifactId>
48+
<version>${project.version}</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
<!-- Use Mockito, pass to dependents if they don't provide it themselves -->
53+
<dependency>
54+
<groupId>org.mockito</groupId>
55+
<artifactId>mockito-core</artifactId>
56+
<version>5.11.0</version>
57+
<scope>compile</scope>
58+
</dependency>
59+
60+
<!-- Pass Brigadier to our dependents -->
61+
<dependency>
62+
<groupId>com.mojang</groupId>
63+
<artifactId>brigadier</artifactId>
64+
<version>1.0.18</version>
65+
<scope>compile</scope>
66+
</dependency>
67+
68+
<!-- Passing this to dependents gets rid of a `SLF4J: No SLF4J providers were found` warning -->
69+
<!-- https://stackoverflow.com/questions/54652836/found-slf4j-api-dependency-but-no-providers-were-found -->
70+
<!-- This version matches the slf4j-api declared by our dependency on paper-api through MockBukkit-1.21 -->
71+
<dependency>
72+
<groupId>org.slf4j</groupId>
73+
<artifactId>slf4j-nop</artifactId>
74+
<version>2.0.9</version>
75+
<scope>compile</scope>
76+
</dependency>
77+
78+
<!-- Run our own tests with JUnit API -->
79+
<!-- Note: This version matches the artifactId `junit-jupiter` dependency transitively inherited
80+
from `MockBukkit-v1.21`. If it doesn't match, the tests might fail with class not found errors. -->
81+
<dependency>
82+
<groupId>org.junit.jupiter</groupId>
83+
<artifactId>junit-jupiter-engine</artifactId>
84+
<version>5.11.0</version>
85+
<scope>test</scope>
86+
</dependency>
87+
</dependencies>
88+
89+
<build>
90+
<!-- Fill ${project.version} in plugin.yml -->
91+
<resources>
92+
<resource>
93+
<directory>src/main/resources</directory>
94+
<filtering>true</filtering>
95+
</resource>
96+
</resources>
97+
98+
<!-- Remove commandapi-core's VersionHandler in favor of ours -->
99+
<plugins>
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-shade-plugin</artifactId>
103+
<executions>
104+
<execution>
105+
<phase>package</phase>
106+
<goals>
107+
<goal>shade</goal>
108+
</goals>
109+
<configuration>
110+
<filters>
111+
<filter>
112+
<artifact>dev.jorel:commandapi-core</artifact>
113+
<excludes>
114+
<exclude>dev/jorel/commandapi/CommandAPIVersionHandler**</exclude>
115+
</excludes>
116+
</filter>
117+
</filters>
118+
<transformers>
119+
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
120+
<resource>LICENSE</resource>
121+
</transformer>
122+
</transformers>
123+
</configuration>
124+
</execution>
125+
</executions>
126+
</plugin>
127+
128+
<!-- Run our own tests -->
129+
<plugin>
130+
<groupId>org.apache.maven.plugins</groupId>
131+
<artifactId>maven-surefire-plugin</artifactId>
132+
<version>3.3.0</version>
133+
<configuration>
134+
<trimStackTrace>false</trimStackTrace>
135+
<!-- To help with debugging tests, you can specify an include
136+
to limit a build to only run a certain test file. Uncomment
137+
here if you want to do that: -->
138+
<!-- <includes>-->
139+
<!-- <include>**/DispatchCommandUtilitiesTests.java</include>-->
140+
<!-- </includes>-->
141+
</configuration>
142+
</plugin>
143+
144+
<!-- Run code coverage report -->
145+
<plugin>
146+
<groupId>org.jacoco</groupId>
147+
<artifactId>jacoco-maven-plugin</artifactId>
148+
<executions>
149+
<!-- to avoid bugs in some situations -->
150+
<execution>
151+
<id>default-prepare-agent</id>
152+
<goals>
153+
<goal>prepare-agent</goal>
154+
</goals>
155+
</execution>
156+
<!-- create report during maven verify phase -->
157+
<execution>
158+
<id>report</id>
159+
<phase>verify</phase>
160+
<goals>
161+
<goal>report</goal>
162+
</goals>
163+
</execution>
164+
</executions>
165+
</plugin>
166+
</plugins>
167+
</build>
168+
</project>

0 commit comments

Comments
 (0)