Skip to content

Commit 3b01885

Browse files
committed
Revert "make tests more flexible"
This reverts commit 1a94490.
1 parent 1a94490 commit 3b01885

1 file changed

Lines changed: 30 additions & 81 deletions

File tree

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

Lines changed: 30 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public Stream<DynamicTest> testRecipes() {
263263

264264
private static final class TestPopulator {
265265

266-
private TestContext base;
266+
private final TestContext base;
267267

268268
private int expectedRegularCrafts;
269269
// This exists due to vanilla having a bug with shift-placing that
@@ -290,11 +290,6 @@ public TestPopulator(final RecipeHolder<?> recipe) {
290290
this.base = new TestContext(recipe);
291291
}
292292

293-
public TestPopulator test(final TestEntry test) {
294-
this.base = this.base.test(test);
295-
return this;
296-
}
297-
298293
public TestPopulator expectCrafts(final int regularCrafts, final int shiftCrafts) {
299294
this.expectedRegularCrafts = regularCrafts;
300295
this.expectedShiftCrafts = shiftCrafts;
@@ -341,13 +336,13 @@ public Stream<TestContext> populate() {
341336
))
342337
.flatMap(context -> Stream.of(context, context.shift()))
343338
// 2 clicks is enough to ensure we always fail
344-
.map(context -> context.clicks(2).expectInputs(List.of(List.of(), List.of())));
339+
.map(context -> context.expectInputs(List.of(List.of(), List.of())));
345340

346341
final Stream<TestContext> toMatchSingleClick = baseInputs.stream()
347342
.map(context -> context.name("Total inventory").inventory(totalInitialInventory))
348343
.flatMap(context -> Stream.of(
349-
context.expectInputAt(0, RecipePlaceTest.createExpectedInput(this.expectedInput, 1)),
350-
context.shift().expectInputAt(0, expectedShiftInput)
344+
context.expectInput(RecipePlaceTest.createExpectedInput(this.expectedInput, 1)),
345+
context.shift().expectInput(expectedShiftInput)
351346
));
352347

353348
// After first click we end up with the same layout no matter the initial input.
@@ -358,82 +353,56 @@ public Stream<TestContext> populate() {
358353
.name("Partial inventory").inventory(this.partialInventory))
359354
.flatMap(context -> Stream.of(
360355
context
361-
.clicks(this.expectedRegularCrafts+1)
362-
.expectInputs(Stream.concat(
363-
IntStream.rangeClosed(1, this.expectedRegularCrafts)
364-
.mapToObj(clicks -> RecipePlaceTest.createExpectedInput(this.expectedInput, clicks)),
365-
Stream.of(RecipePlaceTest.createExpectedInput(this.expectedInput, this.expectedRegularCrafts))
366-
).toList()),
367-
context.shift().clicks(2).expectInputs(List.of(expectedShiftInput, expectedShiftInput))
356+
.expectInputs(IntStream.rangeClosed(1, this.expectedRegularCrafts)
357+
.mapToObj(clicks -> RecipePlaceTest.createExpectedInput(this.expectedInput, clicks))
358+
.toList())
359+
.expectInput(RecipePlaceTest.createExpectedInput(this.expectedInput, this.expectedRegularCrafts)),
360+
context.shift().expectInputs(List.of(expectedShiftInput, expectedShiftInput))
368361
));
369362

370363
return Stream.concat(toFail, Stream.concat(toMatchSingleClick, toMatchMultipleClicks));
371364
}
372365
}
373366

374367
private record TestContext(
375-
RecipeHolder<?> recipe, String testName,
376-
boolean shiftClick, int clicks,
368+
RecipeHolder<?> recipe, String testName, boolean shiftClick,
377369
List<ItemStack> inventory, List<ItemStack> input,
378-
List<TestEntry> tests
370+
List<List<ItemStack>> expectedInputs
379371
) {
380372
public TestContext(final RecipeHolder<?> recipe) {
381-
this(recipe, "", false, 1, List.of(), List.of(), List.of());
373+
this(recipe, "", false, List.of(), List.of(), List.of());
382374
}
383375

384376
public TestContext name(final String testName) {
385377
final String newTestName = this.testName.isEmpty() ? testName : (this.testName + ", " + testName);
386-
return new TestContext(this.recipe, newTestName, this.shiftClick, this.clicks, this.inventory, this.input, this.tests);
378+
return new TestContext(this.recipe, newTestName, this.shiftClick, this.inventory, this.input, this.expectedInputs);
387379
}
388380

389381
public TestContext shift() {
390-
return new TestContext(this.recipe, this.testName, true, this.clicks, this.inventory, this.input, this.tests);
391-
}
392-
393-
public TestContext clicks(final int clicks) {
394-
return new TestContext(this.recipe, this.testName, this.shiftClick, clicks, this.inventory, this.input, this.tests);
382+
return new TestContext(this.recipe, this.testName, true, this.inventory, this.input, this.expectedInputs);
395383
}
396384

397385
public TestContext inventory(final List<ItemStack> items) {
398-
return new TestContext(this.recipe, this.testName, this.shiftClick, this.clicks, items, this.input, this.tests);
386+
return new TestContext(this.recipe, this.testName, this.shiftClick, items, this.input, this.expectedInputs);
399387
}
400388

401389
public TestContext input(final List<ItemStack> items) {
402-
return new TestContext(this.recipe, this.testName, this.shiftClick, this.clicks, this.inventory, items, this.tests);
403-
}
404-
405-
public TestContext test(final TestEntry test) {
406-
final List<TestEntry> newTests = Stream.concat(this.tests.stream(), Stream.of(test)).toList();
407-
return new TestContext(this.recipe, this.testName, this.shiftClick, this.clicks, this.inventory, this.input, newTests);
408-
}
409-
410-
public TestContext testAt(final int clickToTest, final TestEntry test) {
411-
return this.test((context, input, click) -> {
412-
if (click != clickToTest) {
413-
return true;
414-
}
415-
416-
return test.test(context, input, click);
417-
});
390+
return new TestContext(this.recipe, this.testName, this.shiftClick, this.inventory, items, this.expectedInputs);
418391
}
419392

420-
public TestContext expectInputAt(final int clickToTest, final List<ItemStack> expectedInput) {
421-
return this.testAt(clickToTest, TestEntry.matchInput(expectedInput));
393+
public TestContext expectInputs(final List<List<ItemStack>> expectedInputs) {
394+
return new TestContext(this.recipe, this.testName, this.shiftClick, this.inventory, this.input, expectedInputs);
422395
}
423396

424-
public TestContext expectInputs(final List<List<ItemStack>> expectedInputs) {
425-
TestContext test = this;
426-
for (int i = 0; i < expectedInputs.size(); ++i) {
427-
test = test.expectInputAt(i, expectedInputs.get(i));
428-
}
429-
return test;
397+
public TestContext expectInput(final List<ItemStack> expectedInput) {
398+
return this.expectInputs(Stream.concat(this.expectedInputs.stream(), Stream.of(expectedInput)).toList());
430399
}
431400

432401
public String asTestName() {
433402
return String.format("Place recipe %s (Shift click: %s, Total clicks: %s, %s)",
434403
this.recipe.id().location().getPath(),
435404
this.shiftClick,
436-
this.clicks,
405+
this.expectedInputs.size(),
437406
this.testName);
438407
}
439408

@@ -473,54 +442,34 @@ public void test(
473442
input.set(i, initialInput.get(i));
474443
}
475444

476-
for (int i = 0; i < this.clicks(); ++i) {
445+
for (int i = 0; i < this.expectedInputs().size(); ++i) {
477446
menu.handlePlacement(this.shiftClick(), true, this.recipe(), player.serverLevel(), player.getInventory());
478-
for (final TestEntry test : this.tests()) {
479-
if (!test.test(this, input, i)) {
480-
break;
481-
}
482-
}
483-
}
484-
}
485-
}
486-
487-
@FunctionalInterface
488-
private interface TestEntry {
489447

490-
static TestEntry matchInput(final List<ItemStack> expectedInput) {
491-
return (context, input, click) -> {
492448
final List<ItemStack> actualInput = input.slots().stream().map(Slot::peek).toList();
449+
final List<ItemStack> expectedInput = this.expectedInputs().get(i);
450+
final int click = i+1;
493451
for (int j = 0; j < actualInput.size(); ++j) {
494452
final ItemStack actualStack = actualInput.get(j);
495453
final ItemStack expectedStack = expectedInput.size() <= j ? ItemStack.empty() : expectedInput.get(j);
496454
assertTrue(net.minecraft.world.item.ItemStack.matches(
497455
ItemStackUtil.toNative(actualStack), ItemStackUtil.toNative(expectedStack)),
498456
() -> String.format("""
499-
Actual input doesn't match expected input after click %s
457+
Actual input doesn't match expected input after click %s
500458
Test: %s,
501459
Expected input: %s
502460
Actual input: %s
503461
Initial input: %s
504462
Initial inventory: %s""",
505-
click+1,
506-
context.asTestName(),
463+
click,
464+
this.asTestName(),
507465
RecipePlaceTest.stacksToString(false, expectedInput),
508466
RecipePlaceTest.stacksToString(false, actualInput),
509-
RecipePlaceTest.stacksToString(false, context.input()),
510-
RecipePlaceTest.stacksToString(true, context.inventory())
467+
RecipePlaceTest.stacksToString(false, initialInput),
468+
RecipePlaceTest.stacksToString(true, initialInventory)
511469
));
512470
}
513-
514-
return true;
515-
};
471+
}
516472
}
517-
518-
/**
519-
* Performs the test on recipe input after each placement. Clicks start at 0.
520-
*
521-
* @return True if the following tests should be performed within the given click
522-
*/
523-
boolean test(TestContext context, Inventory input, int click);
524473
}
525474

526475
private static final class FakePlayer extends ServerPlayer {

0 commit comments

Comments
 (0)