Skip to content

Commit 301836e

Browse files
committed
Clean up test harnesses, filled layer tests 100 percent
1 parent 282a679 commit 301836e

18 files changed

+279
-207
lines changed

src/test/java/dev/compactmods/crafting/tests/recipes/layers/FilledLayerTests.java

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
package dev.compactmods.crafting.tests.recipes.layers;
22

3+
import java.util.Optional;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
36
import com.google.gson.JsonElement;
47
import com.mojang.serialization.JsonOps;
8+
import dev.compactmods.crafting.api.field.MiniaturizationFieldSize;
9+
import dev.compactmods.crafting.recipes.components.CCMiniRecipeComponents;
510
import dev.compactmods.crafting.recipes.layers.FilledComponentRecipeLayer;
11+
import dev.compactmods.crafting.tests.recipes.util.RecipeTestUtil;
612
import dev.compactmods.crafting.tests.util.FileHelper;
13+
import dev.compactmods.crafting.util.BlockSpaceUtil;
714
import net.minecraft.util.math.AxisAlignedBB;
15+
import net.minecraft.util.math.BlockPos;
816
import org.junit.jupiter.api.Assertions;
917
import org.junit.jupiter.api.Tag;
1018
import org.junit.jupiter.api.Test;
@@ -29,6 +37,9 @@ void CanCreateLayerInstance() {
2937
void ReturnsNoFilledIfDimensionsNull() {
3038
final FilledComponentRecipeLayer layer = getLayerFromFile("layers/filled/basic.json");
3139

40+
// We force the dimensions null here
41+
layer.setRecipeDimensions((AxisAlignedBB) null);
42+
3243
int filled = Assertions.assertDoesNotThrow(layer::getNumberFilledPositions);
3344
Assertions.assertEquals(0, filled);
3445
}
@@ -40,13 +51,109 @@ void CanUpdateDimensions() {
4051

4152
int filledBefore = layer.getNumberFilledPositions();
4253

43-
AxisAlignedBB newDims = new AxisAlignedBB(0, 0, 0, 5, 1, 5);
44-
Assertions.assertDoesNotThrow(() -> layer.setRecipeDimensions(newDims));
54+
Assertions.assertDoesNotThrow(() -> layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM));
4555

4656
int filledAfter = layer.getNumberFilledPositions();
4757

4858
Assertions.assertNotEquals(filledBefore, filledAfter, "Expected component count to change after growing layer dimensions.");
4959
}
5060

61+
@Test
62+
@Tag("minecraft")
63+
void ComponentPositionsAreCorrect() {
64+
final FilledComponentRecipeLayer layer = getLayerFromFile("layers/filled/basic.json");
65+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
66+
67+
Assertions.assertEquals(25, layer.getNumberFilledPositions());
68+
69+
final Set<BlockPos> expected = BlockSpaceUtil.getBlocksIn(MiniaturizationFieldSize.MEDIUM, 0)
70+
.map(BlockPos::immutable)
71+
.collect(Collectors.toSet());
72+
73+
final Set<BlockPos> actual = layer.getPositionsForComponent("G")
74+
.map(BlockPos::immutable)
75+
.collect(Collectors.toSet());
76+
77+
Assertions.assertEquals(expected, actual);
78+
}
79+
80+
@Test
81+
@Tag("minecraft")
82+
void CanFetchComponentByPosition() {
83+
final FilledComponentRecipeLayer layer = getLayerFromFile("layers/filled/basic.json");
84+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
85+
86+
final Optional<String> componentForPosition = layer.getComponentForPosition(BlockPos.ZERO);
87+
Assertions.assertTrue(componentForPosition.isPresent());
88+
componentForPosition.ifPresent(comp -> {
89+
Assertions.assertEquals("G", comp);
90+
});
91+
}
92+
93+
@Test
94+
@Tag("minecraft")
95+
void ReturnsEmptyWhenFetchingOOBPosition() {
96+
final FilledComponentRecipeLayer layer = getLayerFromFile("layers/filled/basic.json");
97+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
98+
99+
// Y = 1 should never happen, in any layer, ever
100+
final Optional<String> componentForPosition = layer.getComponentForPosition(BlockPos.ZERO.above());
101+
Assertions.assertFalse(componentForPosition.isPresent());
102+
}
103+
104+
@Test
105+
@Tag("minecraft")
106+
void LayerMatchesWorldInExactMatchScenario() {
107+
final TestRecipeLayerBlocks blocks = RecipeTestUtil.getLayerHarness("worlds/basic_filled_harness_5x.json");
108+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromHarness("worlds/basic_filled_harness_5x.json");
109+
110+
// Set up a 5x5x1 filled layer, using "G" component
111+
final FilledComponentRecipeLayer layer = new FilledComponentRecipeLayer("G");
112+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
113+
114+
boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
115+
Assertions.assertTrue(matched);
116+
}
117+
118+
@Test
119+
@Tag("minecraft")
120+
void FailsMatchIfAllBlocksNotIdentified() {
121+
final TestRecipeLayerBlocks blocks = RecipeTestUtil.getLayerHarness("worlds/basic_harness_5x.json");
122+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromHarness("worlds/basic_harness_5x.json");
123+
124+
// Set up a 5x5x1 filled layer, using "G" component
125+
final FilledComponentRecipeLayer layer = new FilledComponentRecipeLayer("G");
126+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
127+
128+
boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
129+
Assertions.assertFalse(matched);
130+
}
51131

132+
@Test
133+
@Tag("minecraft")
134+
void FailsMatchIfMoreThanOneBlockFound() {
135+
final TestRecipeLayerBlocks blocks = RecipeTestUtil.getLayerHarness("worlds/basic_harness_allmatched_5x.json");
136+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromHarness("worlds/basic_harness_allmatched_5x.json");
137+
138+
// Set up a 5x5x1 filled layer, using "G" component
139+
final FilledComponentRecipeLayer layer = new FilledComponentRecipeLayer("G");
140+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
141+
142+
boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
143+
Assertions.assertFalse(matched);
144+
}
145+
146+
@Test
147+
@Tag("minecraft")
148+
void FailsMatchIfComponentKeyNotFound() {
149+
final TestRecipeLayerBlocks blocks = RecipeTestUtil.getLayerHarness("worlds/basic_filled_harness_alt_5x.json");
150+
final CCMiniRecipeComponents components = RecipeTestUtil.getComponentsFromHarness("worlds/basic_filled_harness_alt_5x.json");
151+
152+
// Set up a 5x5x1 filled layer, using "G" component
153+
final FilledComponentRecipeLayer layer = new FilledComponentRecipeLayer("G");
154+
layer.setRecipeDimensions(MiniaturizationFieldSize.MEDIUM);
155+
156+
boolean matched = Assertions.assertDoesNotThrow(() -> layer.matches(components, blocks));
157+
Assertions.assertFalse(matched);
158+
}
52159
}

src/test/java/dev/compactmods/crafting/tests/recipes/layers/RecipeLayerUtilTests.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import java.util.Set;
44
import java.util.stream.Collectors;
5-
import com.google.gson.JsonElement;
6-
import com.mojang.serialization.JsonOps;
75
import dev.compactmods.crafting.api.recipe.layers.IRecipeLayerBlocks;
86
import dev.compactmods.crafting.recipes.layers.RecipeLayerUtil;
9-
import dev.compactmods.crafting.tests.util.FileHelper;
7+
import dev.compactmods.crafting.tests.recipes.util.RecipeTestUtil;
108
import net.minecraft.util.Rotation;
119
import net.minecraft.util.math.BlockPos;
1210
import org.junit.jupiter.api.Assertions;
@@ -18,7 +16,7 @@ public class RecipeLayerUtilTests {
1816
@Test
1917
@Tag("minecraft")
2018
void CanRotate() {
21-
TestRecipeLayerBlocks harness = getHarness("layers/mixed/basic_harness.json");
19+
TestRecipeLayerBlocks harness = RecipeTestUtil.getLayerHarness("worlds/basic_harness_5x.json");
2220
Assertions.assertNotNull(harness);
2321

2422
final IRecipeLayerBlocks rotatedClockwise = RecipeLayerUtil.rotate(harness, Rotation.CLOCKWISE_90);
@@ -33,7 +31,7 @@ void CanRotate() {
3331
@Test
3432
@Tag("minecraft")
3533
void CanRotateWithUnknownComponent() {
36-
TestRecipeLayerBlocks harness = getHarness("layers/mixed/unknown_component.json");
34+
TestRecipeLayerBlocks harness = RecipeTestUtil.getLayerHarness("worlds/unknown_component.json");
3735
Assertions.assertNotNull(harness);
3836

3937
boolean identified = harness.allIdentified();
@@ -55,7 +53,7 @@ void CanRotateWithUnknownComponent() {
5553
@Test
5654
@Tag("minecraft")
5755
void NonRotationCreatesCopiedInstance() {
58-
TestRecipeLayerBlocks harness = getHarness("layers/mixed/basic_harness.json");
56+
TestRecipeLayerBlocks harness = RecipeTestUtil.getLayerHarness("worlds/basic_harness_5x.json");
5957
Assertions.assertNotNull(harness);
6058

6159
final IRecipeLayerBlocks rotatedHarness = RecipeLayerUtil.rotate(harness, Rotation.NONE);
@@ -69,10 +67,5 @@ void NonRotationCreatesCopiedInstance() {
6967
Assertions.assertNotSame(harness, rotatedHarness);
7068
}
7169

72-
private TestRecipeLayerBlocks getHarness(String filename) {
73-
final JsonElement mixed = FileHelper.INSTANCE.getJsonFromFile(filename);
74-
return TestRecipeLayerBlocks.CODEC
75-
.parse(JsonOps.INSTANCE, mixed)
76-
.getOrThrow(false, Assertions::fail);
77-
}
70+
7871
}

src/test/java/dev/compactmods/crafting/tests/recipes/layers/TestRecipeLayerBlocks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class TestRecipeLayerBlocks implements IRecipeLayerBlocks {
1818
IRecipeLayer worldLayerDef;
1919
final Set<BlockPos> unmatchedStates;
2020

21-
public static final Codec<TestRecipeLayerBlocks> CODEC = new RecipeLayerBlocksTestHarnessCodec();
21+
public static final Codec<TestRecipeLayerBlocks> CODEC = new TestRecipeLayerBlocksCodec();
2222

2323
TestRecipeLayerBlocks() {
2424
this.bounds = AxisAlignedBB.ofSize(0, 0, 0);

src/test/java/dev/compactmods/crafting/tests/recipes/layers/RecipeLayerBlocksTestHarnessCodec.java renamed to src/test/java/dev/compactmods/crafting/tests/recipes/layers/TestRecipeLayerBlocksCodec.java

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
import com.mojang.serialization.DataResult;
88
import com.mojang.serialization.DynamicOps;
99
import dev.compactmods.crafting.CompactCrafting;
10-
import dev.compactmods.crafting.api.components.IRecipeBlockComponent;
11-
import dev.compactmods.crafting.api.components.IRecipeComponent;
1210
import dev.compactmods.crafting.api.recipe.layers.IRecipeLayer;
1311
import dev.compactmods.crafting.api.recipe.layers.dim.IFixedSizedRecipeLayer;
1412
import dev.compactmods.crafting.recipes.MiniaturizationRecipeCodec;
1513
import dev.compactmods.crafting.recipes.components.BlockComponent;
16-
import dev.compactmods.crafting.recipes.components.CCMiniRecipeComponents;
1714
import net.minecraft.util.math.BlockPos;
1815

19-
public class RecipeLayerBlocksTestHarnessCodec implements Codec<TestRecipeLayerBlocks> {
16+
public class TestRecipeLayerBlocksCodec implements Codec<TestRecipeLayerBlocks> {
2017
@Override
2118
public <T> DataResult<Pair<TestRecipeLayerBlocks, T>> decode(DynamicOps<T> ops, T input) {
2219
TestRecipeLayerBlocks harness = new TestRecipeLayerBlocks();
@@ -27,8 +24,6 @@ public <T> DataResult<Pair<TestRecipeLayerBlocks, T>> decode(DynamicOps<T> ops,
2724
return DataResult.error(e.getMessage());
2825
}
2926

30-
loadComponents(ops, input, harness);
31-
3227
// Force unknown positions
3328
BlockPos.CODEC.listOf()
3429
.optionalFieldOf("forcedUnknownPositions")
@@ -39,6 +34,8 @@ public <T> DataResult<Pair<TestRecipeLayerBlocks, T>> decode(DynamicOps<T> ops,
3934
list.forEach(harness::addForcedUnknownPosition);
4035
});
4136

37+
harness.rebuildComponentTotals();
38+
4239
return DataResult.success(Pair.of(harness, input));
4340
}
4441

@@ -57,35 +54,17 @@ private <T> void loadWorldData(DynamicOps<T> ops, T input, TestRecipeLayerBlocks
5754
harness.bounds = ((IFixedSizedRecipeLayer) world).getDimensions();
5855
harness.worldLayerDef = world;
5956

60-
final Map<String, BlockComponent> worldBlocks = Codec.unboundedMap(Codec.STRING, BlockComponent.CODEC)
61-
.fieldOf("worldBlocks")
57+
final Map<String, BlockComponent> blocks = Codec.unboundedMap(Codec.STRING, BlockComponent.CODEC)
58+
.fieldOf("blocks")
6259
.codec()
6360
.parse(ops, input)
6461
.getOrThrow(false, CompactCrafting.RECIPE_LOGGER::error);
6562

66-
for (Map.Entry<String, BlockComponent> e : worldBlocks.entrySet()) {
63+
for (Map.Entry<String, BlockComponent> e : blocks.entrySet()) {
6764
e.getValue().getFirstMatch().ifPresent(bs -> harness.states.put(e.getKey(), bs));
6865
}
6966
}
7067

71-
private <T> void loadComponents(DynamicOps<T> ops, T input, TestRecipeLayerBlocks harness) {
72-
final Map<String, IRecipeComponent> components = Codec.unboundedMap(Codec.STRING, MiniaturizationRecipeCodec.COMPONENT_CODEC)
73-
.fieldOf("components")
74-
.codec()
75-
.parse(ops, input)
76-
.getOrThrow(false, CompactCrafting.RECIPE_LOGGER::error);
77-
78-
CCMiniRecipeComponents test = new CCMiniRecipeComponents();
79-
components.forEach((ck, c) -> {
80-
if (c instanceof IRecipeBlockComponent)
81-
test.registerBlock(ck, (IRecipeBlockComponent) c);
82-
else
83-
test.registerOther(ck, c);
84-
});
85-
86-
harness.rebuildComponentTotals();
87-
}
88-
8968
@Override
9069
public <T> DataResult<T> encode(TestRecipeLayerBlocks input, DynamicOps<T> ops, T prefix) {
9170
return DataResult.error("Not supported");

src/test/java/dev/compactmods/crafting/tests/recipes/util/RecipeTestUtil.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package dev.compactmods.crafting.tests.recipes.util;
22

3+
import java.util.Map;
4+
import java.util.Optional;
35
import com.google.gson.JsonElement;
6+
import com.mojang.serialization.Codec;
47
import com.mojang.serialization.JsonOps;
58
import dev.compactmods.crafting.CompactCrafting;
69
import dev.compactmods.crafting.recipes.MiniaturizationRecipe;
10+
import dev.compactmods.crafting.recipes.components.BlockComponent;
11+
import dev.compactmods.crafting.recipes.components.CCMiniRecipeComponents;
12+
import dev.compactmods.crafting.tests.recipes.layers.TestRecipeLayerBlocks;
713
import dev.compactmods.crafting.tests.util.FileHelper;
814
import org.junit.jupiter.api.Assertions;
915

10-
import java.util.Optional;
11-
1216
public class RecipeTestUtil {
1317
public static MiniaturizationRecipe getRecipeFromFile(String filename) {
1418
JsonElement json = FileHelper.INSTANCE.getJsonFromFile(filename);
@@ -23,4 +27,26 @@ public static MiniaturizationRecipe getRecipeFromFile(String filename) {
2327
return loaded.get();
2428
}
2529
}
30+
31+
public static TestRecipeLayerBlocks getLayerHarness(String filename) {
32+
final JsonElement data = FileHelper.INSTANCE.getJsonFromFile(filename);
33+
return TestRecipeLayerBlocks.CODEC
34+
.parse(JsonOps.INSTANCE, data)
35+
.getOrThrow(false, Assertions::fail);
36+
}
37+
38+
public static CCMiniRecipeComponents getComponentsFromHarness(String filename) {
39+
final JsonElement data = FileHelper.INSTANCE.getJsonFromFile(filename);
40+
41+
final Map<String, BlockComponent> blocks = Codec.unboundedMap(Codec.STRING, BlockComponent.CODEC)
42+
.fieldOf("blocks")
43+
.codec()
44+
.parse(JsonOps.INSTANCE, data)
45+
.getOrThrow(false, CompactCrafting.RECIPE_LOGGER::error);
46+
47+
CCMiniRecipeComponents components = new CCMiniRecipeComponents();
48+
blocks.forEach(components::registerBlock);
49+
50+
return components;
51+
}
2652
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"type": "compactcrafting:filled",
3-
"component":"Gl"
3+
"component":"G"
44
}

src/test/resources/layers/filled/tests/filled_basic.json

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "compactcrafting:hollow",
3+
"wall": "W"
4+
}

src/test/resources/layers/hollow/hollow.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)