From bab52ee85d32b30b07a9f186b3b25c4a72c5cd62 Mon Sep 17 00:00:00 2001 From: Jeff Smith Date: Tue, 23 Jan 2024 01:36:12 -0600 Subject: [PATCH] Fix tests --- .../turtle/upgrades/ToolPeripheral.java | 7 +- .../upgrades/TurtleToolSerialiserTest.java | 99 ++++++++++++++++--- 2 files changed, 90 insertions(+), 16 deletions(-) diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/ToolPeripheral.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/ToolPeripheral.java index d9d6c7d7d..bb0007671 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/ToolPeripheral.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/upgrades/ToolPeripheral.java @@ -1,8 +1,5 @@ package dan200.computercraft.shared.turtle.upgrades; - -import javax.annotation.Nullable; - import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; import dan200.computercraft.api.lua.MethodResult; @@ -12,7 +9,9 @@ import dan200.computercraft.shared.turtle.core.InteractDirection; import dan200.computercraft.shared.turtle.core.TurtleToolCommand; -public class ToolPeripheral implements IPeripheral{ +import javax.annotation.Nullable; + +public class ToolPeripheral implements IPeripheral { private final ITurtleAccess turtle; private final TurtleSide side; private final String type; diff --git a/projects/common/src/test/java/dan200/computercraft/shared/turtle/upgrades/TurtleToolSerialiserTest.java b/projects/common/src/test/java/dan200/computercraft/shared/turtle/upgrades/TurtleToolSerialiserTest.java index 338acdc1c..e95d87f16 100644 --- a/projects/common/src/test/java/dan200/computercraft/shared/turtle/upgrades/TurtleToolSerialiserTest.java +++ b/projects/common/src/test/java/dan200/computercraft/shared/turtle/upgrades/TurtleToolSerialiserTest.java @@ -4,6 +4,18 @@ package dan200.computercraft.shared.turtle.upgrades; +import net.jqwik.api.Arbitraries; +import net.jqwik.api.Arbitrary; +import net.jqwik.api.Builders; +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; +import net.jqwik.api.Provide; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.block.Block; import dan200.computercraft.api.turtle.ITurtleUpgrade; import dan200.computercraft.api.turtle.TurtleToolDurability; import dan200.computercraft.test.core.StructuralEquality; @@ -11,8 +23,8 @@ import dan200.computercraft.test.shared.MinecraftEqualities; import dan200.computercraft.test.shared.NetworkSupport; import dan200.computercraft.test.shared.WithMinecraft; -import net.jqwik.api.*; -import net.minecraft.core.registries.Registries; + +import javax.annotation.Nullable; import static org.hamcrest.MatcherAssert.assertThat; @@ -22,6 +34,67 @@ class TurtleToolSerialiserTest { WithMinecraft.Setup.bootstrap(); // @Property doesn't run test lifecycle methods. } + static class TurtleToolBuilder { + ResourceLocation id; + String adjective; + Item craftItem; + ItemStack toolItem; + float damageMulitiplier; + boolean allowEnchantments; + TurtleToolDurability consumeDurability; + @Nullable TagKey breakable; + String peripheralType; + + public TurtleToolBuilder withId(ResourceLocation id) { + this.id = id; + return this; + } + + public TurtleToolBuilder withAdjective(String adjective) { + this.adjective = adjective; + return this; + } + + public TurtleToolBuilder withCraftItem(Item craftItem) { + this.craftItem = craftItem; + return this; + } + + public TurtleToolBuilder withToolItem(ItemStack toolItem) { + this.toolItem = toolItem; + return this; + } + + public TurtleToolBuilder withDamageMultiplier(float damageMulitiplier) { + this.damageMulitiplier = damageMulitiplier; + return this; + } + + public TurtleToolBuilder withAllowEnchantments(boolean allowEnchantments) { + this.allowEnchantments = allowEnchantments; + return this; + } + + public TurtleToolBuilder withConsumeDurability(TurtleToolDurability consumeDurability) { + this.consumeDurability = consumeDurability; + return this; + } + + public TurtleToolBuilder withBreakable(@Nullable TagKey breakable) { + this.breakable = breakable; + return this; + } + + public TurtleToolBuilder withPeripheralType(String peripheralType) { + this.peripheralType = peripheralType; + return this; + } + + public TurtleTool build() { + return new TurtleTool(id, adjective, craftItem, toolItem, damageMulitiplier, allowEnchantments, consumeDurability, breakable, peripheralType); + } + } + /** * Sends turtle tools on a roundtrip, ensuring that their contents are reassembled on the other end. * @@ -41,16 +114,18 @@ public void testRoundTrip(@ForAll("tool") TurtleTool tool) { @Provide Arbitrary tool() { - return Combinators.combine( - MinecraftArbitraries.resourceLocation(), - Arbitraries.strings().ofMaxLength(100), - MinecraftArbitraries.item(), - MinecraftArbitraries.itemStack(), - Arbitraries.floats(), - Arbitraries.of(true, false), - Arbitraries.of(TurtleToolDurability.values()), - MinecraftArbitraries.tagKey(Registries.BLOCK) - ).as(TurtleTool::new); + + return Builders.withBuilder(() -> new TurtleToolBuilder()) + .use(MinecraftArbitraries.resourceLocation()).in((builder, resourceLocation) -> builder.withId(resourceLocation)) + .use(Arbitraries.strings().ofMaxLength(100)).in((builder, adjective) -> builder.withAdjective(adjective)) + .use(MinecraftArbitraries.item()).in((builder, craftItem) -> builder.withCraftItem(craftItem)) + .use(MinecraftArbitraries.itemStack()).in((builder, toolItem) -> builder.withToolItem(toolItem)) + .use(Arbitraries.floats()).in((builder, damageMulitiplier) -> builder.withDamageMultiplier(damageMulitiplier)) + .use(Arbitraries.of(true, false)).in((builder, allowEnchantments) -> builder.withAllowEnchantments(allowEnchantments)) + .use(Arbitraries.of(TurtleToolDurability.values())).in((builder, consumeDurability) -> builder.withConsumeDurability(consumeDurability)) + .use(MinecraftArbitraries.tagKey(Registries.BLOCK)).in((builder, breakable) -> builder.withBreakable(breakable)) + .use(Arbitraries.strings().ofMaxLength(100)).in((builder, peripheralType) -> builder.withPeripheralType(peripheralType)) + .build(builder -> builder.build()); } private static final StructuralEquality equality = StructuralEquality.all(