From b3ac5fe26b878f6a7b3844f7c2ee43cfedd41515 Mon Sep 17 00:00:00 2001 From: copokbl Date: Fri, 28 Jun 2024 15:25:31 +1000 Subject: [PATCH] Full command unit test coverage --- .../estools/Commands/DismountCommandTest.java | 40 ++++++ .../estools/Commands/PotionCommandTest.java | 116 ++++++++++++++++++ .../estools/Implementation/TestEntity.java | 3 + .../estools/Implementation/TestPotion.java | 36 ++++++ .../estools/Implementation/TestServer.java | 3 +- 5 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/estools/Commands/DismountCommandTest.java create mode 100644 src/test/java/net/estools/Commands/PotionCommandTest.java create mode 100644 src/test/java/net/estools/Implementation/TestPotion.java diff --git a/src/test/java/net/estools/Commands/DismountCommandTest.java b/src/test/java/net/estools/Commands/DismountCommandTest.java new file mode 100644 index 0000000..f9db842 --- /dev/null +++ b/src/test/java/net/estools/Commands/DismountCommandTest.java @@ -0,0 +1,40 @@ +package net.estools.Commands; + +import net.estools.EsToolsUnitTest; +import net.estools.Implementation.TestPlayer; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +// /dismount [rider1] [rider2]... +public class DismountCommandTest extends EsToolsUnitTest { + + @Test + public void noArg() { + executeAssertOneError("dismount"); + } + + @Test + public void invalidArg() { + executeAssertOneError("dismount invalid"); + } + + @Test + public void dismountFromSelf() { + TestPlayer p2 = createPlayer(); + p2.getPassengers().clear(); + player.addPassenger(p2); + executeAssertSuccess("dismount " + p2.getName()); + + Assertions.assertFalse(player.getPassengers().contains(p2)); + } + + @Test + public void dismountSelf() { + TestPlayer ridee = createPlayer(); + ridee.addPassenger(player); + + executeAssertSuccess("dismount"); + + Assertions.assertFalse(ridee.getPassengers().contains(player)); + } +} diff --git a/src/test/java/net/estools/Commands/PotionCommandTest.java b/src/test/java/net/estools/Commands/PotionCommandTest.java new file mode 100644 index 0000000..c7b58f6 --- /dev/null +++ b/src/test/java/net/estools/Commands/PotionCommandTest.java @@ -0,0 +1,116 @@ +package net.estools.Commands; + +import net.estools.EsToolsUnitTest; +import net.estools.Implementation.TestItemStack; +import net.estools.Implementation.TestPlayer; +import net.estools.Implementation.TestPotion; +import net.estools.ServerApi.EsPotType; +import net.estools.ServerApi.EsPotionEffectType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +// /potion [amplifier] [duration] [amount] [drink/splash/lingering] [player] +public class PotionCommandTest extends EsToolsUnitTest { + + @Test + public void noArg() { + executeAssertOneError("potion"); + } + + @Test + public void invalidEffect() { + executeAssertOneError("potion invalid"); + } + + @Test + public void createPot() { + player.getInventory().clear(); + executeAssertSuccess("potion speed"); + TestItemStack slot0 = (TestItemStack) player.getInventory().getItem(0); + Assertions.assertInstanceOf(TestPotion.class, slot0); + + TestPotion pot = (TestPotion) slot0; + + Assertions.assertEquals(EsPotType.drink, pot.getPotionType()); + Assertions.assertEquals(pot.getEffects()[0].getType(), EsPotionEffectType.fromKey("speed")); + } + + @Test + public void validAmp() { + player.getInventory().clear(); + executeAssertSuccess("potion speed 2"); + TestItemStack slot0 = (TestItemStack) player.getInventory().getItem(0); + Assertions.assertInstanceOf(TestPotion.class, slot0); + + TestPotion pot = (TestPotion) slot0; + + Assertions.assertEquals(EsPotType.drink, pot.getPotionType()); + Assertions.assertEquals(pot.getEffects()[0].getType(), EsPotionEffectType.fromKey("speed")); + Assertions.assertEquals(1, pot.getEffects()[0].getAmp()); + } + + @Test + public void validDuration() { + player.getInventory().clear(); + executeAssertSuccess("potion speed 2 5"); + TestItemStack slot0 = (TestItemStack) player.getInventory().getItem(0); + Assertions.assertInstanceOf(TestPotion.class, slot0); + + TestPotion pot = (TestPotion) slot0; + + Assertions.assertEquals(EsPotType.drink, pot.getPotionType()); + Assertions.assertEquals(pot.getEffects()[0].getType(), EsPotionEffectType.fromKey("speed")); + Assertions.assertEquals(1, pot.getEffects()[0].getAmp()); + Assertions.assertEquals(5*20, pot.getEffects()[0].getDuration()); + } + + @Test + public void validAmount() { + player.getInventory().clear(); + executeAssertSuccess("potion speed 2 5 69"); + TestItemStack slot0 = (TestItemStack) player.getInventory().getItem(0); + Assertions.assertInstanceOf(TestPotion.class, slot0); + + TestPotion pot = (TestPotion) slot0; + + Assertions.assertEquals(EsPotType.drink, pot.getPotionType()); + Assertions.assertEquals(pot.getEffects()[0].getType(), EsPotionEffectType.fromKey("speed")); + Assertions.assertEquals(1, pot.getEffects()[0].getAmp()); + Assertions.assertEquals(5*20, pot.getEffects()[0].getDuration()); + Assertions.assertEquals(69, pot.getAmount()); + } + + @Test + public void validType() { + player.getInventory().clear(); + executeAssertSuccess("potion speed 2 5 69 splash"); + TestItemStack slot0 = (TestItemStack) player.getInventory().getItem(0); + Assertions.assertInstanceOf(TestPotion.class, slot0); + + TestPotion pot = (TestPotion) slot0; + + Assertions.assertEquals(EsPotType.splash, pot.getPotionType()); + Assertions.assertEquals(pot.getEffects()[0].getType(), EsPotionEffectType.fromKey("speed")); + Assertions.assertEquals(1, pot.getEffects()[0].getAmp()); + Assertions.assertEquals(5*20, pot.getEffects()[0].getDuration()); + Assertions.assertEquals(69, pot.getAmount()); + } + + @Test + public void anotherPlayer() { + TestPlayer p = createPlayer(); + + p.getInventory().clear(); + executeAssertSuccess("potion speed 2 5 69 splash " + p.getName()); + TestItemStack slot0 = (TestItemStack) p.getInventory().getItem(0); + Assertions.assertInstanceOf(TestPotion.class, slot0); + + TestPotion pot = (TestPotion) slot0; + + Assertions.assertEquals(EsPotType.splash, pot.getPotionType()); + Assertions.assertEquals(pot.getEffects()[0].getType(), EsPotionEffectType.fromKey("speed")); + Assertions.assertEquals(1, pot.getEffects()[0].getAmp()); + Assertions.assertEquals(5*20, pot.getEffects()[0].getDuration()); + Assertions.assertEquals(69, pot.getAmount()); + } +} diff --git a/src/test/java/net/estools/Implementation/TestEntity.java b/src/test/java/net/estools/Implementation/TestEntity.java index 93bc64e..ab0cf6c 100644 --- a/src/test/java/net/estools/Implementation/TestEntity.java +++ b/src/test/java/net/estools/Implementation/TestEntity.java @@ -113,6 +113,9 @@ public EsLocation getLocation() { @Override public boolean leaveVehicle() { + if (vehicle == null) { + return false; + } vehicle.getPassengers().remove(this); vehicle = null; return true; diff --git a/src/test/java/net/estools/Implementation/TestPotion.java b/src/test/java/net/estools/Implementation/TestPotion.java new file mode 100644 index 0000000..276961c --- /dev/null +++ b/src/test/java/net/estools/Implementation/TestPotion.java @@ -0,0 +1,36 @@ +package net.estools.Implementation; + +import net.estools.ServerApi.EsMaterial; +import net.estools.ServerApi.EsPotType; +import net.estools.ServerApi.EsPotionEffect; +import net.estools.ServerApi.Interfaces.EsPotion; + +import java.util.ArrayList; +import java.util.List; + +public class TestPotion extends TestItemStack implements EsPotion { + private final EsPotType potType; + private final List effects; + + public TestPotion(int amount, EsPotType potType, EsPotionEffect effect) { + super(EsMaterial.fromKey("POTION"), amount); + this.potType = potType; + effects = new ArrayList<>(); + effects.add(effect); + } + + @Override + public EsPotionEffect[] getEffects() { + return effects.toArray(new EsPotionEffect[0]); + } + + @Override + public EsPotType getPotionType() { + return potType; + } + + @Override + public void addEffect(EsPotionEffect effect) { + effects.add(effect); + } +} diff --git a/src/test/java/net/estools/Implementation/TestServer.java b/src/test/java/net/estools/Implementation/TestServer.java index a1a523b..eead4ab 100644 --- a/src/test/java/net/estools/Implementation/TestServer.java +++ b/src/test/java/net/estools/Implementation/TestServer.java @@ -31,6 +31,7 @@ public class TestServer implements EsServer { materials.add(EsMaterial.createUnchecked("NETHERITE_SWORD")); materials.add(EsMaterial.createUnchecked("DIAMOND_SWORD")); materials.add(EsMaterial.createUnchecked("SALMON")); + materials.add(EsMaterial.createUnchecked("POTION")); enchantments = new HashSet<>(); enchantments.add(EsEnchantment.createUnchecked("sharpness")); @@ -90,7 +91,7 @@ public EsItemStack createItemStack(EsMaterial material, int amount) { @Override public EsPotion createPotion(EsPotType potType, EsPotionEffect effect, int amount) { - return null; + return new TestPotion(amount, potType, effect); } @Override