Skip to content

Commit 1e97746

Browse files
committed
Add script tests.
1 parent 1095a5b commit 1e97746

File tree

8 files changed

+88
-49
lines changed

8 files changed

+88
-49
lines changed

src/main/java/org/byteskript/skript/lang/syntax/script/LoadScriptEffect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public LoadScriptEffect() {
4343
@SuppressWarnings("deprecation")
4444
public static void loadScript(Object object, String name) throws IOException {
4545
if (object instanceof File file)
46-
Skript.localInstance().compileLoad(file, name);
46+
Skript.localInstance().compileLoad(file, name.replace('/', '.'));
4747
else if (object instanceof String string)
4848
Skript.localInstance()
49-
.compileLoad(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), name);
49+
.compileLoad(new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8)), name.replace('/', '.'));
5050
}
5151

5252
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected static Unsafe getUnsafe() {
4040

4141
@Deprecated
4242
protected static void graveyard(final Object object) {
43+
if (object instanceof Record) return;
4344
final Field[] fields = object.getClass().getDeclaredFields();
4445
synchronized (object) {
4546
UNSAFE.fullFence();

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

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,57 +50,60 @@ public void all() throws Throwable {
5050
final Iterator<Path> iterator = Files.walk(path, 1).iterator();
5151
int failure = 0;
5252
final List<Throwable> errors = new ArrayList<>();
53-
while (iterator.hasNext()) {
54-
final Path file = iterator.next();
55-
if (!file.toString().endsWith(".bsk")) continue;
56-
final String part = file.toString().substring(file.toString().indexOf("/tests/") + 7);
57-
final String name = part.substring(0, part.length() - 4).replace(File.separatorChar, '.');
58-
System.out.println(ConsoleColour.RESET + "Running test '" + ConsoleColour.GREEN + name + ConsoleColour.RESET + "':");
59-
try (final InputStream stream = Files.newInputStream(file)) {
60-
final PostCompileClass[] classes;
61-
synchronized (this) {
62-
try {
63-
final long now, then;
64-
now = System.currentTimeMillis();
65-
classes = skript.compileComplexScript(stream, "skript." + name);
66-
then = System.currentTimeMillis();
67-
System.out.println(ConsoleColour.GREEN + "\t✓ " + ConsoleColour.RESET + "Parsed in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
68-
} catch (Throwable ex) {
69-
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to parse.");
70-
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to run.");
71-
errors.add(ex);
72-
failure++;
73-
continue;
53+
synchronized (System.out) {
54+
while (iterator.hasNext()) {
55+
final Path file = iterator.next();
56+
if (!file.toString().endsWith(".bsk")) continue;
57+
final String part = file.toString().substring(file.toString().indexOf("/tests/") + 7);
58+
final String name = part.substring(0, part.length() - 4).replace(File.separatorChar, '.');
59+
System.out.println(ConsoleColour.RESET + "Running test '" + ConsoleColour.GREEN + name + ConsoleColour.RESET + "':");
60+
try (final InputStream stream = Files.newInputStream(file)) {
61+
final PostCompileClass[] classes;
62+
synchronized (this) {
63+
try {
64+
final long now, then;
65+
now = System.currentTimeMillis();
66+
classes = skript.compileComplexScript(stream, "skript." + name);
67+
then = System.currentTimeMillis();
68+
System.out.println(ConsoleColour.GREEN + "\t✓ " + ConsoleColour.RESET + "Parsed in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
69+
} catch (Throwable ex) {
70+
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to parse.");
71+
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to run.");
72+
errors.add(ex);
73+
failure++;
74+
continue;
75+
}
76+
try {
77+
final long now, then;
78+
final Script script = skript.loadScripts(classes).iterator().next();
79+
now = System.currentTimeMillis();
80+
final Object object = script.getFunction("test").run(skript).get();
81+
final boolean result = Boolean.TRUE.equals(object);
82+
then = System.currentTimeMillis();
83+
if (result)
84+
System.out.println(ConsoleColour.GREEN + "\t✓ " + ConsoleColour.RESET + "Run in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
85+
else
86+
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Run in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
87+
} catch (Throwable ex) {
88+
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to run.");
89+
errors.add(ex);
90+
failure++;
91+
}
7492
}
75-
try {
76-
final long now, then;
77-
final Script script = skript.loadScripts(classes).iterator().next();
78-
now = System.currentTimeMillis();
79-
final Object object = script.getFunction("test").run(skript).get();
80-
final boolean result = Boolean.TRUE.equals(object);
81-
then = System.currentTimeMillis();
82-
if (result)
83-
System.out.println(ConsoleColour.GREEN + "\t✓ " + ConsoleColour.RESET + "Run in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
84-
else
85-
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Run in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
86-
} catch (Throwable ex) {
87-
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to run.");
88-
errors.add(ex);
89-
failure++;
93+
final File test = new File("target/test-scripts/" + classes[0].name() + ".class");
94+
test.getParentFile().mkdirs();
95+
if (!test.exists()) test.createNewFile();
96+
try (final OutputStream output = new FileOutputStream(test)) {
97+
output.write(classes[0].code());
9098
}
99+
} catch (Throwable ex) {
100+
ex.printStackTrace();
91101
}
92-
final File test = new File("target/test-scripts/" + classes[0].name() + ".class");
93-
test.getParentFile().mkdirs();
94-
if (!test.exists()) test.createNewFile();
95-
try (final OutputStream output = new FileOutputStream(test)) {
96-
output.write(classes[0].code());
97-
}
98-
} catch (Throwable ex) {
99-
ex.printStackTrace();
100102
}
101-
}
102-
for (final Throwable error : errors) {
103-
error.printStackTrace(System.err);
103+
for (final Throwable error : errors)
104+
synchronized (this) {
105+
error.printStackTrace(System.out);
106+
}
104107
}
105108
assert failure < 1 : failure + " tests have failed.";
106109
}

src/test/resources/tests/compiler.bsk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
function test:
3+
trigger:
4+
assert the compiler exists
5+
return true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
function test:
3+
trigger:
4+
assert the current script exists
5+
assert name of script exists
6+
assert name of script is "currentscript"
7+
return true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
function test:
3+
trigger:
4+
assert loaded scripts exists
5+
assert size of (loaded scripts) > 0
6+
return true
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
function test:
3+
trigger:
4+
assert the compiler exists
5+
set {code} to ""
6+
load script {code} as "skript/testing/nothing"
7+
assert skript/testing/nothing exists
8+
return true
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
function test:
3+
trigger:
4+
assert the compiler exists
5+
set {code} to ""
6+
load script {code} as "skript/testing/unload"
7+
assert skript/testing/unload exists
8+
unload script skript/testing/unload
9+
return true

0 commit comments

Comments
 (0)