Skip to content

Commit 9cf6e2a

Browse files
committed
Some documentation additions
1 parent 7a0dd63 commit 9cf6e2a

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

docs/CREATING_TESTS.MD

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ Now you need to set a label to the computer, so it can run the lua test script.
2424
For example, peripheraltest as the test type. The test type is the name of the `@GameTestHolder` class. PeripheralTest.kt for this type.
2525
`environment` would be our test name. So the label would be `peripheraltest.environment`.
2626

27-
The computer needs the id 0 so it can load the default startup.lua and the tests scripts from the resources folder.
27+
The computer can either have the id 0 or 1. It depends what you want to do later.
28+
It is possible to either run a test script on the computer which is explained in "Writing the test" and you can run
29+
code on the computer using the kotlin game tests.
30+
Use id 0 if you want to run a lua script on the computer and id 1 if you only want to run code using kotlin game tests.
2831

2932
Now to save your test, get a structure block, place it in the lower right corner of the test structure and set the mode to save.
3033
Use the command `/give @p minecraft:structure_block` to get a structure block.
@@ -44,6 +47,10 @@ The class should be annotated with `@GameTestHolder`.
4447
The class should have a function annotated with `@GameTest` that will be executed by the game test server later.
4548
The function needs to have the parameter `GameTestContext` that will be used to interact with the game world.
4649

50+
There are two ways to test things on a computer. You can either run a lua script or run code on the computer using kotlin.
51+
52+
#### Lua Tests
53+
4754
A simple game test would look like this. You can see that the context can be used to interact with the world.
4855
You can then use one of the helper functions to interact with the computer. For example, `thenComputerOk()` to check if the script on the computer was executed without any fails.
4956
```kt
@@ -69,4 +76,48 @@ test.eq(false, isRaining, "It should not rain")
6976
Last but not least, import the script to the computer. You need to do that when you're currently in the test world, and you want to write the script in your IDE.
7077
Luckily, the test framework provides a command for that.
7178
`/cctest import` imports the scripts from the resources folder to the world folder.
72-
You can then find the script in the computer's folder using `ls tests/`.
79+
You can then find the script in the computer's folder using `ls tests/`.
80+
81+
82+
#### Kotlin Tests
83+
84+
To run tests on a computer, the computer needs to have the id 1
85+
86+
```kt
87+
thenOnComputer { callPeripheral("left", "playNote") }
88+
```
89+
90+
This would run `callPeripheral("left", "playNote")` on the computer which just calls a function on the peripheral on the left
91+
92+
Here is an example with our note block test
93+
94+
```kt
95+
@GameTest(timeoutTicks = 300)
96+
fun minecraftNoteBlock_Triggering_Allay(context: GameTestHelper) = context.sequence {
97+
// test if playNote triggers an allay
98+
// related issue: https://github.com/IntelligenceModding/AdvancedPeripherals/issues/603
99+
100+
val item = Items.DIAMOND
101+
var allay: Allay? = null
102+
thenExecute {
103+
allay = context.spawn(EntityType.ALLAY, 2, 3, 2)
104+
allay?.setItemInHand(InteractionHand.MAIN_HAND, ItemStack(item))
105+
106+
context.spawnItem(item, 2f, 3f, 2f)
107+
}
108+
109+
thenWaitUntil { context.assertEntityNotPresent(EntityType.ITEM) }
110+
thenWaitUntil {
111+
if (allay?.inventory?.getItem(0)?.count != 1)
112+
context.fail("Expected Allay to pick up item")
113+
}
114+
thenOnComputer { callPeripheral("left", "playNote") }
115+
thenWaitUntil { context.assertEntityPresent(EntityType.ITEM) }
116+
thenWaitUntil {
117+
if (allay?.inventory?.getItem(0)?.count != 0)
118+
context.fail("Expected Allay to drop item")
119+
}
120+
}
121+
```
122+
123+
For more examples, you can also check how the tests from CC work [here](https://github.com/cc-tweaked/CC-Tweaked/tree/mc-1.19.2/src/testMod/kotlin/dan200/computercraft/gametest).

0 commit comments

Comments
 (0)