From 3095ddf2ddcff8d0f4468de42faaac149db05669 Mon Sep 17 00:00:00 2001 From: MulverineX Date: Wed, 7 Oct 2026 18:23:59 -0600 Subject: [PATCH] Add command name to test failure message --- .../misode/packtest/PackTestException.java | 17 ++++++++++++----- .../misode/packtest/PackTestExecutor.java | 4 +++- .../misode/packtest/PackTestFunction.java | 17 ++++++++++++----- .../packtest/mixin/LogTestReporterMixin.java | 2 +- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/misode/packtest/PackTestException.java b/src/main/java/io/github/misode/packtest/PackTestException.java index bef44cd..3486cfb 100644 --- a/src/main/java/io/github/misode/packtest/PackTestException.java +++ b/src/main/java/io/github/misode/packtest/PackTestException.java @@ -6,18 +6,25 @@ public class PackTestException extends GameTestAssertException { final int line; + final String command; - public PackTestException(Component message, int tick, int line) { + public PackTestException(Component message, int tick, int line, String command) { super(message, tick); this.line = line; + this.command = command; } - public int getLine() { - return this.line; + public String getLine() { + return this.command.isEmpty() + ? " on line " + this.line + : " \"" + this.command + "\" on line " + this.line; } @Override public @NonNull Component getDescription() { - return Component.literal("On line " + this.line + ": ").append(super.getDescription()); + String prefix = this.command.isEmpty() + ? "On line " + this.line + ": " + : "\"" + this.command + "\" on line " + this.line + ": "; + return Component.literal(prefix).append(super.getDescription()); } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/misode/packtest/PackTestExecutor.java b/src/main/java/io/github/misode/packtest/PackTestExecutor.java index 3f2f3e5..a73000b 100644 --- a/src/main/java/io/github/misode/packtest/PackTestExecutor.java +++ b/src/main/java/io/github/misode/packtest/PackTestExecutor.java @@ -31,6 +31,7 @@ public class PackTestExecutor { private final int timeout; private final long chatSequence = ChatRecorder.sequence(); private int line = 0; + private String commandName = ""; private boolean done = false; public PackTestExecutor(GameTestHelper helper, int timeout) { @@ -59,6 +60,7 @@ public void run(PackTestFunction function) { while (!steps.isEmpty() && !this.done && this.awaits.isEmpty()) { PackTestFunction.Step step = steps.poll(); this.line = step.line(); + this.commandName = step.commandName(); Commands.executeCommandInContext(source, ctx -> ExecutionContext.queueInitialCommandExecution(ctx, step.command(), step.chain(), source, CommandResultCallback.EMPTY)); } @@ -144,7 +146,7 @@ private void registerPoll(Supplier check, boolean negated) { } private PackTestException failure(Component message) { - return new PackTestException(message, (int)this.helper.getTick(), this.line); + return new PackTestException(message, (int)this.helper.getTick(), this.line, this.commandName); } private boolean isLastTick() { diff --git a/src/main/java/io/github/misode/packtest/PackTestFunction.java b/src/main/java/io/github/misode/packtest/PackTestFunction.java index 973dd78..add8d4e 100644 --- a/src/main/java/io/github/misode/packtest/PackTestFunction.java +++ b/src/main/java/io/github/misode/packtest/PackTestFunction.java @@ -53,7 +53,7 @@ public static PackTestFunction fromLines( } try { - steps.add(new Step(command, parseCommand(dispatcher, context, command), line)); + steps.add(parseCommand(dispatcher, context, command, line)); } catch (CommandSyntaxException e) { throw new IllegalArgumentException("Whilst parsing command on line " + line + ": " + e.getMessage()); } @@ -77,17 +77,24 @@ private static void parseDirective( } } - private static ContextChain parseCommand( + private static Step parseCommand( CommandDispatcher dispatcher, CommandSourceStack context, - String command) throws CommandSyntaxException { + String command, + int line) throws CommandSyntaxException { ParseResults parseResults = dispatcher.parse(command, context); Commands.validateParseResults(parseResults); - return ContextChain.tryFlatten(parseResults.getContext().build(command)) + ContextChain chain = ContextChain.tryFlatten(parseResults.getContext().build(command)) .orElseThrow(() -> CommandSyntaxException.BUILT_IN_EXCEPTIONS .dispatcherUnknownCommand() .createWithContext(parseResults.getReader())); + String commandName = chain.getTopContext().getLastChild().getNodes().stream() + .map(n -> n.getNode().getName()) + .filter(n -> !n.isEmpty()) + .findFirst() + .orElse(""); + return new Step(command, chain, line, commandName); } - public record Step(String command, ContextChain chain, int line) {} + public record Step(String command, ContextChain chain, int line, String commandName) {} } diff --git a/src/main/java/io/github/misode/packtest/mixin/LogTestReporterMixin.java b/src/main/java/io/github/misode/packtest/mixin/LogTestReporterMixin.java index 9a9f80d..a45e9c5 100644 --- a/src/main/java/io/github/misode/packtest/mixin/LogTestReporterMixin.java +++ b/src/main/java/io/github/misode/packtest/mixin/LogTestReporterMixin.java @@ -31,7 +31,7 @@ private void onTestFailed(GameTestInfo testInfo, CallbackInfo ci) { if (PackTest.isAutoEnabled()) { String testName = testInfo.id().toString(); String lineNumber = testInfo.getError() instanceof PackTestException err - ? " on line " + err.getLine() + ? err.getLine() : ""; String message = Util.describeError(testInfo.getError()); if (testInfo.isRequired()) {