| 1 | package net.minecraft.gametest.framework;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.Iterator;
|
| 5 | import java.util.List;
|
| 6 | import java.util.function.Supplier;
|
| 7 | import net.minecraft.network.chat.Component;
|
| 8 |
|
| 9 | public class GameTestSequence {
|
| 10 | private final GameTestInfo parent;
|
| 11 | private final List<GameTestEvent> events = Lists.newArrayList();
|
| 12 | private int lastTick;
|
| 13 |
|
| 14 | public GameTestSequence(final GameTestInfo parent) {
|
| 15 | this.parent = parent;
|
| 16 | this.lastTick = parent.getTick();
|
| 17 | }
|
| 18 |
|
| 19 | public GameTestSequence thenWaitUntil(final Runnable assertion) {
|
| 20 | this.events.add(GameTestEvent.create(assertion));
|
| 21 | return this;
|
| 22 | }
|
| 23 |
|
| 24 | public GameTestSequence thenWaitUntil(final long expectedDelay, final Runnable assertion) {
|
| 25 | this.events.add(GameTestEvent.create(expectedDelay, assertion));
|
| 26 | return this;
|
| 27 | }
|
| 28 |
|
| 29 | public GameTestSequence thenWaitAtLeast(final long minimumDelay, final Runnable assertion) {
|
| 30 | this.events.add(GameTestEvent.createWithMinimumDelay(minimumDelay, assertion));
|
| 31 | return this;
|
| 32 | }
|
| 33 |
|
| 34 | public GameTestSequence thenIdle(final int delta) {
|
| 35 | return this.thenExecuteAfter(delta, () -> {});
|
| 36 | }
|
| 37 |
|
| 38 | public GameTestSequence thenExecute(final Runnable assertion) {
|
| 39 | this.events.add(GameTestEvent.create(() -> this.executeWithoutFail(assertion)));
|
| 40 | return this;
|
| 41 | }
|
| 42 |
|
| 43 | public GameTestSequence thenExecuteAfter(final int delta, final Runnable after) {
|
| 44 | this.events.add(GameTestEvent.create(() -> {
|
| 45 | if (this.parent.getTick() < this.lastTick + delta) {
|
| 46 | throw new GameTestAssertException(Component.translatable("test.error.sequence.not_completed"), this.parent.getTick());
|
| 47 | }
|
| 48 |
|
| 49 | this.executeWithoutFail(after);
|
| 50 | }));
|
| 51 | return this;
|
| 52 | }
|
| 53 |
|
| 54 | public GameTestSequence thenExecuteFor(final int delta, final Runnable check) {
|
| 55 | this.events.add(GameTestEvent.create(() -> {
|
| 56 | if (this.parent.getTick() < this.lastTick + delta) {
|
| 57 | this.executeWithoutFail(check);
|
| 58 | throw new GameTestAssertException(Component.translatable("test.error.sequence.not_completed"), this.parent.getTick());
|
| 59 | }
|
| 60 | }));
|
| 61 | return this;
|
| 62 | }
|
| 63 |
|
| 64 | public void thenSucceed() {
|
| 65 | this.events.add(GameTestEvent.create(this.parent::succeed));
|
| 66 | }
|
| 67 |
|
| 68 | public void thenFail(final Supplier<GameTestException> e) {
|
| 69 | this.events.add(GameTestEvent.create(() -> this.parent.fail(e.get())));
|
| 70 | }
|
| 71 |
|
| 72 | public GameTestSequence.Condition thenTrigger() {
|
| 73 | GameTestSequence.Condition result = new GameTestSequence.Condition();
|
| 74 | this.events.add(GameTestEvent.create(() -> result.trigger(this.parent.getTick())));
|
| 75 | return result;
|
| 76 | }
|
| 77 |
|
| 78 | public void tickAndContinue(final int tick) {
|
| 79 | try {
|
| 80 | this.tick(tick);
|
| 81 | } catch (GameTestAssertException var3) {
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public void tickAndFailIfNotComplete(final int tick) {
|
| 86 | try {
|
| 87 | this.tick(tick);
|
| 88 | } catch (GameTestAssertException e) {
|
| 89 | this.parent.fail(e);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | private void executeWithoutFail(final Runnable assertion) {
|
| 94 | try {
|
| 95 | assertion.run();
|
| 96 | } catch (GameTestAssertException e) {
|
| 97 | this.parent.fail(e);
|
| 98 | }
|
| 99 | }
|
| 100 |
|
| 101 | private void tick(final int tick) {
|
| 102 | Iterator<GameTestEvent> iterator = this.events.iterator();
|
| 103 |
|
| 104 | while (iterator.hasNext()) {
|
| 105 | GameTestEvent event = iterator.next();
|
| 106 | event.assertion.run();
|
| 107 | iterator.remove();
|
| 108 | int delay = tick - this.lastTick;
|
| 109 | int prevTick = this.lastTick;
|
| 110 | this.lastTick = tick;
|
| 111 | if (event.minimumDelay != null && event.minimumDelay > delay) {
|
| 112 | this.parent.fail(new GameTestAssertException(Component.translatable("test.error.sequence.minimum_tick", prevTick + event.minimumDelay), tick));
|
| 113 | break;
|
| 114 | }
|
| 115 |
|
| 116 | if (event.expectedDelay != null && event.expectedDelay != delay) {
|
| 117 | this.parent.fail(new GameTestAssertException(Component.translatable("test.error.sequence.invalid_tick", prevTick + event.expectedDelay), tick));
|
| 118 | break;
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public class Condition {
|
| 124 | private static final int NOT_TRIGGERED = -1;
|
| 125 | private int triggerTime = -1;
|
| 126 |
|
| 127 | public void trigger(final int time) {
|
| 128 | if (this.triggerTime != -1) {
|
| 129 | throw new IllegalStateException("Condition already triggered at " + this.triggerTime);
|
| 130 | }
|
| 131 |
|
| 132 | this.triggerTime = time;
|
| 133 | }
|
| 134 |
|
| 135 | public void assertTriggeredThisTick() {
|
| 136 | int tick = GameTestSequence.this.parent.getTick();
|
| 137 | if (this.triggerTime != tick) {
|
| 138 | if (this.triggerTime == -1) {
|
| 139 | throw new GameTestAssertException(Component.translatable("test.error.sequence.condition_not_triggered"), tick);
|
| 140 | } else {
|
| 141 | throw new GameTestAssertException(Component.translatable("test.error.sequence.condition_already_triggered", this.triggerTime), tick);
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 | }
|
| 147 |
|