|
1 | 1 | package com.robotgryphon.compactmachines.tests; |
2 | 2 |
|
3 | 3 | import com.google.common.graph.Graph; |
| 4 | +import com.mojang.serialization.DataResult; |
| 5 | +import com.robotgryphon.compactmachines.data.codec.CodecExtensions; |
4 | 6 | import com.robotgryphon.compactmachines.data.graph.CompactMachineConnectionGraph; |
5 | 7 | import com.robotgryphon.compactmachines.data.graph.CompactMachineNode; |
6 | 8 | import com.robotgryphon.compactmachines.data.graph.CompactMachineRoomNode; |
7 | 9 | import com.robotgryphon.compactmachines.data.graph.IMachineGraphNode; |
| 10 | +import com.robotgryphon.compactmachines.tests.util.FileHelper; |
| 11 | +import net.minecraft.nbt.*; |
8 | 12 | import net.minecraft.util.math.ChunkPos; |
| 13 | +import net.minecraftforge.common.util.Constants; |
9 | 14 | import org.junit.jupiter.api.Assertions; |
10 | 15 | import org.junit.jupiter.api.DisplayName; |
11 | 16 | import org.junit.jupiter.api.Test; |
12 | 17 |
|
| 18 | +import java.io.File; |
| 19 | +import java.io.IOException; |
13 | 20 | import java.util.Collection; |
14 | 21 | import java.util.Optional; |
15 | 22 |
|
16 | 23 | @DisplayName("Machine Graph Tests") |
17 | 24 | public class GraphTests { |
18 | 25 |
|
| 26 | + private CompactMachineConnectionGraph generateGraphWithSingleRoom() { |
| 27 | + CompactMachineConnectionGraph g = new CompactMachineConnectionGraph(); |
| 28 | + |
| 29 | + g.addMachine(0); |
| 30 | + g.addRoom(new ChunkPos(0, 0)); |
| 31 | + |
| 32 | + g.connectMachineToRoom(0, new ChunkPos(0, 0)); |
| 33 | + return g; |
| 34 | + } |
| 35 | + |
19 | 36 | @Test |
20 | 37 | @DisplayName("Can Create Basic Graph") |
21 | 38 | void basicGraph() { |
@@ -45,12 +62,7 @@ void canCreateGraphWithLinkedMachine() { |
45 | 62 | int machine = 0; |
46 | 63 | ChunkPos room = new ChunkPos(0, 0); |
47 | 64 |
|
48 | | - CompactMachineConnectionGraph g = new CompactMachineConnectionGraph(); |
49 | | - |
50 | | - g.addMachine(0); |
51 | | - g.addRoom(new ChunkPos(0, 0)); |
52 | | - |
53 | | - g.connectMachineToRoom(0, new ChunkPos(0, 0)); |
| 65 | + CompactMachineConnectionGraph g = generateGraphWithSingleRoom(); |
54 | 66 |
|
55 | 67 | Optional<ChunkPos> connectedRoom = g.getConnectedRoom(machine); |
56 | 68 | Assertions.assertTrue(connectedRoom.isPresent()); |
@@ -100,6 +112,52 @@ void canCreateRoomWithMultipleLinkedMachines() { |
100 | 112 | Assertions.assertTrue(linkedMachines.contains(MACHINE_2)); |
101 | 113 | } |
102 | 114 |
|
| 115 | + @Test |
| 116 | + @DisplayName("Correctly serializes to NBT") |
| 117 | + void canSerialize() { |
| 118 | + CompactMachineConnectionGraph graph = generateGraphWithSingleRoom(); |
| 119 | + |
| 120 | + DataResult<INBT> nbtResult = CompactMachineConnectionGraph.CODEC.encodeStart(NBTDynamicOps.INSTANCE, graph); |
| 121 | + |
| 122 | + nbtResult.resultOrPartial(Assertions::fail) |
| 123 | + .ifPresent(nbt -> { |
| 124 | + Assertions.assertTrue(nbt instanceof CompoundNBT); |
| 125 | + |
| 126 | +// try { |
| 127 | +// File file = FileHelper.RESOURCES_DIR.resolve("graph.dat").toFile(); |
| 128 | +// file.delete(); |
| 129 | +// CompressedStreamTools.writeCompressed((CompoundNBT) nbt, file); |
| 130 | +// } catch (IOException e) { |
| 131 | +// e.printStackTrace(); |
| 132 | +// } |
| 133 | + |
| 134 | + CompoundNBT c = (CompoundNBT) nbt; |
| 135 | + Assertions.assertFalse(c.isEmpty()); |
| 136 | + |
| 137 | + Assertions.assertTrue(c.contains("connections")); |
| 138 | + |
| 139 | + ListNBT connections = c.getList("connections", Constants.NBT.TAG_COMPOUND); |
| 140 | + Assertions.assertEquals(1, connections.size(), "Expected one connection from a machine to a single room."); |
| 141 | + |
| 142 | + CompoundNBT conn1 = connections.getCompound(0); |
| 143 | + Assertions.assertNotNull(conn1); |
| 144 | + |
| 145 | + Assertions.assertTrue(conn1.contains("machine")); |
| 146 | + Assertions.assertTrue(conn1.contains("connections")); |
| 147 | + |
| 148 | + CompoundNBT machineChunk = conn1.getCompound("machine"); |
| 149 | + DataResult<ChunkPos> chunkRes = CodecExtensions.CHUNKPOS_CODEC.parse(NBTDynamicOps.INSTANCE, machineChunk); |
| 150 | + chunkRes.resultOrPartial(Assertions::fail) |
| 151 | + .ifPresent(chunk -> { |
| 152 | + Assertions.assertEquals(new ChunkPos(0, 0), chunk); |
| 153 | + }); |
| 154 | + |
| 155 | + ListNBT connList = conn1.getList("connections", Constants.NBT.TAG_INT); |
| 156 | + Assertions.assertNotNull(connList); |
| 157 | + Assertions.assertEquals(1, connList.size()); |
| 158 | + Assertions.assertEquals(0, connList.getInt(0)); |
| 159 | + }); |
| 160 | + } |
103 | 161 | // private void generateData(MutableGraph<IGraphNode> g, HashMap<String, IGraphNode> lookup) { |
104 | 162 | // Random r = new Random(); |
105 | 163 | // MachineExternalLocation[] values = MachineExternalLocation.values(); |
|
0 commit comments