-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow developers to load custom MockCommandAPIBukkit instances in the…
…ir tests This makes it possible to avoid UnimplementedMethodExceptions by overriding and implementing the method yourself NOTE: CommandAPIVersionHandler was changed from an interface to an abstract class so the test-toolkit version could have a non-final field. I don't think this affects anything else.
- Loading branch information
1 parent
525f12d
commit f71519e
Showing
13 changed files
with
143 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 23 additions & 3 deletions
26
...dapi-bukkit-test-toolkit/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
package dev.jorel.commandapi; | ||
|
||
public interface CommandAPIVersionHandler { | ||
static CommandAPIPlatform<?, ?, ?> getPlatform() { | ||
return new MockCommandAPIBukkit(); | ||
public abstract class CommandAPIVersionHandler { | ||
// Allow loading a different platform implementation (most likely to implement something `MockCommandAPIBukkit` doesn't) | ||
private static CommandAPIPlatform<?, ?, ?> alternativePlatform = null; | ||
|
||
/** | ||
* Configures the test kit to use the given {@link CommandAPIPlatform} when the CommandAPI is loaded. | ||
* | ||
* @param platform The {@link CommandAPIPlatform} to use for the next test. This will likely be a custom | ||
* implementation of {@link MockCommandAPIBukkit} that overrides a method you need to run | ||
* tests that doesn't have a proper implementation in {@link MockCommandAPIBukkit}. | ||
*/ | ||
public static void usePlatformImplementation(CommandAPIPlatform<?, ?, ?> platform) { | ||
alternativePlatform = platform; | ||
} | ||
|
||
static LoadContext getPlatform() { | ||
// Default to MockCommandAPIBukkit if not given | ||
CommandAPIPlatform<?, ?, ?> platform = alternativePlatform == null ? new MockCommandAPIBukkit() : alternativePlatform; | ||
|
||
// Reset to avoid platform persisting between tests | ||
alternativePlatform = null; | ||
|
||
return new LoadContext(platform); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...t-toolkit/src/test/java/dev/jorel/commandapi/CommandAPIVersionHandlerOverridingTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package dev.jorel.commandapi; | ||
|
||
import be.seeseemelk.mockbukkit.MockBukkit; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
/** | ||
* Tests for using {@link CommandAPIVersionHandler#usePlatformImplementation(CommandAPIPlatform)} | ||
*/ | ||
class CommandAPIVersionHandlerOverridingTests { | ||
// Setup | ||
@BeforeEach | ||
public void setUp() { | ||
MockBukkit.mock(); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
MockBukkit.unmock(); | ||
} | ||
|
||
private static class CustomMockCommandAPIBukkit extends MockCommandAPIBukkit { | ||
|
||
} | ||
|
||
// Tests | ||
@Test | ||
void testDefaultPlatform() { | ||
MockCommandAPIPlugin.load(); | ||
|
||
assertEquals(MockCommandAPIBukkit.class, CommandAPITestUtilities.getCommandAPIPlatform().getClass()); | ||
} | ||
|
||
@Test | ||
void testChangingPlatform() { | ||
CommandAPIVersionHandler.usePlatformImplementation(new CustomMockCommandAPIBukkit()); | ||
MockCommandAPIPlugin.load(); | ||
|
||
assertEquals(CustomMockCommandAPIBukkit.class, CommandAPITestUtilities.getCommandAPIPlatform().getClass()); | ||
} | ||
|
||
@Test | ||
void testPlatformDoesNotPersist() { | ||
CommandAPIVersionHandler.usePlatformImplementation(new CustomMockCommandAPIBukkit()); | ||
MockCommandAPIPlugin.load(); | ||
|
||
assertEquals(CustomMockCommandAPIBukkit.class, CommandAPITestUtilities.getCommandAPIPlatform().getClass()); | ||
|
||
MockBukkit.unmock(); | ||
MockBukkit.mock(); | ||
|
||
MockCommandAPIPlugin.load(); | ||
assertEquals(MockCommandAPIBukkit.class, CommandAPITestUtilities.getCommandAPIPlatform().getClass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e/commandapi-sponge-core/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...commandapi-velocity-core/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters