Skip to content

Commit e14b62e

Browse files
committed
Error handling test library.
1 parent 01f8f73 commit e14b62e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package mx.kenzie.skript.test;
2+
3+
import mx.kenzie.foundation.language.PostCompileClass;
4+
import mx.kenzie.skript.runtime.Script;
5+
import mx.kenzie.skript.runtime.Skript;
6+
import mx.kenzie.skript.runtime.internal.Member;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
10+
import java.io.ByteArrayInputStream;
11+
import java.nio.charset.StandardCharsets;
12+
13+
public class ErrorHandlingTest extends SkriptTest {
14+
15+
private static final Skript skript = new Skript();
16+
private static Script script;
17+
18+
@BeforeClass
19+
public static void start() throws Throwable {
20+
skript.registerLibrary(new TestingLibrary());
21+
final PostCompileClass cls = skript.compileScript(new ByteArrayInputStream("""
22+
function test_error:
23+
trigger:
24+
set {var} to 1
25+
print "hello"
26+
throw exception
27+
""".getBytes(StandardCharsets.UTF_8)), "skript.error_test");
28+
script = skript.loadScript(cls);
29+
}
30+
31+
@Test
32+
public void basic_use() throws Throwable {
33+
final Member function = script.getFunction("test_error");
34+
assert function != null;
35+
function.invoke();
36+
}
37+
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package mx.kenzie.skript.test;
2+
3+
import mx.kenzie.skript.api.LanguageElement;
4+
import mx.kenzie.skript.api.Library;
5+
import mx.kenzie.skript.api.ModifiableLibrary;
6+
import mx.kenzie.skript.api.syntax.Effect;
7+
import mx.kenzie.skript.compiler.CompileState;
8+
import mx.kenzie.skript.lang.element.StandardElements;
9+
import mx.kenzie.skript.lang.handler.StandardHandlers;
10+
11+
public class TestingLibrary extends ModifiableLibrary {
12+
public TestingLibrary() {
13+
super("testing");
14+
this.registerSyntax(CompileState.CODE_BODY, new ThrowException(this));
15+
}
16+
17+
public static class ThrowException extends Effect {
18+
19+
public ThrowException(Library provider) {
20+
super(provider, StandardElements.EFFECT, "throw exception");
21+
handlers.put(StandardHandlers.RUN, findMethod(ThrowException.class, "error"));
22+
}
23+
24+
public static void error() {
25+
throw new RuntimeException("Testing error.");
26+
}
27+
}
28+
29+
}

0 commit comments

Comments
 (0)