Skip to content

Revert "Revert "Implement game tests for more peripherals"" #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/testMod/java/dan200/computercraft/gametest/core/TestAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import dan200.computercraft.api.lua.LuaFunction;
import dan200.computercraft.gametest.api.ComputerState;
import dan200.computercraft.gametest.api.TestExtensionsKt;
import dan200.computercraft.shared.computer.core.ServerContext;
import de.srendi.advancedperipherals.common.util.LuaConverter;
import net.minecraft.gametest.framework.GameTestSequence;
import net.minecraftforge.server.ServerLifecycleHooks;

import java.util.Optional;

Expand Down Expand Up @@ -80,4 +83,14 @@ public final void ok(Optional<String> marker) throws LuaException {
public final void log(String message) {
ComputerCraft.log.info("[Computer '{}'] {}", label, message);
}

@LuaFunction
public final Object getComputerPosition() {
return ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers().stream()
.filter(computer -> computer.getLabel() != null && computer.getLabel().equals(label))
.findFirst()
.map(computer -> LuaConverter.posToObject(computer.getPosition()))
.orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dan200.computercraft.gametest.core;

import net.minecraft.core.BlockPos;
import net.minecraftforge.event.level.NoteBlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.HashMap;
import java.util.Map;

@Mod.EventBusSubscriber(modid = TestMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class TestEvents {
public static final Map<BlockPos, Integer> triggeredNoteBlocks = new HashMap<>();

@SubscribeEvent
public static void onNoteBlockTrigger(NoteBlockEvent.Play event) {
if (event.getLevel().isClientSide()) return;
triggeredNoteBlocks.put(event.getPos(), triggeredNoteBlocks.getOrDefault(event.getPos(), 0) + 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import java.util.Locale;
import java.util.function.Consumer;

@Mod("advancedperipheralstest")
@Mod(TestMod.MOD_ID)
public class TestMod {

public static final String MOD_ID = "advancedperipheralstest";

public TestMod() {
TestHooks.init();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package de.srendi.advancedperipherals.test

import dan200.computercraft.gametest.api.*
import dan200.computercraft.gametest.core.TestEvents
import net.minecraft.core.BlockPos
import net.minecraft.gametest.framework.*
import net.minecraft.world.InteractionHand
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.animal.allay.Allay
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items

@GameTestHolder
class ModIntegrTest {

@GameTest
fun botaniaFlower(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

@GameTest
fun botaniaManaPool(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

@GameTest
fun botaniaSpreader(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

@GameTest(timeoutTicks = 300)
fun minecraftBeacon(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

@GameTest
fun minecraftNoteBlock(context: GameTestHelper) = context.sequence {
thenExecute { TestEvents.triggeredNoteBlocks.clear() }
thenComputerOk()
thenExecute {
val successBlock = BlockPos(2, 2, 1)
val failBlock = BlockPos(2, 2, 3)

if (TestEvents.triggeredNoteBlocks.getOrDefault(context.absolutePos(successBlock), 0) != 1)
context.fail("Note Block should have played one time", successBlock)

if (TestEvents.triggeredNoteBlocks.getOrDefault(context.absolutePos(failBlock), 0) != 0)
context.fail("Note Block should not have played", failBlock)
}
}

@GameTest(timeoutTicks = 300)
fun minecraftNoteBlock_Triggering_Allay(context: GameTestHelper) = context.sequence {
// test if playNote triggers an allay
// related issue: https://github.com/IntelligenceModding/AdvancedPeripherals/issues/603

val item = Items.DIAMOND
var allay: Allay? = null
thenExecute {
allay = context.spawn(EntityType.ALLAY, 2, 3, 2)
allay?.setItemInHand(InteractionHand.MAIN_HAND, ItemStack(item))

context.spawnItem(item, 2f, 3f, 2f)
}

thenWaitUntil { context.assertEntityNotPresent(EntityType.ITEM) }
thenWaitUntil {
if (allay?.inventory?.getItem(0)?.count != 1)
context.fail("Expected Allay to pick up item")
}
thenOnComputer { callPeripheral("left", "playNote") }
thenWaitUntil { context.assertEntityPresent(EntityType.ITEM) }
thenWaitUntil {
if (allay?.inventory?.getItem(0)?.count != 0)
context.fail("Expected Allay to drop item")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,19 @@ class PeripheralTest {
thenComputerOk();
}

@GameTest
fun nbtStorage(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

@GameTest(timeoutTicks = 300)
fun rsIntegrator(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

@GameTest(timeoutTicks = 300)
fun geoScanner(context: GameTestHelper) = context.sequence {
thenComputerOk();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
--- Advanced Peripherals tests for the Botania integration on Flowers
--- Covers `getMana`, `getMaxMana`, `isFloating`,
--- `isOnEnchantedSoil`, `isEmpty`, `isFull`
---

-- Test for Entropinnyum (Flower has full mana store and is on enchanted soil)
test.eq("manaFlower", peripheral.getType("left"), "Peripheral should be manaFlower")
entropinnyum = peripheral.wrap("left")
test.assert(entropinnyum, "Peripheral not found")

test.eq(entropinnyum.getMaxMana(), entropinnyum.getMana(), "Entropinnyum should have full mana")
test.eq(6500, entropinnyum.getMaxMana(), "Entropinnyum should have a max mana of 6500")
test.assert(entropinnyum.isOnEnchantedSoil(), "Entropinnyum should be on enchanted soil")
test.assert(not entropinnyum.isFloating(), "Entropinnyum should not be floating")
-- test.assert(entropinnyum.isFull(), "Entropinnyum should be full") TODO: uncomment for 1.20.1 AP versions
-- test.assert(not entropinnyum.isEmpty(), "Entropinnyum should not be empty") TODO: uncomment for 1.20.1 AP versions

-- Test for Endoflame (Flower has no mana stored and is on normal soil)
test.eq("manaFlower", peripheral.getType("right"), "Peripheral should be manaFlower")
endoflame = peripheral.wrap("right")
test.assert(endoflame, "Peripheral not found")

test.eq(0, endoflame.getMana(), "Endoflame should have no mana")
test.eq(300, endoflame.getMaxMana(), "Endoflame should have a max mana of 300")
test.assert(not endoflame.isOnEnchantedSoil(), "Endoflame should not be on enchanted soil")
test.assert(not endoflame.isFloating(), "Endoflame should not be floating")
-- test.assert(not endoflame.isFull(), "Endoflame should not be full") TODO: uncomment for 1.20.1 AP versions
-- test.assert(endoflame.isEmpty(), "Endoflame should be empty") TODO: uncomment for 1.20.1 AP versions

-- Test for Kekimurus (Flower has 1800 mana stored and is floating)
test.eq("manaFlower", peripheral.getType("back"), "Peripheral should be manaFlower")
kekimurus = peripheral.wrap("back")
test.assert(kekimurus, "Peripheral not found")

test.eq(1800, kekimurus.getMana(), "Kekimurus should have 1800 mana")
test.eq(9001, kekimurus.getMaxMana(), "Kekimurus should have a max mana of 9001")
test.assert(not kekimurus.isOnEnchantedSoil(), "Kekimurus should not be on enchanted soil")
test.assert(kekimurus.isFloating(), "Kekimurus should be floating")
-- test.assert(not kekimurus.isFull(), "Kekimurus should not be full") TODO: uncomment for 1.20.1 AP versions
-- test.assert(not kekimurus.isEmpty(), "Kekimurus should not be empty") TODO: uncomment for 1.20.1 AP versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
--- Advanced Peripherals tests for the Botania integration on Mana Pools
--- Covers `getMana`, `getMaxMana`, `getManaNeeded`,
--- `isEmpty`, `isFull`
---

-- TODO Add tests for canChargeItem, hasItems and getItems in 1.20.1 AP versions

-- Test Fabulous Mana Pool (should be empty)
test.eq("manaPool", peripheral.getType("left"), "Peripheral should be manaPool")
fabulous = peripheral.wrap("left")
test.assert(fabulous, "Peripheral not found")

test.eq(0, fabulous.getMana(), "Mana should be 0")
test.eq(1000000, fabulous.getMaxMana(), "Max mana should be 1000000")
test.eq(1000000, fabulous.getManaNeeded(), "Mana needed should be 1000000")
-- test.assert(fabulous.isEmpty(), "Mana pool should be empty") TODO method currently not implemented
test.assert(not fabulous.isFull(), "Mana pool should not be full")

-- Test Mana Pool (should have 36000 mana)
test.eq("manaPool", peripheral.getType("right"), "Peripheral should be manaPool")
manaPool = peripheral.wrap("right")
test.assert(manaPool, "Peripheral not found")

test.eq(36000, manaPool.getMana(), "Mana should be 36000")
test.eq(1000000, manaPool.getMaxMana(), "Max mana should be 1000000")
test.eq(964000, manaPool.getManaNeeded(), "Mana needed should be 964000")
-- test.assert(not manaPool.isEmpty(), "Mana pool should not be empty") TODO method currently not implemented
test.assert(not manaPool.isFull(), "Mana pool should not be full")

-- Test Creative Mana Pool (should have 1000000 mana)
test.eq("manaPool", peripheral.getType("back"), "Peripheral should be manaPool")
creative = peripheral.wrap("back")
test.assert(creative, "Peripheral not found")

test.eq(1000000, creative.getMana(), "Mana should be 1000000")
test.eq(1000000, creative.getMaxMana(), "Max mana should be 1000000")
test.eq(0, creative.getManaNeeded(), "Mana needed should be 0")
-- test.assert(not creative.isEmpty(), "Mana pool should not be empty") TODO method currently not implemented
test.assert(creative.isFull(), "Mana pool should be full")
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
--- Advanced Peripherals tests for the Botania integration on Mana Spreaders
--- Covers `getMana`, `getMaxMana`, `getVariant`,
--- `isEmpty`, `isFull`, `getBounding`
---

-- TODO Add tests for hasLens and getLens in 1.20.1 AP versions

-- Test basic Mana Spreader that is empty and directed towards a Mana Pool
test.eq("manaSpreader", peripheral.getType("left"), "Peripheral should be manaSpreader")
spreader = peripheral.wrap("left")
test.assert(spreader, "Peripheral not found")

test.eq(0, spreader.getMana(), "Mana should be 0")
test.eq(1000, spreader.getMaxMana(), "Max mana should be 1000")
test.eq("MANA", spreader.getVariant(), "Variant should be MANA")
-- test.assert(spreader.isEmpty(), "Mana Spreader should be empty") TODO: method returns the wrong value currently
test.assert(not spreader.isFull(), "Mana Spreader should not be full")

bounding = spreader.getBounding()
computerPos = test.getComputerPosition()
test.assert(bounding, "Spreader binding should be returned")
test.assert(computerPos, "Computer position should be returned")

test.eq(computerPos.x + 1, bounding.x, "Bounding x should be set to Mana pool (+1 relative to computer)")
test.eq(computerPos.y, bounding.y, "Bounding y should be set to Mana pool (same as computer)")
test.eq(computerPos.z - 1, bounding.z, "Bounding z should be set to Mana pool (-1 relative to computer")

-- Test Gaia Mana Spreader that is full
test.eq("manaSpreader", peripheral.getType("right"), "Peripheral should be manaSpreader")
gaiaSpreader = peripheral.wrap("right")
test.assert(gaiaSpreader, "Peripheral not found")

test.eq(6400, gaiaSpreader.getMana(), "Mana should be 6400")
test.eq(6400, gaiaSpreader.getMaxMana(), "Max mana should be 6400")
test.eq("GAIA", gaiaSpreader.getVariant(), "Variant should be GAIA")
-- test.assert(not gaiaSpreader.isEmpty(), "Mana Spreader should not be empty") TODO: method returns the wrong value currently
test.assert(gaiaSpreader.isFull(), "Mana Spreader should be full")

test.assert(not gaiaSpreader.getBounding(), "Mana Spreader should not be bound to anything")

-- Test Elven Mana Spreader that has 177 mana
test.eq("manaSpreader", peripheral.getType("back"), "Peripheral should be manaSpreader")
elvenSpreader = peripheral.wrap("back")
test.assert(elvenSpreader, "Peripheral not found")

test.eq(177, elvenSpreader.getMana(), "Mana should be 177")
test.eq(1000, elvenSpreader.getMaxMana(), "Max mana should be 1000")
test.eq("ELVEN", elvenSpreader.getVariant(), "Variant should be ELVEN")
-- test.assert(not elvenSpreader.isEmpty(), "Mana Spreader should not be empty") TODO: method returns the wrong value currently
test.assert(not elvenSpreader.isFull(), "Mana Spreader should not be full")

test.assert(not elvenSpreader.getBounding(), "Mana Spreader should not be bound to anything")

-- Test Pulse Mana Spreader that is empty
test.eq("manaSpreader", peripheral.getType("top"), "Peripheral should be manaSpreader")
pulseSpreader = peripheral.wrap("top")
test.assert(pulseSpreader, "Peripheral not found")

test.eq(0, pulseSpreader.getMana(), "Mana should be 0")
test.eq(1000, pulseSpreader.getMaxMana(), "Max mana should be 1000")
test.eq("REDSTONE", pulseSpreader.getVariant(), "Variant should be REDSTONE")
-- test.assert(pulseSpreader.isEmpty(), "Mana Spreader should be empty") TODO: method returns the wrong value currently
test.assert(not pulseSpreader.isFull(), "Mana Spreader should not be full")

test.assert(not pulseSpreader.getBounding(), "Mana Spreader should not be bound to anything")
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
--- Advanced Peripherals tests for the Minecraft integration on Beacons
--- Covers `getLevel`, `getPrimaryEffect`, `getSecondaryEffect`
---

-- Wait until the beacon structure is formed
sleep(4)

-- Test left beacon, level 4, primary and secondary effect set to haste
test.eq("beacon", peripheral.getType("left"), "Peripheral should be beacon")
beacon = peripheral.wrap("left")
test.assert(beacon, "Peripheral not found")

test.eq(4, beacon.getLevel(), "Level should be 4")
test.eq("effect.minecraft.haste", beacon.getPrimaryEffect(), "Primary effect should be haste")
test.eq("effect.minecraft.haste", beacon.getSecondaryEffect(), "Secondary effect should be haste")

-- Test right beacon, level 4, primary effect set to speed, secondary effect not set
test.eq("beacon", peripheral.getType("right"), "Peripheral should be beacon")
beacon = peripheral.wrap("right")
test.assert(beacon, "Peripheral not found")

test.eq(4, beacon.getLevel(), "Level should be 4")
test.eq("effect.minecraft.speed", beacon.getPrimaryEffect(), "Primary effect should be haste")
test.eq("none", beacon.getSecondaryEffect(), "Secondary effect should be none")

-- Test top beacon, level 0, primary and secondary effect not set
test.eq("beacon", peripheral.getType("top"), "Peripheral should be beacon")
beacon = peripheral.wrap("top")
test.assert(beacon, "Peripheral not found")

test.eq(0, beacon.getLevel(), "Level should be 0")
test.eq("none", beacon.getPrimaryEffect(), "Primary effect should be none")
test.eq("none", beacon.getSecondaryEffect(), "Secondary effect should be none")
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
--- Advanced Peripherals tests for the Minecraft integration on Note Blocks
--- Covers `playNote`, `getNote`, `changeNoteBy`, `changeNote`
---

test.eq("noteBlock", peripheral.getType("left"), "Peripheral should be noteBlock")
noteBlock = peripheral.wrap("left")
test.assert(noteBlock, "Peripheral not found")

test.eq(4, noteBlock.getNote(), "Note should be 4")
test.eq(24, noteBlock.changeNoteBy(24), "Note should be 24 after setting it to 24")
test.eq(24, noteBlock.getNote(), "Note should be 24")
test.eq(0, noteBlock.changeNote(), "Note should be 0 after cycling it")
test.eq(0, noteBlock.getNote(), "Note should be 0")
test.eq(1, noteBlock.changeNote(), "Note should be 1 after cycling it")
test.eq(1, noteBlock.getNote(), "Note should be 1")
noteBlock.playNote()

-- this note block has a block above it, so it should not play a note
test.eq("noteBlock", peripheral.getType("right"), "Peripheral should be noteBlock")
silentNoteBlock = peripheral.wrap("right")
test.assert(silentNoteBlock, "Peripheral not found")

silentNoteBlock.playNote()
Loading