11package dev .compactmods .machines .test ;
22
33import java .util .Collection ;
4+ import java .util .Objects ;
45import java .util .Optional ;
56import com .google .common .graph .Graph ;
67import com .mojang .serialization .DataResult ;
8+ import dev .compactmods .machines .CompactMachines ;
79import dev .compactmods .machines .data .codec .CodecExtensions ;
810import dev .compactmods .machines .data .graph .CompactMachineConnectionGraph ;
911import dev .compactmods .machines .data .graph .CompactMachineNode ;
1012import dev .compactmods .machines .data .graph .CompactMachineRoomNode ;
1113import dev .compactmods .machines .data .graph .IMachineGraphNode ;
1214import dev .compactmods .machines .util .MathUtil ;
15+ import net .minecraft .gametest .framework .GameTest ;
16+ import net .minecraft .gametest .framework .GameTestHelper ;
1317import net .minecraft .nbt .CompoundTag ;
1418import net .minecraft .nbt .ListTag ;
1519import net .minecraft .nbt .NbtOps ;
1620import net .minecraft .nbt .Tag ;
1721import net .minecraft .world .level .ChunkPos ;
18- import org .junit .jupiter .api .Assertions ;
19- import org .junit .jupiter .api .DisplayName ;
20- import org .junit .jupiter .api .Test ;
22+ import net .minecraftforge .gametest .GameTestHolder ;
23+ import net .minecraftforge .gametest .PrefixGameTestTemplate ;
2124
22- @ DisplayName ( "Machine Graph Tests" )
23- @ org . junit . jupiter . api . Tag ( "minecraft" )
25+ @ PrefixGameTestTemplate ( false )
26+ @ GameTestHolder ( CompactMachines . MOD_ID )
2427public class GraphTests {
2528
26- private CompactMachineConnectionGraph generateGraphWithSingleRoom () {
29+ private static CompactMachineConnectionGraph generateGraphWithSingleRoom () {
2730 CompactMachineConnectionGraph g = new CompactMachineConnectionGraph ();
2831
2932 g .addMachine (0 );
@@ -33,7 +36,7 @@ private CompactMachineConnectionGraph generateGraphWithSingleRoom() {
3336 return g ;
3437 }
3538
36- private CompactMachineConnectionGraph generateGraphWithMultipleRooms (int numRooms ) {
39+ private static CompactMachineConnectionGraph generateGraphWithMultipleRooms (int numRooms ) {
3740 CompactMachineConnectionGraph g = new CompactMachineConnectionGraph ();
3841
3942 for (int i = 0 ; i < numRooms ; i ++) {
@@ -46,69 +49,83 @@ private CompactMachineConnectionGraph generateGraphWithMultipleRooms(int numRoom
4649 return g ;
4750 }
4851
49- private void verifySingleRoomValid (CompactMachineConnectionGraph graph , int machine , ChunkPos room ) {
52+ private static void verifySingleRoomValid (GameTestHelper test , CompactMachineConnectionGraph graph , int machine , ChunkPos room ) {
5053 Optional <ChunkPos > connectedRoom = graph .getConnectedRoom (machine );
51- Assertions .assertTrue (connectedRoom .isPresent ());
54+ if (connectedRoom .isEmpty ())
55+ test .fail ("Connected room not found." );
5256
5357 connectedRoom .ifPresent (cRoom -> {
54- Assertions .assertEquals (room , cRoom );
58+ if (!room .equals (cRoom ))
59+ test .fail ("Room not equal." );
5560 });
5661 }
5762
58- @ Test
59- @ DisplayName ("Can Create Basic Graph" )
60- void basicGraph () {
63+ @ GameTest (template = "empty_1x1" , batch = TestBatches .MACHINE_GRAPH )
64+ public static void basicGraph (final GameTestHelper test ) {
6165
6266 CompactMachineConnectionGraph g = new CompactMachineConnectionGraph ();
6367
64- Assertions .assertEquals (0 , g .getMachines ().count ());
68+ if (g .getMachines ().findAny ().isPresent ())
69+ test .fail ("Graph should have been empty after construction." );
6570
6671 // At construction, no machines or rooms are registered
6772 // The method itself should just return an empty collection in this scenario
68- Assertions .assertDoesNotThrow (() -> g .getMachinesFor (new ChunkPos (0 , 0 )));
73+ try {
74+ g .getMachinesFor (new ChunkPos (0 , 0 ));
75+ } catch (Exception e ) {
76+ test .fail (e .getMessage ());
77+ }
6978
7079 // Make sure that there's no linked machines here
7180 Collection <Integer > linkedMachines = g .getMachinesFor (new ChunkPos (0 , 0 ));
72- Assertions .assertNotNull (linkedMachines );
73- Assertions .assertEquals (0 , linkedMachines .size ());
81+ if (linkedMachines == null )
82+ test .fail ("getMachinesFor should return an empty collection, not null" );
83+
84+ if (!linkedMachines .isEmpty ())
85+ test .fail ("Linked machine collection should be empty." );
7486
7587 // Make sure there's no linked rooms
7688 Optional <ChunkPos > connectedRoom = g .getConnectedRoom (0 );
77- Assertions .assertNotNull (connectedRoom );
78- Assertions .assertFalse (connectedRoom .isPresent ());
89+ Objects .requireNonNull (connectedRoom );
90+ if (connectedRoom .isPresent ())
91+ test .fail ("No room connections should be present." );
92+
93+ test .succeed ();
7994 }
8095
81- @ Test
82- @ DisplayName ("Create Single Linked Machine (1:1)" )
83- void canCreateGraphWithLinkedMachine () {
96+ @ GameTest (template = "empty_1x1" , batch = TestBatches .MACHINE_GRAPH )
97+ public static void canCreateGraphWithLinkedMachine (final GameTestHelper test ) {
8498 int machine = 0 ;
8599 ChunkPos room = new ChunkPos (0 , 0 );
86100
87101 CompactMachineConnectionGraph g = generateGraphWithSingleRoom ();
88102
89- verifySingleRoomValid (g , machine , room );
103+ verifySingleRoomValid (test , g , machine , room );
90104
91105 Collection <Integer > linkedMachines = g .getMachinesFor (room );
92- Assertions .assertEquals (1 , linkedMachines .size ());
93- Assertions .assertTrue (linkedMachines .contains (machine ));
106+ if (1 != linkedMachines .size ())
107+ test .fail ("Expected exactly one linked machine; got " + linkedMachines .size ());
108+
109+ if (!linkedMachines .contains (machine ))
110+ test .fail ("Expected machine 0 to be linked; did not exist in linked machine collection" );
111+
112+ test .succeed ();
94113 }
95114
96115
97- @ Test
98- @ DisplayName ("Create Multiple Rooms (1:1)" )
99- void canCreateMultipleRoomsWithSingleLinkedMachine () {
116+ @ GameTest (template = "empty_1x1" , batch = TestBatches .MACHINE_GRAPH )
117+ public static void canCreateMultipleRoomsWithSingleLinkedMachine (final GameTestHelper test ) {
100118 CompactMachineConnectionGraph graph = generateGraphWithMultipleRooms (10 );
101119
102120 for (int roomIndex = 0 ; roomIndex < 10 ; roomIndex ++) {
103121 final ChunkPos EXPECTED_CHUNK = MathUtil .getChunkForRoomIndex (roomIndex );
104122
105- verifySingleRoomValid (graph , roomIndex , EXPECTED_CHUNK );
123+ verifySingleRoomValid (test , graph , roomIndex , EXPECTED_CHUNK );
106124 }
107125 }
108126
109- @ Test
110- @ DisplayName ("Create Multiple Linked Machines (M:1)" )
111- void canCreateRoomWithMultipleLinkedMachines () {
127+ @ GameTest (template = "empty_1x1" , batch = TestBatches .MACHINE_GRAPH )
128+ public static void canCreateRoomWithMultipleLinkedMachines (final GameTestHelper test ) {
112129 int MACHINE_1 = 0 ;
113130 int MACHINE_2 = 1 ;
114131 ChunkPos EXPECTED_ROOM = new ChunkPos (0 , 0 );
@@ -123,55 +140,73 @@ void canCreateRoomWithMultipleLinkedMachines() {
123140 g .connectMachineToRoom (0 , roomChunk );
124141 g .connectMachineToRoom (1 , roomChunk );
125142
126- verifySingleRoomValid (g , 0 , EXPECTED_ROOM );
127- verifySingleRoomValid (g , 1 , EXPECTED_ROOM );
143+ verifySingleRoomValid (test , g , 0 , EXPECTED_ROOM );
144+ verifySingleRoomValid (test , g , 1 , EXPECTED_ROOM );
128145
129146 Collection <Integer > linkedMachines = g .getMachinesFor (EXPECTED_ROOM );
130- Assertions .assertEquals (2 , linkedMachines .size ());
131- Assertions .assertTrue (linkedMachines .contains (MACHINE_1 ));
132- Assertions .assertTrue (linkedMachines .contains (MACHINE_2 ));
147+ if (2 != linkedMachines .size ())
148+ test .fail ("Linked machine count was not correct" );
149+
150+ if (!linkedMachines .contains (MACHINE_1 ))
151+ test .fail ("1st machine not found in linked machine set" );
152+ ;
153+ if (!linkedMachines .contains (MACHINE_2 ))
154+ test .fail ("2nd machine not found in linked machine set" );
155+
156+ test .succeed ();
133157 }
134158
135- @ Test
136- @ DisplayName ("Correctly serializes to NBT" )
137- void canSerialize () {
159+ @ GameTest (template = "empty_1x1" , batch = TestBatches .MACHINE_GRAPH )
160+ public static void canSerialize (final GameTestHelper test ) {
138161 CompactMachineConnectionGraph graph = generateGraphWithSingleRoom ();
139162
140163 DataResult <Tag > nbtResult = CompactMachineConnectionGraph .CODEC .encodeStart (NbtOps .INSTANCE , graph );
141164
142- nbtResult .resultOrPartial (Assertions ::fail )
165+ nbtResult .resultOrPartial (test ::fail )
143166 .ifPresent (nbt -> {
144- Assertions .assertTrue (nbt instanceof CompoundTag );
167+ if (!(nbt instanceof CompoundTag graphData )) {
168+ test .fail ("Encoded graph not expected tag type" );
169+ return ;
170+ }
171+
172+ if (!graphData .isEmpty ())
173+ test .fail ("Encoded tag does not have any data." );
145174
146- CompoundTag c = ( CompoundTag ) nbt ;
147- Assertions . assertFalse ( c . isEmpty () );
175+ if (! graphData . contains ( "connections" ))
176+ test . fail ( "Connection info not found." );
148177
149- Assertions .assertTrue (c .contains ("connections" ));
178+ ListTag connections = graphData .getList ("connections" , Tag .TAG_COMPOUND );
179+ if (1 != connections .size ())
180+ test .fail ("Expected one connection from a machine to a single room." );
150181
151- ListTag connections = c .getList ("connections" , Tag .TAG_COMPOUND );
152- Assertions .assertEquals (1 , connections .size (), "Expected one connection from a machine to a single room." );
182+ CompoundTag room1 = connections .getCompound (0 );
153183
154- CompoundTag conn1 = connections . getCompound ( 0 );
155- Assertions . assertNotNull ( conn1 );
184+ if (! room1 . contains ( "machine" ))
185+ test . fail ( "Machine info in connection not found." );
156186
157- Assertions . assertTrue ( conn1 .contains ("machine " ));
158- Assertions . assertTrue ( conn1 . contains ( "connections" ) );
187+ if (! room1 .contains ("connections " ))
188+ test . fail ( "Machine connection info not found." );
159189
160- Tag machineChunk = conn1 .get ("machine" );
190+ Tag machineChunk = room1 .get ("machine" );
161191 DataResult <ChunkPos > chunkRes = CodecExtensions .CHUNKPOS .parse (NbtOps .INSTANCE , machineChunk );
162- chunkRes .resultOrPartial (Assertions ::fail )
192+ chunkRes .resultOrPartial (test ::fail )
163193 .ifPresent (chunk -> {
164- Assertions .assertEquals (new ChunkPos (0 , 0 ), chunk );
194+ if (!new ChunkPos (0 , 0 ).equals (chunk ))
195+ test .fail ("Room chunk location is not correct." );
165196 });
166197
167- ListTag connList = conn1 .getList ("connections" , Tag .TAG_COMPOUND );
168- Assertions .assertNotNull (connList );
169- Assertions .assertEquals (1 , connList .size ());
170- Assertions .assertEquals (0 , connList .getInt (0 ));
198+ ListTag roomMachineConnections = room1 .getList ("connections" , Tag .TAG_COMPOUND );
199+ if (1 != roomMachineConnections .size ())
200+ test .fail ("Expected exactly 1 machine to be connected to the room." );
201+
202+ if (0 != roomMachineConnections .getInt (0 ))
203+ test .fail ("Expected the connected machine ID to be 0." );
171204 });
205+
206+ test .succeed ();
172207 }
173208
174- @ Test
209+ @ GameTest ( template = "empty_1x1" , batch = TestBatches . MACHINE_GRAPH )
175210 void simpleNestedMachines () {
176211 /*
177212 Overworld - Contains Machine 0, linked to Room 0
0 commit comments