Skip to content

Commit 89508cc

Browse files
committed
Current unit tests (non-MC) passing
1 parent 900c669 commit 89508cc

14 files changed

+332
-241
lines changed

src/test/java/dev/compactmods/crafting/tests/GameTestsMain.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public class GameTestsMain {
2020
public static void registerTests(final RegisterGameTestsEvent game) {
2121
CompactCrafting.LOGGER.debug("Registering game tests.");
2222

23+
// try {
24+
// var f = new File("test-report.xml");
25+
// GlobalTestReporter.replaceWith(new JUnitLikeTestReporter(f));
26+
// } catch (ParserConfigurationException e) {
27+
// e.printStackTrace();
28+
// }
29+
2330
// projectors
2431
game.register(Projectors.class);
2532

src/test/java/dev/compactmods/crafting/tests/recipes/MiniaturizationRecipes.java

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,26 @@
1313
import dev.compactmods.crafting.recipes.MiniaturizationRecipe;
1414
import dev.compactmods.crafting.recipes.blocks.RecipeBlocks;
1515
import dev.compactmods.crafting.recipes.setup.FakeInventory;
16-
import dev.compactmods.crafting.server.ServerConfig;
1716
import dev.compactmods.crafting.tests.recipes.util.RecipeTestUtil;
1817
import dev.compactmods.crafting.util.BlockSpaceUtil;
1918
import net.minecraft.core.BlockPos;
2019
import net.minecraft.gametest.framework.GameTest;
2120
import net.minecraft.gametest.framework.GameTestHelper;
2221
import net.minecraft.resources.ResourceLocation;
2322
import net.minecraft.world.item.ItemStack;
24-
import net.minecraft.world.item.crafting.RecipeManager;
2523
import net.minecraft.world.level.block.Blocks;
2624
import net.minecraft.world.level.block.state.BlockState;
27-
import net.minecraftforge.server.ServerLifecycleHooks;
2825
import org.junit.jupiter.api.Assertions;
2926
import org.junit.jupiter.api.Test;
3027

3128
// @MCTestClass
3229
public class MiniaturizationRecipes {
3330

34-
private static RecipeManager RECIPES;
35-
36-
@org.junit.jupiter.api.BeforeAll
37-
static void BeforeAllTests() {
38-
ServerConfig.RECIPE_REGISTRATION.set(true);
39-
ServerConfig.RECIPE_MATCHING.set(true);
40-
ServerConfig.FIELD_BLOCK_CHANGES.set(true);
41-
42-
RECIPES = ServerLifecycleHooks.getCurrentServer().getRecipeManager();
43-
}
44-
4531
@Nullable
46-
private static MiniaturizationRecipe getRecipe(String name) {
47-
return (MiniaturizationRecipe) RECIPES.byKey(new ResourceLocation("compactcrafting", name)).orElse(null);
32+
private static MiniaturizationRecipe getRecipe(GameTestHelper testHelper, String name) {
33+
return (MiniaturizationRecipe) testHelper.getLevel().getRecipeManager()
34+
.byKey(new ResourceLocation("compactcrafting", name))
35+
.orElse(null);
4836
}
4937

5038
@Test
@@ -84,15 +72,22 @@ public static void FakesFakeInventories(final GameTestHelper helper) {
8472
});
8573
}
8674

87-
@Test
88-
void FakesAssemble() {
75+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
76+
public static void FakesAssemble(final GameTestHelper test) {
8977
MiniaturizationRecipe recipe = new MiniaturizationRecipe();
9078
Assertions.assertNotNull(recipe);
9179

92-
Assertions.assertDoesNotThrow(() -> {
80+
try {
9381
ItemStack result = recipe.assemble(new FakeInventory());
94-
Assertions.assertTrue(result.isEmpty());
95-
});
82+
if(!result.isEmpty())
83+
test.fail("Expected an empty result");
84+
}
85+
86+
catch(Exception e) {
87+
test.fail(e.getMessage());
88+
}
89+
90+
test.succeed();
9691
}
9792

9893
@Test
@@ -106,8 +101,8 @@ void FakesCanCraftDimensions() {
106101
});
107102
}
108103

109-
@Test
110-
void FakesResultItem() {
104+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
105+
public static void FakesResultItem(final GameTestHelper test) {
111106
MiniaturizationRecipe recipe = new MiniaturizationRecipe();
112107
Assertions.assertNotNull(recipe);
113108

@@ -117,9 +112,9 @@ void FakesResultItem() {
117112
});
118113
}
119114

120-
@Test
121-
void RecipeSuppliesBasicMinecraftRegistrationInfo() {
122-
final MiniaturizationRecipe enderCrystal = getRecipe("ender_crystal");
115+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
116+
public static void RecipeSuppliesBasicMinecraftRegistrationInfo(final GameTestHelper test) {
117+
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
123118
Assertions.assertNotNull(enderCrystal);
124119

125120
final var serializer = Assertions.assertDoesNotThrow(enderCrystal::getSerializer);
@@ -130,18 +125,18 @@ void RecipeSuppliesBasicMinecraftRegistrationInfo() {
130125
}
131126

132127

133-
@Test
134-
void RecipeReturnsEmptyIfLayerNotRegistered() {
135-
final MiniaturizationRecipe enderCrystal = getRecipe("ender_crystal");
128+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
129+
public static void RecipeReturnsEmptyIfLayerNotRegistered(final GameTestHelper test) {
130+
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
136131
Assertions.assertNotNull(enderCrystal);
137132

138133
final Optional<IRecipeLayer> layer = Assertions.assertDoesNotThrow(() -> enderCrystal.getLayer(999));
139134
Assertions.assertFalse(layer.isPresent());
140135
}
141136

142-
@Test
143-
void FitsInCorrectFieldSizes() {
144-
final MiniaturizationRecipe enderCrystal = getRecipe("ender_crystal");
137+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
138+
public static void FitsInCorrectFieldSizes(final GameTestHelper test) {
139+
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
145140
Assertions.assertNotNull(enderCrystal);
146141

147142
MiniaturizationFieldSize[] badSizes = new MiniaturizationFieldSize[]{
@@ -159,9 +154,9 @@ void FitsInCorrectFieldSizes() {
159154
Assertions.assertTrue(enderCrystal.fitsInFieldSize(gs), "Did not fit in field size: " + gs);
160155
}
161156

162-
@Test
163-
void CanGetComponentTotals() {
164-
final MiniaturizationRecipe recipe = getRecipe("ender_crystal");
157+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
158+
public static void CanGetComponentTotals(final GameTestHelper test) {
159+
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
165160
Assertions.assertNotNull(recipe);
166161

167162
final Map<String, Integer> totals = Assertions.assertDoesNotThrow(recipe::getComponentTotals);
@@ -178,18 +173,18 @@ void CanGetComponentTotals() {
178173
Assertions.assertEquals(1, totalObsidian);
179174
}
180175

181-
@Test
182-
void UnregisteredBlockReturnsZeroCount() {
183-
final MiniaturizationRecipe recipe = getRecipe("ender_crystal");
176+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
177+
public static void UnregisteredBlockReturnsZeroCount(final GameTestHelper test) {
178+
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
184179
Assertions.assertNotNull(recipe);
185180

186181
final int required = Assertions.assertDoesNotThrow(() -> recipe.getComponentRequiredCount("?"));
187182
Assertions.assertEquals(0, required);
188183
}
189184

190-
@Test
191-
void HasCraftingTime() {
192-
final MiniaturizationRecipe recipe = getRecipe("ender_crystal");
185+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
186+
public static void HasCraftingTime(final GameTestHelper test) {
187+
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
193188
Assertions.assertNotNull(recipe);
194189

195190
final int required = Assertions.assertDoesNotThrow(recipe::getCraftingTime);
@@ -198,11 +193,11 @@ void HasCraftingTime() {
198193

199194

200195
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
201-
public static void MatchesExactStructure(final GameTestHelper helper) {
202-
final BlockPos zero = helper.relativePos(BlockPos.ZERO);
203-
final MiniaturizationRecipe enderCrystal = getRecipe("ender_crystal");
196+
public static void MatchesExactStructure(final GameTestHelper test) {
197+
final BlockPos zero = test.relativePos(BlockPos.ZERO);
198+
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
204199
final IRecipeBlocks blocks = RecipeBlocks
205-
.create(helper.getLevel(), enderCrystal.getComponents(), enderCrystal.getDimensions().move(zero))
200+
.create(test.getLevel(), enderCrystal.getComponents(), enderCrystal.getDimensions().move(zero))
206201
.normalize();
207202

208203
Assertions.assertDoesNotThrow(() -> {
@@ -213,15 +208,15 @@ public static void MatchesExactStructure(final GameTestHelper helper) {
213208

214209

215210
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
216-
public static void RecipeFailsIfUnidentifiedBlock(final GameTestHelper helper) {
217-
final MiniaturizationRecipe enderCrystal = getRecipe("ender_crystal");
211+
public static void RecipeFailsIfUnidentifiedBlock(final GameTestHelper test) {
212+
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
218213
Assertions.assertNotNull(enderCrystal);
219214

220215
// Force an unknown component in the exact center
221-
helper.setBlock(new BlockPos(2, 2, 2), Blocks.GOLD_BLOCK.defaultBlockState());
216+
test.setBlock(new BlockPos(2, 2, 2), Blocks.GOLD_BLOCK.defaultBlockState());
222217

223218
final IRecipeBlocks blocks = RecipeBlocks
224-
.create(helper.getLevel(), enderCrystal.getComponents(), RecipeTestUtil.getFieldBounds(MiniaturizationFieldSize.MEDIUM, helper))
219+
.create(test.getLevel(), enderCrystal.getComponents(), RecipeTestUtil.getFieldBounds(MiniaturizationFieldSize.MEDIUM, test))
225220
.normalize();
226221

227222
Assertions.assertDoesNotThrow(() -> {
@@ -230,9 +225,9 @@ public static void RecipeFailsIfUnidentifiedBlock(final GameTestHelper helper) {
230225
});
231226
}
232227

233-
@Test
234-
void CanStreamLayerInfo() {
235-
final MiniaturizationRecipe enderCrystal = getRecipe("ender_crystal");
228+
@GameTest(template = "empty_medium", templateNamespace = CompactCrafting.MOD_ID, prefixTemplateWithClassname = false)
229+
public static void CanStreamLayerInfo(final GameTestHelper test) {
230+
final MiniaturizationRecipe enderCrystal = getRecipe(test, "ender_crystal");
236231
final Stream<IRecipeLayer> strem = Assertions.assertDoesNotThrow(enderCrystal::getLayers);
237232

238233
Assertions.assertNotNull(strem);
@@ -242,12 +237,12 @@ void CanStreamLayerInfo() {
242237
}
243238

244239
@GameTest(template = "recipes/ender_crystal", templateNamespace = CompactCrafting.MOD_ID)
245-
public static void RecipeFailsIfDifferentDimensions(final GameTestHelper helper) {
246-
final MiniaturizationRecipe recipe = getRecipe("compact_walls");
240+
public static void RecipeFailsIfDifferentDimensions(final GameTestHelper test) {
241+
final MiniaturizationRecipe recipe = getRecipe(test, "compact_walls");
247242
Assertions.assertNotNull(recipe);
248243

249244
final IRecipeBlocks blocks = RecipeBlocks
250-
.create(helper.getLevel(), recipe.getComponents(), RecipeTestUtil.getFieldBounds(MiniaturizationFieldSize.MEDIUM, helper))
245+
.create(test.getLevel(), recipe.getComponents(), RecipeTestUtil.getFieldBounds(MiniaturizationFieldSize.MEDIUM, test))
251246
.normalize();
252247

253248
final boolean matched = Assertions.assertDoesNotThrow(() -> recipe.matches(blocks));
@@ -256,16 +251,16 @@ public static void RecipeFailsIfDifferentDimensions(final GameTestHelper helper)
256251

257252

258253
@GameTest(template = "recipes/empty_medium", templateNamespace = CompactCrafting.MOD_ID)
259-
public static void RecipeFailsIfNoRotationsMatched(final GameTestHelper helper) {
260-
final MiniaturizationRecipe recipe = getRecipe("ender_crystal");
254+
public static void RecipeFailsIfNoRotationsMatched(final GameTestHelper test) {
255+
final MiniaturizationRecipe recipe = getRecipe(test, "ender_crystal");
261256
Assertions.assertNotNull(recipe);
262257

263258
// Set up the 8 corners to be glass, so block creation below matches field boundaries
264259
final BlockState glass = Blocks.GLASS.defaultBlockState();
265-
BlockSpaceUtil.getCornersOfBounds(MiniaturizationFieldSize.MEDIUM).forEach(p -> helper.setBlock(p, glass));
260+
BlockSpaceUtil.getCornersOfBounds(MiniaturizationFieldSize.MEDIUM).forEach(p -> test.setBlock(p, glass));
266261

267262
final IRecipeBlocks blocks = RecipeBlocks
268-
.create(helper.getLevel(), recipe.getComponents(), RecipeTestUtil.getFieldBounds(MiniaturizationFieldSize.MEDIUM, helper))
263+
.create(test.getLevel(), recipe.getComponents(), RecipeTestUtil.getFieldBounds(MiniaturizationFieldSize.MEDIUM, test))
269264
.normalize();
270265

271266
final boolean matched = Assertions.assertDoesNotThrow(() -> recipe.matches(blocks));

0 commit comments

Comments
 (0)