Skip to content

Commit b166378

Browse files
committed
Add test tests.
1 parent 1e97746 commit b166378

File tree

5 files changed

+112
-28
lines changed

5 files changed

+112
-28
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2021 ByteSkript org (Moderocky)
3+
* View the full licence information and permissions:
4+
* https://github.com/Moderocky/ByteSkript/blob/master/LICENSE
5+
*/
6+
7+
package org.byteskript.skript.compiler.structure;
8+
9+
import mx.kenzie.foundation.MethodBuilder;
10+
import org.byteskript.skript.api.SyntaxElement;
11+
import org.byteskript.skript.compiler.Context;
12+
import org.byteskript.skript.error.ScriptCompileError;
13+
import org.objectweb.asm.Label;
14+
15+
public class TestTree extends ProgrammaticSplitTree {
16+
17+
private final SectionMeta owner;
18+
private final MultiLabel end = new MultiLabel();
19+
private boolean open;
20+
private Label next = new Label();
21+
22+
public TestTree(SectionMeta owner) {
23+
this.owner = owner;
24+
this.open = true;
25+
}
26+
27+
@Override
28+
public SectionMeta owner() {
29+
return owner;
30+
}
31+
32+
public Label getNext() {
33+
return next;
34+
}
35+
36+
public MultiLabel getEnd() {
37+
return end;
38+
}
39+
40+
@Override
41+
public void start(Context context) {
42+
43+
}
44+
45+
@Override
46+
public void branch(Context context) {
47+
48+
}
49+
50+
@Override
51+
public void close(Context context) {
52+
this.open = false;
53+
final MethodBuilder method = context.getMethod();
54+
if (method == null) throw new ScriptCompileError(context.lineNumber(), "Extraction tree left unclosed.");
55+
method.writeCode(end.instruction());
56+
context.removeTree(this);
57+
}
58+
59+
@Override
60+
public boolean permit(SyntaxElement element) {
61+
return false;
62+
}
63+
64+
@Override
65+
public boolean isOpen() {
66+
return open;
67+
}
68+
69+
}

src/main/java/org/byteskript/skript/lang/syntax/test/TestEffect.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import org.byteskript.skript.compiler.Pattern;
1616
import org.byteskript.skript.compiler.SkriptLangSpec;
1717
import org.byteskript.skript.compiler.structure.MultiLabel;
18+
import org.byteskript.skript.compiler.structure.ProgrammaticSplitTree;
19+
import org.byteskript.skript.compiler.structure.TestTree;
1820
import org.byteskript.skript.compiler.structure.TryCatchTree;
1921
import org.byteskript.skript.error.ScriptCompileError;
2022
import org.byteskript.skript.lang.element.StandardElements;
@@ -49,7 +51,7 @@ public CompileState getSubState() {
4951

5052
@Override
5153
public void preCompile(Context context, Pattern.Match match) throws Throwable {
52-
final TryCatchTree tree = new TryCatchTree(context.getSection(1), new MultiLabel());
54+
final TestTree tree = new TestTree(context.getSection());
5355
context.createTree(tree);
5456
tree.start(context);
5557
final MethodBuilder method = context.getMethod();
@@ -61,16 +63,7 @@ public void preCompile(Context context, Pattern.Match match) throws Throwable {
6163

6264
@Override
6365
public void compile(Context context, Pattern.Match match) throws Throwable {
64-
final TryCatchTree tree = context.findTree(TryCatchTree.class);
65-
final Label label = tree.getEnd().use();
66-
final Label next = tree.getStartCatch();
67-
final MethodBuilder method = context.getMethod();
68-
if (method == null) throw new ScriptCompileError(context.lineNumber(), "Test effect used outside method.");
69-
context.getMethod().writeCode(((writer, visitor) -> {
70-
visitor.visitJumpInsn(Opcodes.GOTO, label);
71-
visitor.visitLabel(next);
72-
}));
73-
method.writeCode(WriteInstruction.invoke(ExtractedSyntaxCalls.class.getMethod("handleTestError", Throwable.class)));
66+
final TestTree tree = context.findTree(TestTree.class);
7467
tree.close(context);
7568
context.setState(CompileState.CODE_BODY);
7669
}

src/main/java/org/byteskript/skript/runtime/threading/ScriptExceptionHandler.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,27 @@ public void uncaughtException(Thread source, Throwable throwable) {
2525
if (throwable instanceof ScriptParseError error) { // these already look pretty.
2626
error.printStackTrace(System.err);
2727
} else if (source instanceof ScriptThread thread) {
28+
final StringBuilder builder = new StringBuilder();
2829
final Class<?> start = thread.initiator;
29-
System.err.println("An error occurred while running a script.");
30-
System.err.println("\t" + throwable.getMessage());
30+
builder.append("An error occurred while running a script.\n");
31+
builder.append("\t").append(throwable.getClass().getSimpleName()).append(": ")
32+
.append(throwable.getMessage()).append("\n");
3133
if (start != null)
32-
System.err.println("This program started in: " + BLACK_BACKGROUND + YELLOW + start.getName()
33-
.replace('.', '/') + ".bsk" + RESET);
34+
builder.append("This program started in: " + BLACK_BACKGROUND + YELLOW).append(start.getName()
35+
.replace('.', '/')).append(".bsk").append(RESET).append("\n");
3436
final StackTraceElement[] elements = throwable.getStackTrace();
3537
if (elements == null || elements.length < 1) return;
36-
System.err.println("The error came from:");
38+
builder.append("The error came from:" + "\n");
3739
if (elements[0].getClassName().startsWith("skript.")) {
38-
System.err.println("\t'" + RED + elements[0].getClassName() + RESET + "' line " + CYAN + elements[0].getLineNumber() + RESET);
39-
System.err.println("\t(This is from Skript code.)" + RESET);
40+
builder.append("\t'" + RED).append(elements[0].getClassName()).append(RESET).append("' line ")
41+
.append(CYAN).append(elements[0].getLineNumber()).append(RESET).append("\n");
42+
builder.append("\t(This is from Skript code.)" + RESET + "\n");
4043
} else {
41-
System.err.println("\t'" + RED + elements[0].getClassName() + RESET + "' line " + CYAN + elements[0].getLineNumber() + RESET);
42-
System.err.println("\t(This is from a Java library.)" + RESET);
44+
builder.append("\t'" + RED + elements[0].getClassName() + RESET + "' line " + CYAN + elements[0].getLineNumber() + RESET + "\n");
45+
builder.append("\t(This is from a Java library.)" + RESET + "\n");
4346
}
44-
System.err.println("Below is the list of trigger calls that caused this error.");
45-
System.err.println("The top line was the most recent call.");
47+
builder.append("Below is the list of trigger calls that caused this error.\n");
48+
builder.append("The top line was the most recent call.\n");
4649
for (final StackTraceElement element : elements) {
4750
final String location = element.getClassName();
4851
if (!location.startsWith("skript.")) continue;
@@ -79,9 +82,12 @@ public void uncaughtException(Thread source, Throwable throwable) {
7982
.append(CYAN)
8083
.append(element.getLineNumber())
8184
.append(RESET);
82-
System.err.println(error);
85+
builder.append(error);
8386
}
84-
} else {
87+
synchronized (System.err) {
88+
System.err.println(builder);
89+
}
90+
} else synchronized (System.err) {
8591
System.err.print("Exception in thread \""
8692
+ source.getName() + "\" ");
8793
throwable.printStackTrace(System.err);

src/test/java/org/byteskript/skript/test/GenericTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,18 @@ public void all() throws Throwable {
7777
final long now, then;
7878
final Script script = skript.loadScripts(classes).iterator().next();
7979
now = System.currentTimeMillis();
80-
final Object object = script.getFunction("test").run(skript).get();
81-
final boolean result = Boolean.TRUE.equals(object);
80+
final boolean result;
81+
synchronized (System.err) {
82+
final Object object = script.getFunction("test").run(skript).get();
83+
result = Boolean.TRUE.equals(object);
84+
}
8285
then = System.currentTimeMillis();
8386
if (result)
8487
System.out.println(ConsoleColour.GREEN + "\t✓ " + ConsoleColour.RESET + "Run in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
85-
else
88+
else {
8689
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Run in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
90+
failure++;
91+
}
8792
} catch (Throwable ex) {
8893
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to run.");
8994
errors.add(ex);
@@ -102,7 +107,7 @@ public void all() throws Throwable {
102107
}
103108
for (final Throwable error : errors)
104109
synchronized (this) {
105-
error.printStackTrace(System.out);
110+
error.printStackTrace(System.err);
106111
}
107112
}
108113
assert failure < 1 : failure + " tests have failed.";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
function test:
3+
trigger:
4+
set {var} to 1
5+
set system property "skript.test_mode" to "false"
6+
test: set {var} to 2
7+
assert {var} is 1
8+
set system property "skript.test_mode" to "true"
9+
test: set {var} to 2
10+
assert {var} is 2
11+
return true

0 commit comments

Comments
 (0)