Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/main/java/io/github/misode/packtest/PackTestException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -144,7 +146,7 @@ private void registerPoll(Supplier<AssertResult> 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() {
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/io/github/misode/packtest/PackTestFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -77,17 +77,24 @@ private static void parseDirective(
}
}

private static ContextChain<CommandSourceStack> parseCommand(
private static Step parseCommand(
CommandDispatcher<CommandSourceStack> dispatcher,
CommandSourceStack context,
String command) throws CommandSyntaxException {
String command,
int line) throws CommandSyntaxException {
ParseResults<CommandSourceStack> parseResults = dispatcher.parse(command, context);
Commands.validateParseResults(parseResults);
return ContextChain.tryFlatten(parseResults.getContext().build(command))
ContextChain<CommandSourceStack> 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<CommandSourceStack> chain, int line) {}
public record Step(String command, ContextChain<CommandSourceStack> chain, int line, String commandName) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Loading