Skip to content

Commit 6720fbd

Browse files
committed
Begin writing individual tests.
1 parent 2e0bc34 commit 6720fbd

File tree

17 files changed

+227
-28
lines changed

17 files changed

+227
-28
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.byteskript</groupId>
88
<artifactId>byteskript</artifactId>
9-
<version>1.0.25</version>
9+
<version>1.0.26</version>
1010
<name>ByteSkript</name>
1111
<description>A compiled JVM implementation of the Skript language.</description>
1212

src/main/java/org/byteskript/skript/lang/syntax/comparison/IsArray.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66

77
package org.byteskript.skript.lang.syntax.comparison;
88

9-
import mx.kenzie.foundation.MethodBuilder;
109
import mx.kenzie.foundation.Type;
1110
import org.byteskript.skript.api.note.Documentation;
1211
import org.byteskript.skript.api.syntax.RelationalExpression;
13-
import org.byteskript.skript.compiler.*;
12+
import org.byteskript.skript.compiler.CommonTypes;
13+
import org.byteskript.skript.compiler.Context;
14+
import org.byteskript.skript.compiler.Pattern;
15+
import org.byteskript.skript.compiler.SkriptLangSpec;
1416
import org.byteskript.skript.lang.element.StandardElements;
17+
import org.byteskript.skript.lang.handler.StandardHandlers;
18+
import org.byteskript.skript.runtime.internal.OperatorHandler;
1519

1620
@Documentation(
1721
name = "Is Array",
@@ -27,7 +31,14 @@
2731
public class IsArray extends RelationalExpression {
2832

2933
public IsArray() {
30-
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Object% (is|are) a[n] array");
34+
super(SkriptLangSpec.LIBRARY, StandardElements.EXPRESSION, "%Object% (is|are) a[n] array");
35+
try {
36+
handlers.put(StandardHandlers.FIND, OperatorHandler.class.getMethod("isArray", Object.class));
37+
handlers.put(StandardHandlers.GET, OperatorHandler.class.getMethod("isArray", Object.class));
38+
} catch (NoSuchMethodException e) {
39+
e.printStackTrace();
40+
}
41+
// todo something is wrong with this syntax
3142
}
3243

3344
@Override
@@ -36,17 +47,6 @@ public Pattern.Match match(String thing, Context context) {
3647
return super.match(thing, context);
3748
}
3849

39-
@Override
40-
public void compile(Context context, Pattern.Match match) throws Throwable {
41-
final MethodBuilder method = context.getMethod();
42-
method.writeCode((writer, visitor) -> {
43-
visitor.visitMethodInsn(182, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
44-
visitor.visitMethodInsn(182, "java/lang/Class", "isArray", "()Z", false);
45-
visitor.visitMethodInsn(184, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
46-
});
47-
context.setState(CompileState.STATEMENT);
48-
}
49-
5050
@Override
5151
public Type getReturnType() {
5252
return CommonTypes.BOOLEAN;

src/main/java/org/byteskript/skript/runtime/Skript.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public Future<?> runScript(final Runnable executable) {
416416
""")
417417
@GenerateExample
418418
public Future<?> runScript(final ScriptRunner runner) {
419-
return runScript(runner, null);
419+
return this.runScript(runner, null);
420420
}
421421

422422
@Description("""
@@ -437,6 +437,7 @@ public Future<?> runScript(final ScriptRunner runner, final Event event) {
437437
thread.event = event;
438438
try {
439439
runner.run();
440+
future.value(runner.result());
440441
} catch (ThreadDeath ignore) {
441442
// This is likely from an exit the current process effect, we don't want to make noise
442443
} finally {

src/main/java/org/byteskript/skript/runtime/internal/Member.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,24 @@ public Class<? extends CompiledScript> owner() {
177177
""")
178178
public Future<?> run(Skript skript, Object... arguments) {
179179
final ScriptRunner runner = new ScriptRunner() {
180+
181+
private Object value;
182+
183+
@Override
184+
public Object result() {
185+
synchronized (this) {
186+
return value;
187+
}
188+
}
189+
180190
@Override
181191
public void start() {
182-
invoker.invoke(arguments);
192+
final Object result = invoker.invoke(arguments);
193+
synchronized (this) {
194+
this.value = result;
195+
}
183196
}
184-
197+
185198
@Override
186199
public Class<? extends CompiledScript> owner() {
187200
return script.mainClass();

src/main/java/org/byteskript/skript/runtime/internal/OperatorHandler.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,13 @@ public static Number root(Object a) {
209209
}
210210
//endregion
211211

212-
//region Comparisons
212+
public static Boolean equals(Object a, Object b) {
213+
if (Objects.equals(a, b)) return true;
214+
if (isArray(a) && isArray(b)) return Arrays.equals((Object[]) a, (Object[]) b);
215+
if (!(a instanceof Number x) || !(b instanceof Number y)) return false;
216+
return Double.compare(x.doubleValue(), y.doubleValue()) == 0;
217+
}
218+
213219
public static Boolean gt(Object a, Object b) {
214220
if (!(a instanceof Number x) || !(b instanceof Number y)) {
215221
throw new ScriptRuntimeError("Provided inputs must be numerical.");
@@ -303,10 +309,10 @@ public static Boolean matches(Object a, Object b) {
303309
return equals(a, b); // probably a mistaken use?
304310
}
305311

306-
public static Boolean equals(Object a, Object b) {
307-
if (Objects.equals(a, b)) return true;
308-
if (!(a instanceof Number x) || !(b instanceof Number y)) return false;
309-
return Double.compare(x.doubleValue(), y.doubleValue()) == 0;
312+
//region Comparisons
313+
public static Boolean isArray(Object a) {
314+
if (a == null) return false;
315+
return a.getClass().isArray();
310316
}
311317
//endregion
312318

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@
1414
import java.util.concurrent.TimeUnit;
1515
import java.util.concurrent.TimeoutException;
1616

17-
public class ScriptFinishFuture implements Future<Void> {
17+
public class ScriptFinishFuture implements Future<Object> {
1818

1919
private final Skript skript;
2020
private final Object lock = new Object();
2121
public ScriptThread thread;
22+
protected Object value;
2223

2324
public ScriptFinishFuture(Skript skript) {
2425
this.skript = skript;
2526
}
2627

28+
public void value(Object object) {
29+
synchronized (this) {
30+
this.value = object;
31+
}
32+
}
33+
2734
public void finish() {
2835
synchronized (lock) {
2936
lock.notify();
@@ -49,18 +56,22 @@ public boolean isDone() {
4956
}
5057

5158
@Override
52-
public Void get() throws InterruptedException, ExecutionException {
59+
public Object get() throws InterruptedException, ExecutionException {
5360
synchronized (lock) {
5461
lock.wait();
5562
}
56-
return null;
63+
synchronized (this) {
64+
return value;
65+
}
5766
}
5867

5968
@Override
60-
public Void get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
69+
public Object get(long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
6170
synchronized (lock) {
6271
lock.wait(unit.toMillis(timeout));
6372
}
64-
return null;
73+
synchronized (this) {
74+
return value;
75+
}
6576
}
6677
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ default void run() {
2121
thread.variables.clear();
2222
}
2323

24+
default Object result() {
25+
return null;
26+
}
27+
2428
void start();
2529

2630
Class<? extends CompiledScript> owner();

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
import org.junit.BeforeClass;
1414
import org.junit.Test;
1515

16+
import java.io.File;
17+
import java.io.FileOutputStream;
18+
import java.io.InputStream;
19+
import java.io.OutputStream;
20+
import java.net.URI;
21+
import java.nio.file.*;
22+
import java.util.Collections;
23+
import java.util.Iterator;
24+
1625
public class GenericTest extends SkriptTest {
1726

1827
private static final Skript skript = new Skript();
@@ -25,6 +34,55 @@ public static void start() throws Throwable {
2534
script = skript.loadScript(cls);
2635
}
2736

37+
@Test
38+
public void all() throws Throwable {
39+
final URI uri = GenericTest.class.getClassLoader().getResource("tests").toURI();
40+
final Path path;
41+
if (uri.getScheme().equals("jar")) {
42+
final FileSystem system = FileSystems.newFileSystem(uri, Collections.emptyMap());
43+
path = system.getPath("tests");
44+
} else {
45+
path = Paths.get(uri);
46+
}
47+
final Iterator<Path> iterator = Files.walk(path, 1).iterator();
48+
int failure = 0;
49+
while (iterator.hasNext()) {
50+
final Path file = iterator.next();
51+
if (!file.toString().endsWith(".bsk")) continue;
52+
final String part = file.toString().substring(file.toString().indexOf("/tests/") + 7);
53+
final String name = part.substring(0, part.length() - 4).replace(File.separatorChar, '.');
54+
try (final InputStream stream = Files.newInputStream(file)) {
55+
final PostCompileClass cls;
56+
synchronized (this) {
57+
try {
58+
cls = skript.compileScript(stream, "skript." + name);
59+
} catch (Throwable ex) {
60+
System.err.println("Error in '" + name + "':");
61+
ex.printStackTrace(System.err);
62+
failure++;
63+
continue;
64+
}
65+
try {
66+
final Script script = skript.loadScript(cls);
67+
final boolean result = (boolean) script.getFunction("test").run(skript).get();
68+
assert result : "Test failed.";
69+
} catch (Throwable ex) {
70+
System.err.println("Error in '" + name + "':");
71+
ex.printStackTrace(System.err);
72+
failure++;
73+
}
74+
}
75+
final File test = new File("target/test-scripts/" + cls.name() + ".class");
76+
test.getParentFile().mkdirs();
77+
if (!test.exists()) test.createNewFile();
78+
try (final OutputStream output = new FileOutputStream(test)) {
79+
output.write(cls.code());
80+
}
81+
}
82+
}
83+
assert failure < 1 : failure + " tests have failed.";
84+
}
85+
2886
@Test
2987
public void generic_expressions() throws Throwable {
3088
final Member function = script.getFunction("generic_expressions");

src/test/resources/tests/contains.bsk

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
function test:
3+
trigger:
4+
assert "hello" contains "h": "Contains string part failed."
5+
assert "hello" contains "ello": "Contains string part failed."
6+
assert "hello" contains "hello": "Contains string self failed."
7+
assert "hello" contains "there" is false: "Contains string incorrectly succeeded."
8+
assert "hello" contains "helo" is false: "Contains string incorrectly succeeded."
9+
assert (1, 2, 3) contains 1: "Contains array failed."
10+
assert (1, 2, 3) contains 3: "Contains array failed."
11+
assert (1, 2, 3) contains 4 is false: "Contains array failed."
12+
set {list} to a new list
13+
add 1 to {list}
14+
add 2 to {list}
15+
assert {list} contains 1: "Contains list failed."
16+
assert {list} contains 2: "Contains list failed."
17+
assert {x} contains {x} is false: "Nothing contains nothing."
18+
return true

src/test/resources/tests/exists.bsk

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
function test:
3+
trigger:
4+
assert "hello" exists: "Exists check failed."
5+
assert "" exists: "Exists check failed."
6+
assert {x} exists is false: "Exists check failed."
7+
assert true exists: "Exists check failed."
8+
assert false exists: "Exists check failed."
9+
set {var} to true
10+
assert {var} exists: "Exists check failed."
11+
assert 1 exists: "Exists check failed."
12+
assert (1, 2, 3) exists: "Exists check failed."
13+
return true

0 commit comments

Comments
 (0)