1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 22:53:22 +00:00
CC-Tweaked/projects/common/src/testMod/kotlin/dan200/computercraft/gametest/Printer_Test.kt
Jonathan Coates 8f92417a2f
Add a system for client-side tests (#1219)
- Add a new ClientJavaExec Gradle task, which is used for client-side
   tests. This:

   - Copies the exec spec from another JavaExec task.
   - Sets some additional system properties to configure on gametest framework.
   - Runs Java inside an X framebuffer (when available), meaning we
     don't need to spin up a new window.

   We also configure this task so that only one instance can run at
   once, meaning we don't spawn multiple MC windows at once!

 - Port our 1.16 client test framework to 1.19. This is mostly the same
   as before, but screenshots no longer do a golden test: they /just/
   write to a folder. Screenshots are compared manually afterwards.

   This is still pretty brittle, and there's a lot of sleeps scattered
   around in the code. It's not clear how well this will play on CI.

 - Roll our own game test loader, rather than relying on the mod loader
   to do it for us. This ensures that loading is consistent between
   platforms (we already had to do some hacks for Forge) and makes it
   easier to provide custom logic for loading client-only tests.

 - Run several client tests (namely those involving monitor rendering)
   against Sodium and Iris too. There's some nastiness here to set up
   new Loom run configurations and automatically configure Iris to use
   Complementary Shaders, but it's not too bad. These tests /don't/ run
   on CI, so it doesn't need to be as reliable.
2022-11-18 23:57:25 +00:00

94 lines
3.7 KiB
Kotlin

package dan200.computercraft.gametest
import dan200.computercraft.gametest.api.assertBlockHas
import dan200.computercraft.gametest.api.assertExactlyItems
import dan200.computercraft.gametest.api.getBlockEntity
import dan200.computercraft.gametest.api.sequence
import dan200.computercraft.shared.ModRegistry
import dan200.computercraft.shared.peripheral.printer.PrinterBlock
import net.minecraft.core.BlockPos
import net.minecraft.gametest.framework.GameTest
import net.minecraft.gametest.framework.GameTestHelper
import net.minecraft.network.chat.Component
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.minecraft.world.level.block.RedStoneWireBlock
class Printer_Test {
/**
* Check comparators can read the contents of the disk drive
*/
@GameTest
fun Comparator(helper: GameTestHelper) = helper.sequence {
val printerPos = BlockPos(2, 2, 2)
val dustPos = BlockPos(2, 2, 4)
// Adding items should provide power
thenExecute {
val drive = helper.getBlockEntity(printerPos, ModRegistry.BlockEntities.PRINTER.get())
drive.setItem(0, ItemStack(Items.BLACK_DYE))
drive.setItem(1, ItemStack(Items.PAPER))
drive.setChanged()
}
thenIdle(2)
thenExecute { helper.assertBlockHas(dustPos, RedStoneWireBlock.POWER, 1) }
// And removing them should reset power.
thenExecute {
val drive = helper.getBlockEntity(printerPos, ModRegistry.BlockEntities.PRINTER.get())
drive.clearContent()
drive.setChanged()
}
thenIdle(2)
thenExecute { helper.assertBlockHas(dustPos, RedStoneWireBlock.POWER, 0) }
}
/**
* Changing the inventory contents updates the block state
*/
@GameTest
fun Contents_updates_state(helper: GameTestHelper) = helper.sequence {
val pos = BlockPos(2, 2, 2)
thenExecute {
val drive = helper.getBlockEntity(pos, ModRegistry.BlockEntities.PRINTER.get())
drive.setItem(1, ItemStack(Items.PAPER))
drive.setChanged()
helper.assertBlockHas(pos, PrinterBlock.TOP, true, message = "One item in the top row")
helper.assertBlockHas(pos, PrinterBlock.BOTTOM, false, message = "One item in the top row")
drive.setItem(7, ItemStack(Items.PAPER))
drive.setChanged()
helper.assertBlockHas(pos, PrinterBlock.TOP, true, message = "One item in each row")
helper.assertBlockHas(pos, PrinterBlock.BOTTOM, true, message = "One item in each row")
drive.setItem(1, ItemStack.EMPTY)
drive.setChanged()
helper.assertBlockHas(pos, PrinterBlock.TOP, false, message = "One item in the bottom")
helper.assertBlockHas(pos, PrinterBlock.BOTTOM, true, message = "One item in the bottom row")
drive.setItem(7, ItemStack.EMPTY)
drive.setChanged()
helper.assertBlockHas(pos, PrinterBlock.TOP, false, message = "Empty")
helper.assertBlockHas(pos, PrinterBlock.BOTTOM, false, message = "Empty")
}
}
/**
* When the block is broken, we drop the contents and an optionally named stack.
*/
@GameTest
fun Drops_contents(helper: GameTestHelper) = helper.sequence {
thenExecute {
helper.level.destroyBlock(helper.absolutePos(BlockPos(2, 2, 2)), true)
helper.assertExactlyItems(
ItemStack(ModRegistry.Items.PRINTER.get()).setHoverName(Component.literal("My Printer")),
ItemStack(Items.PAPER),
ItemStack(Items.BLACK_DYE),
message = "Breaking a printer should drop the contents",
)
}
}
}