Skip to content

Commit a0bb7ae

Browse files
committed
move bad layout tests to its own populator
1 parent 6fb1659 commit a0bb7ae

File tree

1 file changed

+158
-108
lines changed

1 file changed

+158
-108
lines changed

src/test/java/org/spongepowered/common/recipe/RecipePlaceTest.java

Lines changed: 158 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -130,88 +130,104 @@ private static Stream<TestContext> streamCraftingRecipes() {
130130
&& stack.maxStackQuantity() == bigPearl.maxStackQuantity(),
131131
pearl);
132132

133-
return Stream.<TestPopulator>of(
134-
new DefaultedTestPopulator(new TestContext("regular_shaped_crafting", CraftingRecipe.shapedBuilder()
133+
return Stream.of(
134+
Stream.of(new TestContext("regular_shaped_crafting", CraftingRecipe.shapedBuilder()
135135
.aisle("S S", " P ")
136136
.where('S', stoneIngredient)
137137
.where('P', anyPearlIngredient)
138138
.result(result)))
139-
140-
.expectCrafts(8, 16)
141-
.expectInput(List.of(
142-
stone, empty, stone,
143-
empty, bigPearl, empty,
144-
empty, empty, empty))
145-
146-
.partialInventory(List.of(stone64, bigPearl8, bigPearl8))
147-
.partialInput(List.of(
148-
stone64, empty, empty,
149-
empty, bigPearl4, empty,
150-
empty, empty, empty))
151-
152-
.badInventory(Collections.nCopies(9, bedrock))
153-
.badInput(Collections.nCopies(9, bedrock)),
154-
155-
new DefaultedTestPopulator(new TestContext("custom_shaped_crafting", CraftingRecipe.shapedBuilder()
139+
.flatMap(test -> Stream.of(
140+
new DefaultedTestPopulator(test)
141+
.expectCrafts(8, 16)
142+
.expectInput(List.of(
143+
stone, empty, stone,
144+
empty, bigPearl, empty,
145+
empty, empty, empty))
146+
147+
.partialInventory(List.of(stone64, bigPearl8, bigPearl8))
148+
.partialInput(List.of(
149+
stone64, empty, empty,
150+
empty, bigPearl4, empty,
151+
empty, empty, empty))
152+
.badInput(Collections.nCopies(9, bedrock)),
153+
new BadLayoutTestPopulator(test)
154+
.badInventory(Collections.nCopies(9, bedrock))
155+
.badInput(Collections.nCopies(9, bedrock))
156+
)).flatMap(TestPopulator::populate),
157+
158+
Stream.of(new TestContext("custom_shaped_crafting", CraftingRecipe.shapedBuilder()
156159
.aisle("SSS", "BBB", "SSS")
157160
.where('S', smallPearlIngredient)
158161
.where('B', bigPearlIngredient)
159162
.result(result)))
163+
.flatMap(test -> Stream.of(
164+
new DefaultedTestPopulator(test)
165+
.expectCrafts(4, 4)
166+
.expectInput(List.of(
167+
smallPearl, smallPearl, smallPearl,
168+
bigPearl, bigPearl, bigPearl,
169+
smallPearl, smallPearl, smallPearl))
170+
171+
.partialInventory(List.of(smallPearl32, bigPearl, bigPearl, bigPearl, bigPearl, smallPearl32))
172+
.partialInput(List.of(
173+
smallPearl4, empty, smallPearl4,
174+
bigPearl4, empty, bigPearl4,
175+
smallPearl, empty, empty))
176+
.badInput(Collections.nCopies(9, pearl)),
177+
new BadLayoutTestPopulator(test)
178+
.badInventory(List.of(pearl16))
179+
.badInput(Collections.nCopies(9, pearl))
180+
))
181+
.flatMap(TestPopulator::populate),
160182

161-
.expectCrafts(4, 4)
162-
.expectInput(List.of(
163-
smallPearl, smallPearl, smallPearl,
164-
bigPearl, bigPearl, bigPearl,
165-
smallPearl, smallPearl, smallPearl))
166-
167-
.partialInventory(List.of(smallPearl32, bigPearl, bigPearl, bigPearl, bigPearl, smallPearl32))
168-
.partialInput(List.of(
169-
smallPearl4, empty, smallPearl4,
170-
bigPearl4, empty, bigPearl4,
171-
smallPearl, empty, empty))
172-
173-
.badInventory(List.of(pearl16))
174-
.badInput(Collections.nCopies(9, pearl)),
175-
176-
new DefaultedTestPopulator(new TestContext("regular_shapeless_crafting", CraftingRecipe.shapelessBuilder()
177-
.addIngredients(anyPearlIngredient, stoneIngredient, anyPearlIngredient)))
178-
179-
.expectCrafts(16, 16)
180-
.expectInput(List.of(
181-
smallPearl, stone, smallPearl,
182-
empty, empty, empty,
183-
empty, empty, empty))
184-
185-
.partialInventory(List.of(smallPearl32))
186-
.partialInput(List.of(
187-
smallPearl4, stone64, empty,
188-
empty, empty, empty,
189-
empty, empty, empty))
190-
191-
.badInventory(Collections.nCopies(10, bedrock))
192-
.badInput(Collections.nCopies(3, bedrock))/*,
183+
Stream.of(new TestContext("regular_shapeless_crafting", CraftingRecipe.shapelessBuilder()
184+
.addIngredients(
185+
anyPearlIngredient, stoneIngredient, anyPearlIngredient)))
186+
.flatMap(test -> Stream.of(
187+
new DefaultedTestPopulator(test)
188+
.expectCrafts(16, 16)
189+
.expectInput(List.of(
190+
smallPearl, stone, smallPearl,
191+
empty, empty, empty,
192+
empty, empty, empty))
193+
194+
.partialInventory(List.of(smallPearl32))
195+
.partialInput(List.of(
196+
smallPearl4, stone64, empty,
197+
empty, empty, empty,
198+
empty, empty, empty))
199+
.badInput(Collections.nCopies(3, bedrock)),
200+
new BadLayoutTestPopulator(test)
201+
.badInventory(Collections.nCopies(10, bedrock))
202+
.badInput(Collections.nCopies(3, bedrock))
203+
))
204+
.flatMap(TestPopulator::populate)/*,
193205
194206
//TODO uncomment after shapeless recipe fix
195-
new DefaultedTestPopulator(new TestContext("custom_shapeless_crafting", CraftingRecipe.shapelessBuilder()
207+
Stream.of(new TestContext("custom_shapeless_crafting", CraftingRecipe.shapelessBuilder()
196208
.addIngredients(
197209
smallPearlIngredient, smallPearlIngredient, smallPearlIngredient,
198210
bigPearlIngredient, bigPearlIngredient, bigPearlIngredient)))
199-
200-
.expectCrafts(8, 16)
201-
.expectInput(List.of(
202-
smallPearl, smallPearl, smallPearl,
203-
bigPearl, bigPearl, bigPearl,
204-
empty, empty, empty))
205-
206-
.partialInventory(Collections.nCopies(5, bigPearl8))
207-
.partialInput(List.of(
208-
smallPearl32, smallPearl32, smallPearl,
209-
bigPearl4, bigPearl4, empty,
210-
empty, empty, empty))
211-
212-
.badInventory(List.of(pearl16))
213-
.badInput(Collections.nCopies(6, pearl))*/
214-
).flatMap(TestPopulator::populate);
211+
.flatMap(test -> Stream.of(
212+
new DefaultedTestPopulator(test)
213+
.expectCrafts(8, 16)
214+
.expectInput(List.of(
215+
smallPearl, smallPearl, smallPearl,
216+
bigPearl, bigPearl, bigPearl,
217+
empty, empty, empty))
218+
219+
.partialInventory(Collections.nCopies(5, bigPearl8))
220+
.partialInput(List.of(
221+
smallPearl32, smallPearl32, smallPearl,
222+
bigPearl4, bigPearl4, empty,
223+
empty, empty, empty))
224+
.badInput(Collections.nCopies(6, pearl)),
225+
new BadLayoutTestPopulator(test)
226+
.badInventory(List.of(pearl16))
227+
.badInput(Collections.nCopies(6, pearl))
228+
))
229+
.flatMap(TestPopulator::populate)*/
230+
).flatMap(Function.identity());
215231
}
216232

217233
private static Stream<TestContext> streamSmeltingRecipes() {
@@ -222,34 +238,43 @@ private static Stream<TestContext> streamSmeltingRecipes() {
222238
bigSnowball.offer(Keys.MAX_STACK_SIZE, 4);
223239
final ItemStack result = ItemStack.of(ItemTypes.BARRIER);
224240

225-
return Stream.<TestPopulator>of(
226-
new DefaultedTestPopulator(new TestContext("regular_smelting", CookingRecipe.builder()
241+
return Stream.of(
242+
Stream.of(new TestContext("regular_smelting", CookingRecipe.builder()
227243
.type(RecipeTypes.SMELTING)
228244
.ingredient(Ingredient.of(snowball.type()))
229245
.result(result)))
246+
.flatMap(test -> Stream.of(
247+
new DefaultedTestPopulator(test)
248+
.expectCrafts(4, 8)
249+
.expectInput(List.of(bigSnowball))
250+
251+
.partialInventory(Collections.nCopies(8, bigSnowball))
252+
.badInput(List.of(bedrock)),
253+
new BadLayoutTestPopulator(test)
254+
.badInventory(List.of(bedrock))
255+
.badInput(List.of(bedrock))
256+
))
257+
.flatMap(TestPopulator::populate),
230258

231-
.expectCrafts(4, 8)
232-
.expectInput(List.of(bigSnowball))
233-
234-
.partialInventory(Collections.nCopies(8, bigSnowball))
235-
.badInventory(List.of(bedrock))
236-
.badInput(List.of(bedrock)),
237-
238-
new DefaultedTestPopulator(new TestContext("custom_smelting", CookingRecipe.builder()
259+
Stream.of(new TestContext("custom_smelting", CookingRecipe.builder()
239260
.type(RecipeTypes.SMELTING)
240261
.ingredient(Ingredient.of(ResourceKey.sponge("big_snowball"),
241262
stack -> stack.type() == bigSnowball.type()
242263
&& stack.maxStackQuantity() == bigSnowball.maxStackQuantity(),
243264
snowball))
244265
.result(result)))
245-
246-
.expectCrafts(4, 8)
247-
.expectInput(List.of(bigSnowball))
248-
249-
.partialInventory(Collections.nCopies(8, bigSnowball))
250-
.badInventory(List.of(snowball4))
251-
.badInput(List.of(snowball4))
252-
).flatMap(TestPopulator::populate);
266+
.flatMap(test -> Stream.of(
267+
new DefaultedTestPopulator(test)
268+
.expectCrafts(4, 8)
269+
.expectInput(List.of(bigSnowball))
270+
.partialInventory(Collections.nCopies(8, bigSnowball))
271+
.badInput(List.of(snowball4)),
272+
new BadLayoutTestPopulator(test)
273+
.badInventory(List.of(snowball4))
274+
.badInput(List.of(snowball4))
275+
))
276+
.flatMap(TestPopulator::populate)
277+
).flatMap(Function.identity());
253278
}
254279

255280
@TestFactory
@@ -265,6 +290,10 @@ public Stream<DynamicTest> generateTests() {
265290
@FunctionalInterface
266291
private interface TestPopulator {
267292

293+
static TestPopulator composite(final Stream<TestPopulator> populators) {
294+
return () -> populators.flatMap(TestPopulator::populate);
295+
}
296+
268297
Stream<TestContext> populate();
269298
}
270299

@@ -308,8 +337,6 @@ private static final class DefaultedTestPopulator implements TestPopulator {
308337
// All of them should be considered for expected crafts
309338
private List<ItemStack> partialInventory = List.of();
310339
private List<ItemStack> partialInput = List.of();
311-
// "Bad" items exist to ensure placer clears input and doesn't pull wrong items if there is no other choice
312-
private List<ItemStack> badInventory = List.of();
313340
private List<ItemStack> badInput = List.of();
314341

315342
public DefaultedTestPopulator(final TestContext base) {
@@ -337,11 +364,6 @@ public DefaultedTestPopulator partialInput(final List<ItemStack> items) {
337364
return this;
338365
}
339366

340-
public DefaultedTestPopulator badInventory(final List<ItemStack> items) {
341-
this.badInventory = items;
342-
return this;
343-
}
344-
345367
public DefaultedTestPopulator badInput(final List<ItemStack> items) {
346368
this.badInput = items;
347369
return this;
@@ -351,22 +373,12 @@ public DefaultedTestPopulator badInput(final List<ItemStack> items) {
351373
public Stream<TestContext> populate() {
352374
final List<ItemStack> totalInitialInventory = Stream.concat(this.partialInventory.stream(), this.partialInput.stream()).toList();
353375
final List<ItemStack> expectedShiftInput = RecipePlaceTest.createExpectedInput(this.expectedInput, this.expectedShiftCrafts);
354-
final List<TestContext> baseInputs = List.of(
355-
this.base.name("Empty input").input(List.of()),
356-
this.base.name("Bad input").input(this.badInput)
357-
);
358376

359-
final Stream<TestContext> testBadLayouts = baseInputs.stream()
377+
final Stream<TestContext> testSingleClick = Stream.of(this.base)
360378
.flatMap(context -> Stream.of(
361-
context.name("Empty inventory").inventory(List.of()),
362-
context.name("Bad inventory").inventory(this.badInventory)
379+
context.name("Empty input").input(List.of()),
380+
context.name("Bad input").input(this.badInput)
363381
))
364-
.flatMap(context -> Stream.of(context, context.shift()))
365-
.flatMap(context -> Stream.of(context, context.creative()))
366-
// 2 clicks is enough to ensure we always get empty input
367-
.map(context -> context.expectInputs(List.of(List.of(), List.of())));
368-
369-
final Stream<TestContext> testSingleClick = baseInputs.stream()
370382
.map(context -> context.name("Total inventory").inventory(totalInitialInventory))
371383
.flatMap(context -> Stream.of(context, context.creative()))
372384
.flatMap(context -> Stream.of(
@@ -400,11 +412,49 @@ public Stream<TestContext> populate() {
400412
context.creative().expectInput(List.of())
401413
));
402414

403-
return Stream.of(testBadLayouts, testSingleClick, testMultipleClicks, testFullInventory)
415+
return Stream.of(testSingleClick, testMultipleClicks, testFullInventory)
404416
.flatMap(Function.identity());
405417
}
406418
}
407419

420+
private static final class BadLayoutTestPopulator implements TestPopulator {
421+
422+
private final TestContext base;
423+
private List<ItemStack> badInventory = List.of();
424+
private List<ItemStack> badInput = List.of();
425+
426+
public BadLayoutTestPopulator(final TestContext base) {
427+
this.base = base;
428+
}
429+
430+
public BadLayoutTestPopulator badInventory(final List<ItemStack> items) {
431+
this.badInventory = items;
432+
return this;
433+
}
434+
435+
public BadLayoutTestPopulator badInput(final List<ItemStack> items) {
436+
this.badInput = items;
437+
return this;
438+
}
439+
440+
@Override
441+
public Stream<TestContext> populate() {
442+
return Stream.of(this.base)
443+
.flatMap(context -> Stream.of(
444+
context.name("Empty input").input(List.of()),
445+
context.name("Bad input").input(this.badInput)
446+
))
447+
.flatMap(context -> Stream.of(
448+
context.name("Empty inventory").inventory(List.of()),
449+
context.name("Bad inventory").inventory(this.badInventory)
450+
))
451+
.flatMap(context -> Stream.of(context, context.shift()))
452+
.flatMap(context -> Stream.of(context, context.creative()))
453+
// 2 clicks is enough to ensure we always get empty input
454+
.map(context -> context.expectInputs(List.of(List.of(), List.of())));
455+
}
456+
}
457+
408458
private record TestContext(
409459
RecipeHolder<?> recipe, String testName,
410460
boolean shiftClick, boolean creativeMode,
@@ -466,7 +516,7 @@ public void testCrafting() {
466516
}
467517

468518
public void testSmelting() {
469-
this.test(MenuType.FURNACE, menu -> ((Inventory) ((AbstractFurnaceMenuAccessor) menu).accessor$container()).slot(0).get());
519+
this.test(MenuType.FURNACE, menu -> ((Inventory) ((AbstractFurnaceMenuAccessor) menu).accessor$container()).slot(0).orElseThrow());
470520
}
471521

472522
public void test(

0 commit comments

Comments
 (0)