Skip to content

Commit 7421144

Browse files
authored
Merge pull request #612 from dogboy21/peripheral-tests
Implement game tests for more peripherals
2 parents e1a84cd + 62ee0aa commit 7421144

22 files changed

+2428
-1
lines changed

src/testMod/java/dan200/computercraft/gametest/core/TestAPI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import dan200.computercraft.api.lua.LuaFunction;
1313
import dan200.computercraft.gametest.api.ComputerState;
1414
import dan200.computercraft.gametest.api.TestExtensionsKt;
15+
import dan200.computercraft.shared.computer.core.ServerContext;
16+
import de.srendi.advancedperipherals.common.util.LuaConverter;
1517
import net.minecraft.gametest.framework.GameTestSequence;
18+
import net.minecraftforge.server.ServerLifecycleHooks;
1619

1720
import java.util.Optional;
1821

@@ -80,4 +83,14 @@ public final void ok(Optional<String> marker) throws LuaException {
8083
public final void log(String message) {
8184
ComputerCraft.log.info("[Computer '{}'] {}", label, message);
8285
}
86+
87+
@LuaFunction
88+
public final Object getComputerPosition() {
89+
return ServerContext.get(ServerLifecycleHooks.getCurrentServer()).registry().getComputers().stream()
90+
.filter(computer -> computer.getLabel() != null && computer.getLabel().equals(label))
91+
.findFirst()
92+
.map(computer -> LuaConverter.posToObject(computer.getPosition()))
93+
.orElse(null);
94+
}
95+
8396
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dan200.computercraft.gametest.core;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraftforge.event.level.NoteBlockEvent;
5+
import net.minecraftforge.eventbus.api.SubscribeEvent;
6+
import net.minecraftforge.fml.common.Mod;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
@Mod.EventBusSubscriber(modid = TestMod.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
12+
public class TestEvents {
13+
public static final Map<BlockPos, Integer> triggeredNoteBlocks = new HashMap<>();
14+
15+
@SubscribeEvent
16+
public static void onNoteBlockTrigger(NoteBlockEvent.Play event) {
17+
if (event.getLevel().isClientSide()) return;
18+
triggeredNoteBlocks.put(event.getPos(), triggeredNoteBlocks.getOrDefault(event.getPos(), 0) + 1);
19+
}
20+
21+
}

src/testMod/java/dan200/computercraft/gametest/core/TestMod.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@
3232
import java.util.Locale;
3333
import java.util.function.Consumer;
3434

35-
@Mod("advancedperipheralstest")
35+
@Mod(TestMod.MOD_ID)
3636
public class TestMod {
37+
38+
public static final String MOD_ID = "advancedperipheralstest";
39+
3740
public TestMod() {
3841
TestHooks.init();
3942

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package de.srendi.advancedperipherals.test
2+
3+
import dan200.computercraft.gametest.api.*
4+
import dan200.computercraft.gametest.core.TestEvents
5+
import net.minecraft.core.BlockPos
6+
import net.minecraft.gametest.framework.*
7+
import net.minecraft.world.InteractionHand
8+
import net.minecraft.world.entity.EntityType
9+
import net.minecraft.world.entity.animal.allay.Allay
10+
import net.minecraft.world.item.ItemStack
11+
import net.minecraft.world.item.Items
12+
13+
@GameTestHolder
14+
class ModIntegrTest {
15+
16+
@GameTest
17+
fun botaniaFlower(context: GameTestHelper) = context.sequence {
18+
thenComputerOk();
19+
}
20+
21+
@GameTest
22+
fun botaniaManaPool(context: GameTestHelper) = context.sequence {
23+
thenComputerOk();
24+
}
25+
26+
@GameTest
27+
fun botaniaSpreader(context: GameTestHelper) = context.sequence {
28+
thenComputerOk();
29+
}
30+
31+
@GameTest(timeoutTicks = 300)
32+
fun minecraftBeacon(context: GameTestHelper) = context.sequence {
33+
thenComputerOk();
34+
}
35+
36+
@GameTest
37+
fun minecraftNoteBlock(context: GameTestHelper) = context.sequence {
38+
thenExecute { TestEvents.triggeredNoteBlocks.clear() }
39+
thenComputerOk()
40+
thenExecute {
41+
val successBlock = BlockPos(2, 2, 1)
42+
val failBlock = BlockPos(2, 2, 3)
43+
44+
if (TestEvents.triggeredNoteBlocks.getOrDefault(context.absolutePos(successBlock), 0) != 1)
45+
context.fail("Note Block should have played one time", successBlock)
46+
47+
if (TestEvents.triggeredNoteBlocks.getOrDefault(context.absolutePos(failBlock), 0) != 0)
48+
context.fail("Note Block should not have played", failBlock)
49+
}
50+
}
51+
52+
@GameTest(timeoutTicks = 300)
53+
fun minecraftNoteBlock_Triggering_Allay(context: GameTestHelper) = context.sequence {
54+
// test if playNote triggers an allay
55+
// related issue: https://github.com/IntelligenceModding/AdvancedPeripherals/issues/603
56+
57+
val item = Items.DIAMOND
58+
var allay: Allay? = null
59+
thenExecute {
60+
allay = context.spawn(EntityType.ALLAY, 2, 3, 2)
61+
allay?.setItemInHand(InteractionHand.MAIN_HAND, ItemStack(item))
62+
63+
context.spawnItem(item, 2f, 3f, 2f)
64+
}
65+
66+
thenWaitUntil { context.assertEntityNotPresent(EntityType.ITEM) }
67+
thenWaitUntil {
68+
if (allay?.inventory?.getItem(0)?.count != 1)
69+
context.fail("Expected Allay to pick up item")
70+
}
71+
thenOnComputer { callPeripheral("left", "playNote") }
72+
thenWaitUntil { context.assertEntityPresent(EntityType.ITEM) }
73+
thenWaitUntil {
74+
if (allay?.inventory?.getItem(0)?.count != 0)
75+
context.fail("Expected Allay to drop item")
76+
}
77+
}
78+
79+
}

src/testMod/kotlin/de/srendi/advancedperipherals/test/PeripheralTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,19 @@ class PeripheralTest {
3737
thenComputerOk();
3838
}
3939

40+
@GameTest
41+
fun nbtStorage(context: GameTestHelper) = context.sequence {
42+
thenComputerOk();
43+
}
44+
45+
@GameTest(timeoutTicks = 300)
46+
fun rsIntegrator(context: GameTestHelper) = context.sequence {
47+
thenComputerOk();
48+
}
49+
50+
@GameTest(timeoutTicks = 300)
51+
fun geoScanner(context: GameTestHelper) = context.sequence {
52+
thenComputerOk();
53+
}
54+
4055
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
--- Advanced Peripherals tests for the Botania integration on Flowers
3+
--- Covers `getMana`, `getMaxMana`, `isFloating`,
4+
--- `isOnEnchantedSoil`, `isEmpty`, `isFull`
5+
---
6+
7+
-- Test for Entropinnyum (Flower has full mana store and is on enchanted soil)
8+
test.eq("manaFlower", peripheral.getType("left"), "Peripheral should be manaFlower")
9+
entropinnyum = peripheral.wrap("left")
10+
test.assert(entropinnyum, "Peripheral not found")
11+
12+
test.eq(entropinnyum.getMaxMana(), entropinnyum.getMana(), "Entropinnyum should have full mana")
13+
test.eq(6500, entropinnyum.getMaxMana(), "Entropinnyum should have a max mana of 6500")
14+
test.assert(entropinnyum.isOnEnchantedSoil(), "Entropinnyum should be on enchanted soil")
15+
test.assert(not entropinnyum.isFloating(), "Entropinnyum should not be floating")
16+
-- test.assert(entropinnyum.isFull(), "Entropinnyum should be full") TODO: uncomment for 1.20.1 AP versions
17+
-- test.assert(not entropinnyum.isEmpty(), "Entropinnyum should not be empty") TODO: uncomment for 1.20.1 AP versions
18+
19+
-- Test for Endoflame (Flower has no mana stored and is on normal soil)
20+
test.eq("manaFlower", peripheral.getType("right"), "Peripheral should be manaFlower")
21+
endoflame = peripheral.wrap("right")
22+
test.assert(endoflame, "Peripheral not found")
23+
24+
test.eq(0, endoflame.getMana(), "Endoflame should have no mana")
25+
test.eq(300, endoflame.getMaxMana(), "Endoflame should have a max mana of 300")
26+
test.assert(not endoflame.isOnEnchantedSoil(), "Endoflame should not be on enchanted soil")
27+
test.assert(not endoflame.isFloating(), "Endoflame should not be floating")
28+
-- test.assert(not endoflame.isFull(), "Endoflame should not be full") TODO: uncomment for 1.20.1 AP versions
29+
-- test.assert(endoflame.isEmpty(), "Endoflame should be empty") TODO: uncomment for 1.20.1 AP versions
30+
31+
-- Test for Kekimurus (Flower has 1800 mana stored and is floating)
32+
test.eq("manaFlower", peripheral.getType("back"), "Peripheral should be manaFlower")
33+
kekimurus = peripheral.wrap("back")
34+
test.assert(kekimurus, "Peripheral not found")
35+
36+
test.eq(1800, kekimurus.getMana(), "Kekimurus should have 1800 mana")
37+
test.eq(9001, kekimurus.getMaxMana(), "Kekimurus should have a max mana of 9001")
38+
test.assert(not kekimurus.isOnEnchantedSoil(), "Kekimurus should not be on enchanted soil")
39+
test.assert(kekimurus.isFloating(), "Kekimurus should be floating")
40+
-- test.assert(not kekimurus.isFull(), "Kekimurus should not be full") TODO: uncomment for 1.20.1 AP versions
41+
-- test.assert(not kekimurus.isEmpty(), "Kekimurus should not be empty") TODO: uncomment for 1.20.1 AP versions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
--- Advanced Peripherals tests for the Botania integration on Mana Pools
3+
--- Covers `getMana`, `getMaxMana`, `getManaNeeded`,
4+
--- `isEmpty`, `isFull`
5+
---
6+
7+
-- TODO Add tests for canChargeItem, hasItems and getItems in 1.20.1 AP versions
8+
9+
-- Test Fabulous Mana Pool (should be empty)
10+
test.eq("manaPool", peripheral.getType("left"), "Peripheral should be manaPool")
11+
fabulous = peripheral.wrap("left")
12+
test.assert(fabulous, "Peripheral not found")
13+
14+
test.eq(0, fabulous.getMana(), "Mana should be 0")
15+
test.eq(1000000, fabulous.getMaxMana(), "Max mana should be 1000000")
16+
test.eq(1000000, fabulous.getManaNeeded(), "Mana needed should be 1000000")
17+
-- test.assert(fabulous.isEmpty(), "Mana pool should be empty") TODO method currently not implemented
18+
test.assert(not fabulous.isFull(), "Mana pool should not be full")
19+
20+
-- Test Mana Pool (should have 36000 mana)
21+
test.eq("manaPool", peripheral.getType("right"), "Peripheral should be manaPool")
22+
manaPool = peripheral.wrap("right")
23+
test.assert(manaPool, "Peripheral not found")
24+
25+
test.eq(36000, manaPool.getMana(), "Mana should be 36000")
26+
test.eq(1000000, manaPool.getMaxMana(), "Max mana should be 1000000")
27+
test.eq(964000, manaPool.getManaNeeded(), "Mana needed should be 964000")
28+
-- test.assert(not manaPool.isEmpty(), "Mana pool should not be empty") TODO method currently not implemented
29+
test.assert(not manaPool.isFull(), "Mana pool should not be full")
30+
31+
-- Test Creative Mana Pool (should have 1000000 mana)
32+
test.eq("manaPool", peripheral.getType("back"), "Peripheral should be manaPool")
33+
creative = peripheral.wrap("back")
34+
test.assert(creative, "Peripheral not found")
35+
36+
test.eq(1000000, creative.getMana(), "Mana should be 1000000")
37+
test.eq(1000000, creative.getMaxMana(), "Max mana should be 1000000")
38+
test.eq(0, creative.getManaNeeded(), "Mana needed should be 0")
39+
-- test.assert(not creative.isEmpty(), "Mana pool should not be empty") TODO method currently not implemented
40+
test.assert(creative.isFull(), "Mana pool should be full")
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
--- Advanced Peripherals tests for the Botania integration on Mana Spreaders
3+
--- Covers `getMana`, `getMaxMana`, `getVariant`,
4+
--- `isEmpty`, `isFull`, `getBounding`
5+
---
6+
7+
-- TODO Add tests for hasLens and getLens in 1.20.1 AP versions
8+
9+
-- Test basic Mana Spreader that is empty and directed towards a Mana Pool
10+
test.eq("manaSpreader", peripheral.getType("left"), "Peripheral should be manaSpreader")
11+
spreader = peripheral.wrap("left")
12+
test.assert(spreader, "Peripheral not found")
13+
14+
test.eq(0, spreader.getMana(), "Mana should be 0")
15+
test.eq(1000, spreader.getMaxMana(), "Max mana should be 1000")
16+
test.eq("MANA", spreader.getVariant(), "Variant should be MANA")
17+
-- test.assert(spreader.isEmpty(), "Mana Spreader should be empty") TODO: method returns the wrong value currently
18+
test.assert(not spreader.isFull(), "Mana Spreader should not be full")
19+
20+
bounding = spreader.getBounding()
21+
computerPos = test.getComputerPosition()
22+
test.assert(bounding, "Spreader binding should be returned")
23+
test.assert(computerPos, "Computer position should be returned")
24+
25+
test.eq(computerPos.x + 1, bounding.x, "Bounding x should be set to Mana pool (+1 relative to computer)")
26+
test.eq(computerPos.y, bounding.y, "Bounding y should be set to Mana pool (same as computer)")
27+
test.eq(computerPos.z - 1, bounding.z, "Bounding z should be set to Mana pool (-1 relative to computer")
28+
29+
-- Test Gaia Mana Spreader that is full
30+
test.eq("manaSpreader", peripheral.getType("right"), "Peripheral should be manaSpreader")
31+
gaiaSpreader = peripheral.wrap("right")
32+
test.assert(gaiaSpreader, "Peripheral not found")
33+
34+
test.eq(6400, gaiaSpreader.getMana(), "Mana should be 6400")
35+
test.eq(6400, gaiaSpreader.getMaxMana(), "Max mana should be 6400")
36+
test.eq("GAIA", gaiaSpreader.getVariant(), "Variant should be GAIA")
37+
-- test.assert(not gaiaSpreader.isEmpty(), "Mana Spreader should not be empty") TODO: method returns the wrong value currently
38+
test.assert(gaiaSpreader.isFull(), "Mana Spreader should be full")
39+
40+
test.assert(not gaiaSpreader.getBounding(), "Mana Spreader should not be bound to anything")
41+
42+
-- Test Elven Mana Spreader that has 177 mana
43+
test.eq("manaSpreader", peripheral.getType("back"), "Peripheral should be manaSpreader")
44+
elvenSpreader = peripheral.wrap("back")
45+
test.assert(elvenSpreader, "Peripheral not found")
46+
47+
test.eq(177, elvenSpreader.getMana(), "Mana should be 177")
48+
test.eq(1000, elvenSpreader.getMaxMana(), "Max mana should be 1000")
49+
test.eq("ELVEN", elvenSpreader.getVariant(), "Variant should be ELVEN")
50+
-- test.assert(not elvenSpreader.isEmpty(), "Mana Spreader should not be empty") TODO: method returns the wrong value currently
51+
test.assert(not elvenSpreader.isFull(), "Mana Spreader should not be full")
52+
53+
test.assert(not elvenSpreader.getBounding(), "Mana Spreader should not be bound to anything")
54+
55+
-- Test Pulse Mana Spreader that is empty
56+
test.eq("manaSpreader", peripheral.getType("top"), "Peripheral should be manaSpreader")
57+
pulseSpreader = peripheral.wrap("top")
58+
test.assert(pulseSpreader, "Peripheral not found")
59+
60+
test.eq(0, pulseSpreader.getMana(), "Mana should be 0")
61+
test.eq(1000, pulseSpreader.getMaxMana(), "Max mana should be 1000")
62+
test.eq("REDSTONE", pulseSpreader.getVariant(), "Variant should be REDSTONE")
63+
-- test.assert(pulseSpreader.isEmpty(), "Mana Spreader should be empty") TODO: method returns the wrong value currently
64+
test.assert(not pulseSpreader.isFull(), "Mana Spreader should not be full")
65+
66+
test.assert(not pulseSpreader.getBounding(), "Mana Spreader should not be bound to anything")
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
--- Advanced Peripherals tests for the Minecraft integration on Beacons
3+
--- Covers `getLevel`, `getPrimaryEffect`, `getSecondaryEffect`
4+
---
5+
6+
-- Wait until the beacon structure is formed
7+
sleep(4)
8+
9+
-- Test left beacon, level 4, primary and secondary effect set to haste
10+
test.eq("beacon", peripheral.getType("left"), "Peripheral should be beacon")
11+
beacon = peripheral.wrap("left")
12+
test.assert(beacon, "Peripheral not found")
13+
14+
test.eq(4, beacon.getLevel(), "Level should be 4")
15+
test.eq("effect.minecraft.haste", beacon.getPrimaryEffect(), "Primary effect should be haste")
16+
test.eq("effect.minecraft.haste", beacon.getSecondaryEffect(), "Secondary effect should be haste")
17+
18+
-- Test right beacon, level 4, primary effect set to speed, secondary effect not set
19+
test.eq("beacon", peripheral.getType("right"), "Peripheral should be beacon")
20+
beacon = peripheral.wrap("right")
21+
test.assert(beacon, "Peripheral not found")
22+
23+
test.eq(4, beacon.getLevel(), "Level should be 4")
24+
test.eq("effect.minecraft.speed", beacon.getPrimaryEffect(), "Primary effect should be haste")
25+
test.eq("none", beacon.getSecondaryEffect(), "Secondary effect should be none")
26+
27+
-- Test top beacon, level 0, primary and secondary effect not set
28+
test.eq("beacon", peripheral.getType("top"), "Peripheral should be beacon")
29+
beacon = peripheral.wrap("top")
30+
test.assert(beacon, "Peripheral not found")
31+
32+
test.eq(0, beacon.getLevel(), "Level should be 0")
33+
test.eq("none", beacon.getPrimaryEffect(), "Primary effect should be none")
34+
test.eq("none", beacon.getSecondaryEffect(), "Secondary effect should be none")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
--- Advanced Peripherals tests for the Minecraft integration on Note Blocks
3+
--- Covers `playNote`, `getNote`, `changeNoteBy`, `changeNote`
4+
---
5+
6+
test.eq("noteBlock", peripheral.getType("left"), "Peripheral should be noteBlock")
7+
noteBlock = peripheral.wrap("left")
8+
test.assert(noteBlock, "Peripheral not found")
9+
10+
test.eq(4, noteBlock.getNote(), "Note should be 4")
11+
test.eq(24, noteBlock.changeNoteBy(24), "Note should be 24 after setting it to 24")
12+
test.eq(24, noteBlock.getNote(), "Note should be 24")
13+
test.eq(0, noteBlock.changeNote(), "Note should be 0 after cycling it")
14+
test.eq(0, noteBlock.getNote(), "Note should be 0")
15+
test.eq(1, noteBlock.changeNote(), "Note should be 1 after cycling it")
16+
test.eq(1, noteBlock.getNote(), "Note should be 1")
17+
noteBlock.playNote()
18+
19+
-- this note block has a block above it, so it should not play a note
20+
test.eq("noteBlock", peripheral.getType("right"), "Peripheral should be noteBlock")
21+
silentNoteBlock = peripheral.wrap("right")
22+
test.assert(silentNoteBlock, "Peripheral not found")
23+
24+
silentNoteBlock.playNote()

0 commit comments

Comments
 (0)