Skip to content

Commit 9742f23

Browse files
authored
Fix AdvancedDetectorCoverTest (#4451)
1 parent 8bf5f32 commit 9742f23

File tree

2 files changed

+51
-12
lines changed

2 files changed

+51
-12
lines changed

src/test/java/com/gregtechceu/gtceu/common/cover/AdvancedDetectorCoverTest.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.gregtechceu.gtceu.common.cover;
22

33
import com.gregtechceu.gtceu.GTCEu;
4+
import com.gregtechceu.gtceu.api.capability.IWorkable;
45
import com.gregtechceu.gtceu.api.machine.IMachineBlockEntity;
56
import com.gregtechceu.gtceu.api.machine.MetaMachine;
67
import com.gregtechceu.gtceu.api.machine.SimpleTieredMachine;
@@ -20,6 +21,8 @@
2021
import net.neoforged.neoforge.gametest.GameTestHolder;
2122
import net.neoforged.neoforge.gametest.PrefixGameTestTemplate;
2223

24+
import org.apache.commons.lang3.mutable.MutableInt;
25+
2326
/**
2427
* The "electrolyzer" template contains a creative tank with water,
2528
* that is set to auto-output into an electrolyzer when supplied with a redstone signal
@@ -30,16 +33,24 @@
3033
@GameTestHolder(GTCEu.MOD_ID)
3134
public class AdvancedDetectorCoverTest {
3235

33-
@GameTest(template = "electrolyzer", batch = "coverTests", required = false)
34-
public static void BLOCKED_BY_LDLIB_WEIRDNESS_PROBABLY_testAdvancedActivityDetectorCover(GameTestHelper helper) {
36+
@GameTest(template = "electrolyzer", batch = "coverTests")
37+
public static void testAdvancedActivityDetectorCoverWithActivity(GameTestHelper helper) {
3538
helper.pullLever(new BlockPos(2, 2, 2));
3639
MetaMachine machine = ((IMachineBlockEntity) helper.getBlockEntity(new BlockPos(1, 2, 1))).getMetaMachine();
3740
TestUtils.placeCover(helper, machine, GTItems.COVER_ACTIVITY_DETECTOR_ADVANCED.asStack(), Direction.WEST);
38-
helper.runAtTickTime(30, () -> helper.assertRedstoneSignal(
39-
new BlockPos(1, 2, 1),
40-
Direction.WEST,
41-
signal -> signal > 0,
42-
() -> "expected redstone signal"));
41+
MutableInt expected = new MutableInt();
42+
helper.runAtTickTime(40 - machine.getOffsetTimer() % 20, () -> {
43+
IWorkable workable = (IWorkable) machine;
44+
expected.setValue(Math.round(15f * workable.getProgress() / workable.getMaxProgress()));
45+
});
46+
helper.runAtTickTime(41 - machine.getOffsetTimer() % 20, () -> {
47+
// due to this cover updating only once every 20 ticks, we need to check multiple values
48+
TestUtils.assertRedstoneEither(helper, new BlockPos(0, 2, 1),
49+
(expected.intValue() + 13) % 15,
50+
(expected.intValue() + 14) % 15,
51+
expected.intValue());
52+
helper.succeed();
53+
});
4354
}
4455

4556
@GameTest(template = "electrolyzer", batch = "coverTests", required = false)

src/test/java/com/gregtechceu/gtceu/gametest/util/TestUtils.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import net.minecraft.core.Direction;
2525
import net.minecraft.core.MappedRegistry;
2626
import net.minecraft.core.registries.BuiltInRegistries;
27+
import net.minecraft.gametest.framework.GameTestAssertPosException;
2728
import net.minecraft.gametest.framework.GameTestHelper;
2829
import net.minecraft.network.chat.MutableComponent;
2930
import net.minecraft.world.item.ItemStack;
@@ -112,7 +113,7 @@ public static boolean areItemStacksEqual(List<ItemStack> stack1, List<ItemStack>
112113
* @return {@code true} if fluids and amounts are equal
113114
*/
114115
public static boolean isFluidStackEqual(FluidStack stack1, FluidStack stack2) {
115-
return stack1.isFluidEqual(stack2) && stack1.getAmount() == stack2.getAmount();
116+
return FluidStack.isSameFluid(stack1, stack2) && stack1.getAmount() == stack2.getAmount();
116117
}
117118

118119
/**
@@ -121,7 +122,7 @@ public static boolean isFluidStackEqual(FluidStack stack1, FluidStack stack2) {
121122
* @return {@code true} if items are equal, and if stack2's amount is within range.
122123
*/
123124
public static boolean isFluidStackWithinRange(FluidStack stack1, FluidStack stack2, int min, int max) {
124-
return stack1.isFluidEqual(stack2) && isFluidWithinRange(stack2, min, max);
125+
return FluidStack.isSameFluid(stack1, stack2) && isFluidWithinRange(stack2, min, max);
125126
}
126127

127128
/**
@@ -271,10 +272,10 @@ public static void assertEqual(GameTestHelper helper, ItemStack stack1, ItemStac
271272
}
272273

273274
public static void assertEqual(GameTestHelper helper, FluidStack stack1, FluidStack stack2) {
274-
helper.assertTrue(stack1.isFluidStackIdentical(stack2),
275+
helper.assertTrue(FluidStack.matches(stack1, stack2),
275276
"Fluid stacks not equal: \"%s %d\" != \"%s %d\"".formatted(
276-
stack1.getDisplayName().getString(), stack1.getAmount(),
277-
stack2.getDisplayName().getString(), stack2.getAmount()));
277+
stack1.getHoverName().getString(), stack1.getAmount(),
278+
stack2.getHoverName().getString(), stack2.getAmount()));
278279
}
279280

280281
public static void assertLampOn(GameTestHelper helper, BlockPos pos) {
@@ -317,4 +318,31 @@ public static void succeedAfterTest(GameTestHelper helper, long timeout) {
317318
public static void assertEqual(GameTestHelper helper, @Nullable BlockPos pos1, @Nullable BlockPos pos2) {
318319
helper.assertTrue(pos1 != null && pos1.equals(pos2), "Expected %s to equal to %s".formatted(pos1, pos2));
319320
}
321+
322+
public static void assertRedstone(GameTestHelper helper, BlockPos pos, int min, int max) {
323+
BlockPos absolutePos = helper.absolutePos(pos);
324+
int strength = helper.getLevel().getBestNeighborSignal(absolutePos);
325+
if (strength > max || strength < min) {
326+
throw new GameTestAssertPosException(
327+
"Expected redstone signal between %d and %d, got %d".formatted(min, max, strength),
328+
absolutePos, pos, helper.getTick());
329+
}
330+
}
331+
332+
public static void assertRedstoneEither(GameTestHelper helper, BlockPos pos, int... values) {
333+
BlockPos absolutePos = helper.absolutePos(pos);
334+
int strength = helper.getLevel().getBestNeighborSignal(absolutePos);
335+
boolean pass = false;
336+
for (int i : values) {
337+
if (i == strength) {
338+
pass = true;
339+
break;
340+
}
341+
}
342+
if (!pass) {
343+
throw new GameTestAssertPosException(
344+
"Expected redstone signal to be one of %s, got %d".formatted(values, strength),
345+
absolutePos, pos, helper.getTick());
346+
}
347+
}
320348
}

0 commit comments

Comments
 (0)