-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
352 additions
and
7 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,5 @@ pluginManagement { | |
gradlePluginPortal() | ||
} | ||
} | ||
|
||
rootProject.name = 'elmendorf' |
66 changes: 66 additions & 0 deletions
66
src/main/java/io/github/ladysnake/elmendorf/ConnectionChecker.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,66 @@ | ||
/* | ||
* Copyright (C) 2021 Ladysnake | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package io.github.ladysnake.elmendorf; | ||
|
||
import net.minecraft.network.Packet; | ||
import net.minecraft.util.Identifier; | ||
|
||
import java.util.List; | ||
import java.util.Queue; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
public interface ConnectionChecker { | ||
PacketChecker sent(Class<? extends Packet<?>> packetType); | ||
|
||
PacketChecker sent(Identifier channelId); | ||
|
||
PacketChecker sent(Predicate<Packet<?>> test, String errorMessage); | ||
|
||
void sentPackets(Consumer<Queue<Packet<?>>> test); | ||
|
||
final class PacketChecker { | ||
private final List<Packet<?>> packets; | ||
private final String defaultErrorMessage; | ||
|
||
public PacketChecker(List<Packet<?>> packets, String defaultErrorMessage) { | ||
this.packets = packets; | ||
this.defaultErrorMessage = defaultErrorMessage; | ||
} | ||
|
||
public void atLeast(int times) { | ||
GameTestUtil.assertTrue("%s to be sent at least %d times, was %d".formatted(defaultErrorMessage, times, this.packets.size()), this.packets.size() >= times); | ||
} | ||
|
||
public void atLeast(String errorMessage, int times) { | ||
GameTestUtil.assertTrue(errorMessage, this.packets.size() >= times); | ||
} | ||
|
||
public void exactly(int times) { | ||
GameTestUtil.assertTrue("%s to be sent %d times, was %d".formatted(defaultErrorMessage, times, this.packets.size()), this.packets.size() == times); | ||
} | ||
|
||
public void exactly(int times, String errorMessage) { | ||
GameTestUtil.assertTrue(errorMessage, this.packets.size() == times); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/github/ladysnake/elmendorf/ConnectionTestConfiguration.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,26 @@ | ||
/* | ||
* Copyright (C) 2021 Ladysnake | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package io.github.ladysnake.elmendorf; | ||
|
||
public interface ConnectionTestConfiguration { | ||
void toFlushPacketsEachTick(); | ||
} |
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
101 changes: 101 additions & 0 deletions
101
src/main/java/io/github/ladysnake/elmendorf/impl/MockClientConnection.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,101 @@ | ||
/* | ||
* Copyright (C) 2021 Ladysnake | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package io.github.ladysnake.elmendorf.impl; | ||
|
||
import io.github.ladysnake.elmendorf.ConnectionChecker; | ||
import io.github.ladysnake.elmendorf.ConnectionTestConfiguration; | ||
import io.github.ladysnake.elmendorf.GameTestUtil; | ||
import io.netty.util.concurrent.Future; | ||
import io.netty.util.concurrent.GenericFutureListener; | ||
import net.minecraft.network.ClientConnection; | ||
import net.minecraft.network.NetworkSide; | ||
import net.minecraft.network.Packet; | ||
import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Queue; | ||
import java.util.function.Consumer; | ||
import java.util.function.Predicate; | ||
|
||
public class MockClientConnection extends ClientConnection implements ConnectionChecker, ConnectionTestConfiguration { | ||
private final Queue<Packet<?>> packetQueue = new ArrayDeque<>(); | ||
private boolean flushEachTick; | ||
|
||
public MockClientConnection(NetworkSide side) { | ||
super(side); | ||
} | ||
|
||
@Override | ||
public void toFlushPacketsEachTick() { | ||
this.flushEachTick = true; | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
super.tick(); | ||
if (this.flushEachTick) { | ||
this.packetQueue.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public PacketChecker sent(Class<? extends Packet<?>> packetType) { | ||
return sent(packetType::isInstance, "Expected packet of type " + packetType.getTypeName()); | ||
} | ||
|
||
@Override | ||
public PacketChecker sent(Identifier channelId) { | ||
return sent(packet -> packet instanceof CustomPayloadS2CPacket p && Objects.equals(p.getChannel(), channelId), "Expected packet for channel " + channelId); | ||
} | ||
|
||
@Override | ||
public PacketChecker sent(Predicate<Packet<?>> test, String errorMessage) { | ||
List<Packet<?>> packets = this.packetQueue.stream().filter(test).toList(); | ||
GameTestUtil.assertFalse(errorMessage, packets.isEmpty()); | ||
return new PacketChecker(packets, errorMessage); | ||
} | ||
|
||
@Override | ||
public void sentPackets(Consumer<Queue<Packet<?>>> test) { | ||
test.accept(this.packetQueue); | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void send(Packet<?> packet, @Nullable GenericFutureListener<? extends Future<? super Void>> callback) { | ||
this.packetQueue.add(packet); | ||
} | ||
|
||
public Queue<Packet<?>> getPacketQueue() { | ||
return packetQueue; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/testmod/java/io/github/ladysnake/ripstop/RipstopTestSuite.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,43 @@ | ||
/* | ||
* Copyright (C) 2021 Ladysnake | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package io.github.ladysnake.ripstop; | ||
|
||
import io.github.ladysnake.elmendorf.GameTestUtil; | ||
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest; | ||
import net.minecraft.network.packet.s2c.play.ClearTitleS2CPacket; | ||
import net.minecraft.test.GameTest; | ||
import net.minecraft.test.GameTestException; | ||
import net.minecraft.test.TestContext; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class RipstopTestSuite implements FabricGameTest { | ||
@GameTest(structureName = EMPTY_STRUCTURE) | ||
public void test(TestContext ctx) { | ||
var player = GameTestUtil.spawnPlayer(ctx, 5, 5, 5); | ||
player.networkHandler.sendPacket(new ClearTitleS2CPacket(true)); | ||
player.networkHandler.sendPacket(new ClearTitleS2CPacket(false)); | ||
GameTestUtil.verifyConnection(player).sent(ClearTitleS2CPacket.class).atLeast(2); | ||
GameTestUtil.assertThrows(GameTestException.class, | ||
() -> GameTestUtil.verifyConnection(player).sent(new Identifier("ribbit"))); | ||
ctx.complete(); | ||
} | ||
} |
Oops, something went wrong.