Skip to content

Commit

Permalink
Full command unit test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
CoPokBl committed Jun 28, 2024
1 parent b3559a1 commit b3ac5fe
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/test/java/net/estools/Commands/DismountCommandTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
116 changes: 116 additions & 0 deletions src/test/java/net/estools/Commands/PotionCommandTest.java
Original file line number Diff line number Diff line change
@@ -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 <effect> [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());
}
}
3 changes: 3 additions & 0 deletions src/test/java/net/estools/Implementation/TestEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public EsLocation getLocation() {

@Override
public boolean leaveVehicle() {
if (vehicle == null) {
return false;
}
vehicle.getPassengers().remove(this);
vehicle = null;
return true;
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/net/estools/Implementation/TestPotion.java
Original file line number Diff line number Diff line change
@@ -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<EsPotionEffect> 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);
}
}
3 changes: 2 additions & 1 deletion src/test/java/net/estools/Implementation/TestServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b3ac5fe

Please sign in to comment.