Skip to content

Commit fdcaeb4

Browse files
committed
Add tests for literals and maps.
1 parent 3ecfb54 commit fdcaeb4

File tree

7 files changed

+70
-18
lines changed

7 files changed

+70
-18
lines changed

src/main/java/org/byteskript/skript/compiler/CommonTypes.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class CommonTypes {
3030
public static final Type BOOLEAN = new Type(Boolean.class);
3131
public static final Type INTEGER = new Type(Integer.class);
3232
public static final Type DOUBLE = new Type(Double.class);
33+
public static final Type FLOAT = new Type(Float.class);
34+
public static final Type LONG = new Type(Long.class);
3335
public static final Type NUMBER = new Type(Number.class);
3436
public static final Type CLASS = new Type(Class.class);
3537
public static final Type TYPE = new Type(java.lang.reflect.Type.class);

src/main/java/org/byteskript/skript/compiler/SkriptLangSpec.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
package org.byteskript.skript.compiler;
88

99
import mx.kenzie.foundation.Type;
10-
import mx.kenzie.foundation.compiler.State;
1110
import mx.kenzie.foundation.language.Compiler;
1211
import mx.kenzie.foundation.language.LanguageDefinition;
1312
import mx.kenzie.foundation.language.PostCompileClass;
1413
import mx.kenzie.foundation.opcodes.JavaVersion;
15-
import org.byteskript.skript.api.*;
14+
import org.byteskript.skript.api.Event;
15+
import org.byteskript.skript.api.LanguageElement;
16+
import org.byteskript.skript.api.Library;
17+
import org.byteskript.skript.api.ModifiableLibrary;
1618
import org.byteskript.skript.app.ScriptRunner;
1719
import org.byteskript.skript.app.SimpleThrottleController;
1820
import org.byteskript.skript.app.SkriptApp;
@@ -77,7 +79,10 @@
7779
import java.io.InputStream;
7880
import java.net.URL;
7981
import java.security.CodeSource;
80-
import java.util.*;
82+
import java.util.ArrayList;
83+
import java.util.Collection;
84+
import java.util.List;
85+
import java.util.Map;
8186
import java.util.regex.Pattern;
8287
import java.util.zip.ZipEntry;
8388
import java.util.zip.ZipInputStream;
@@ -93,15 +98,15 @@ public final class SkriptLangSpec extends ModifiableLibrary implements LanguageD
9398

9499
final LanguageElement[] grammar = StandardElements.values();
95100

96-
final Map<State, List<SyntaxElement>> syntax = new HashMap<>();
97-
98101
private SkriptLangSpec() {
99102
super("Skript");
100103
this.registerTypes(
101104
CommonTypes.CLASS,
102105
CommonTypes.TYPE,
103106
CommonTypes.INTEGER,
104107
CommonTypes.DOUBLE,
108+
CommonTypes.FLOAT,
109+
CommonTypes.LONG,
105110
CommonTypes.NUMBER,
106111
CommonTypes.BOOLEAN,
107112
CommonTypes.STRING,

src/main/java/org/byteskript/skript/lang/syntax/type/TypeExpression.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.byteskript.skript.compiler.Pattern;
1717
import org.byteskript.skript.compiler.SkriptLangSpec;
1818
import org.byteskript.skript.lang.element.StandardElements;
19+
import org.byteskript.skript.runtime.Skript;
1920

2021
import java.util.Map;
2122
import java.util.regex.Matcher;
@@ -52,11 +53,7 @@ public void compile(Context context, Pattern.Match match) throws Throwable {
5253

5354
@Override
5455
public Class<?> parse(String input) {
55-
try {
56-
return Class.forName(input.replace('/', '.'));
57-
} catch (ClassNotFoundException e) {
58-
return null;
59-
}
56+
return Skript.findAnyClass(input.replace('/', '.'));
6057
}
6158

6259
@Override

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public static Object getListSize(Object target) {
121121
}
122122

123123
public static Object getListValue(Object key, Object target) {
124+
if (target instanceof Map) return getMapValue(key, target);
124125
final Number number = Skript.convert(key, Number.class);
125126
if (target instanceof List list) return list.get(number.intValue());
126127
if (target instanceof Object[] list) return list[number.intValue()];
@@ -129,6 +130,7 @@ public static Object getListValue(Object key, Object target) {
129130

130131
@SuppressWarnings("unchecked")
131132
public static void setListValue(Object key, Object target, Object value) {
133+
if (target instanceof Map) setMapValue(key, target, value);
132134
final Number number = Skript.convert(key, Number.class);
133135
if (target instanceof List list) {
134136
list.remove(number.intValue());
@@ -142,6 +144,7 @@ public static void setListValue(Object key, Object target, Object value) {
142144
}
143145

144146
public static void deleteListValue(Object key, Object target) {
147+
if (target instanceof Map) deleteMapValue(key, target);
145148
final Number number = Skript.convert(key, Number.class);
146149
if (target instanceof List list) {
147150
list.remove(number.intValue());

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import mx.kenzie.foundation.language.PostCompileClass;
1010
import org.byteskript.skript.runtime.Script;
1111
import org.byteskript.skript.runtime.Skript;
12+
import org.byteskript.skript.runtime.internal.ConsoleColour;
1213
import org.byteskript.skript.runtime.internal.Member;
1314
import org.junit.BeforeClass;
1415
import org.junit.Test;
@@ -19,8 +20,10 @@
1920
import java.io.OutputStream;
2021
import java.net.URI;
2122
import java.nio.file.*;
23+
import java.util.ArrayList;
2224
import java.util.Collections;
2325
import java.util.Iterator;
26+
import java.util.List;
2427

2528
public class GenericTest extends SkriptTest {
2629

@@ -46,11 +49,13 @@ public void all() throws Throwable {
4649
}
4750
final Iterator<Path> iterator = Files.walk(path, 1).iterator();
4851
int failure = 0;
52+
final List<Throwable> errors = new ArrayList<>();
4953
while (iterator.hasNext()) {
5054
final Path file = iterator.next();
5155
if (!file.toString().endsWith(".bsk")) continue;
5256
final String part = file.toString().substring(file.toString().indexOf("/tests/") + 7);
5357
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 + "':");
5459
try (final InputStream stream = Files.newInputStream(file)) {
5560
final PostCompileClass[] classes;
5661
synchronized (this) {
@@ -59,24 +64,28 @@ public void all() throws Throwable {
5964
now = System.currentTimeMillis();
6065
classes = skript.compileComplexScript(stream, "skript." + name);
6166
then = System.currentTimeMillis();
62-
System.out.println("Parsed " + name + " in " + (then - now) + " milliseconds.");
67+
System.out.println(ConsoleColour.GREEN + "\t " + ConsoleColour.RESET + "Parsed in " + ConsoleColour.BLUE + (then - now) + ConsoleColour.RESET + " milliseconds.");
6368
} catch (Throwable ex) {
64-
System.err.println("Error in '" + name + "':");
65-
ex.printStackTrace(System.err);
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);
6672
failure++;
6773
continue;
6874
}
6975
try {
7076
final long now, then;
7177
final Script script = skript.loadScripts(classes).iterator().next();
7278
now = System.currentTimeMillis();
73-
final boolean result = (boolean) script.getFunction("test").run(skript).get();
79+
final Object object = script.getFunction("test").run(skript).get();
80+
final boolean result = Boolean.TRUE.equals(object);
7481
then = System.currentTimeMillis();
75-
assert result : "Test failed.";
76-
System.out.println("Run " + name + " in " + (then - now) + " milliseconds.");
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.");
7786
} catch (Throwable ex) {
78-
System.err.println("Error in '" + name + "':");
79-
ex.printStackTrace(System.err);
87+
System.out.println(ConsoleColour.RED + "\t✗ " + ConsoleColour.RESET + "Failed to run.");
88+
errors.add(ex);
8089
failure++;
8190
}
8291
}
@@ -90,6 +99,9 @@ public void all() throws Throwable {
9099
ex.printStackTrace();
91100
}
92101
}
102+
for (final Throwable error : errors) {
103+
error.printStackTrace(System.err);
104+
}
93105
assert failure < 1 : failure + " tests have failed.";
94106
}
95107

src/test/resources/tests/keyinmap.bsk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
function test:
3+
trigger:
4+
set {var} to a new map
5+
set "blob" in {var} to 1
6+
assert "blob" in {var} is 1
7+
set 3 in {var} to 2
8+
assert 3 in {var} is 2
9+
assert size of {var} is 2
10+
return true

src/test/resources/tests/literals.bsk

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
function test:
3+
trigger:
4+
assert true is true
5+
assert false is false
6+
assert true is not false
7+
assert false is not true
8+
assert true is a boolean
9+
assert 1 is an integer
10+
assert 1 is a number
11+
assert (1.0 is an integer) is false
12+
assert 1.0 is a number
13+
assert -1 is an integer
14+
assert -1.0 is a number
15+
assert 1.0F is a float
16+
assert 1.0D is a double
17+
assert 10L is a long
18+
assert 10L is a number
19+
assert (null exists) is false
20+
assert "hello" exists
21+
assert /hello/ exists
22+
assert /.+/ exists
23+
return true

0 commit comments

Comments
 (0)