-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Patch]: Added StatusEffect handler tests (#986)
Added Minecraft-backed JUnit tests to the common module Started with codec testing for the StatusEffect handler
- Loading branch information
1 parent
0675913
commit 5a1c4d3
Showing
6 changed files
with
200 additions
and
10 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
4 changes: 4 additions & 0 deletions
4
...re/src/test/java/com/sigmundgranaas/forgero/fabric/gametest/handler/StatusEffectTest.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,4 @@ | ||
package com.sigmundgranaas.forgero.fabric.gametest.handler; | ||
|
||
public class StatusEffectTest { | ||
} |
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
14 changes: 14 additions & 0 deletions
14
...ommon/src/test/java/com/sigmundgranaas/forgero/minecraft/common/handler/Bootstrapped.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,14 @@ | ||
package com.sigmundgranaas.forgero.minecraft.common.handler; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
|
||
import net.minecraft.Bootstrap; | ||
import net.minecraft.SharedConstants; | ||
|
||
public interface Bootstrapped { | ||
@BeforeAll | ||
static void bootStrap() { | ||
SharedConstants.createGameVersion(); | ||
Bootstrap.initialize(); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...est/java/com/sigmundgranaas/forgero/minecraft/common/handler/StatusEffectHandlerTest.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,96 @@ | ||
package com.sigmundgranaas.forgero.minecraft.common.handler; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.mojang.serialization.JsonOps; | ||
import com.sigmundgranaas.forgero.core.property.Attribute; | ||
import com.sigmundgranaas.forgero.core.property.attribute.BaseAttribute; | ||
import com.sigmundgranaas.forgero.minecraft.common.handler.targeted.onHitEntity.StatusEffectHandler; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import net.minecraft.entity.effect.StatusEffect; | ||
import net.minecraft.entity.effect.StatusEffects; | ||
import net.minecraft.registry.Registries; | ||
import net.minecraft.util.Identifier; | ||
|
||
class StatusEffectHandlerTest implements Bootstrapped { | ||
private StatusEffectHandler handler; | ||
private StatusEffect effect; | ||
private Attribute level; | ||
private Attribute duration; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
effect = StatusEffects.POISON; | ||
level = BaseAttribute.of(2, StatusEffectHandler.EFFECT_LEVEL_ATTRIBUTE_TYPE); | ||
duration = BaseAttribute.of(100, StatusEffectHandler.EFFECT_DURATION_TYPE); | ||
handler = new StatusEffectHandler(effect, level, duration, "minecraft:targeted_entity"); | ||
} | ||
|
||
@Test | ||
void testCodecEncode() { | ||
JsonObject json = StatusEffectHandler.BUILDER.encodeStart(JsonOps.INSTANCE, handler) | ||
.result() | ||
.map(JsonElement::getAsJsonObject) | ||
.orElseThrow(); | ||
|
||
assertEquals("minecraft:poison", json.get("effect").getAsString()); | ||
assertEquals(2, json.get("level").getAsJsonObject().get("value").getAsInt()); | ||
assertEquals(100, json.get("duration").getAsJsonObject().get("value").getAsInt()); | ||
assertEquals("minecraft:targeted_entity", json.get("target").getAsString()); | ||
} | ||
|
||
@Test | ||
void testCodecDecode() { | ||
JsonObject json = new JsonObject(); | ||
json.addProperty("effect", "minecraft:poison"); | ||
json.addProperty("level", 2); | ||
json.addProperty("duration", 100); | ||
json.addProperty("target", "minecraft:targeted_entity"); | ||
|
||
StatusEffectHandler decodedHandler = StatusEffectHandler.BUILDER.parse(JsonOps.INSTANCE, json) | ||
.result() | ||
.orElseThrow(); | ||
|
||
assertEquals(Registries.STATUS_EFFECT.get(new Identifier("minecraft:poison")), decodedHandler.effect()); | ||
assertEquals(2, decodedHandler.level().compute().asInt()); | ||
assertEquals(100, decodedHandler.duration().compute().asInt()); | ||
assertEquals("minecraft:targeted_entity", decodedHandler.target()); | ||
} | ||
|
||
@Test | ||
void testCodecDecodeDefaultValues() { | ||
JsonObject json = new JsonObject(); | ||
json.addProperty("effect", "minecraft:poison"); | ||
json.addProperty("target", "minecraft:targeted_entity"); | ||
|
||
StatusEffectHandler decodedHandler = StatusEffectHandler.BUILDER.parse(JsonOps.INSTANCE, json) | ||
.result() | ||
.orElseThrow(); | ||
|
||
assertEquals(Registries.STATUS_EFFECT.get(new Identifier("minecraft:poison")), decodedHandler.effect()); | ||
assertEquals(1, decodedHandler.level().compute().asInt()); | ||
assertEquals(20 * 30, decodedHandler.duration().compute().asInt()); | ||
assertEquals("minecraft:targeted_entity", decodedHandler.target()); | ||
} | ||
|
||
@Test | ||
void testInvalidTarget() { | ||
handler = new StatusEffectHandler(effect, level, duration, "invalid_target"); | ||
assertThrows(IllegalArgumentException.class, () -> handler.onHit(null, null, null)); | ||
} | ||
|
||
@Test | ||
void testNullEffect() { | ||
assertThrows(NullPointerException.class, () -> new StatusEffectHandler(null, level, duration, "minecraft:targeted_entity")); | ||
} | ||
|
||
@Test | ||
void testNullTarget() { | ||
assertThrows(NullPointerException.class, () -> new StatusEffectHandler(effect, level, duration, null)); | ||
} | ||
} |