Skip to content

Commit c0ae4a8

Browse files
committed
Add type tests.
1 parent b166378 commit c0ae4a8

File tree

14 files changed

+204
-18
lines changed

14 files changed

+204
-18
lines changed

src/main/java/org/byteskript/skript/lang/syntax/type/property/LocalEntry.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ public LocalEntry() {
2929
public void compile(Context context, Pattern.Match match) {
3030
final String name = match.meta();
3131
final boolean value = Boolean.parseBoolean(name);
32-
if (value) {
33-
context.getField().removeModifiers(0x0001);
34-
context.getField().addModifiers(0x0002);
35-
} else {
36-
context.getField().removeModifiers(0x0002);
37-
context.getField().addModifiers(0x0001);
32+
if (context.getField() != null) {
33+
if (value) {
34+
context.getField().removeModifiers(0x0001);
35+
} else {
36+
context.getField().addModifiers(0x0001);
37+
}
38+
} else if (context.getMethod() != null) {
39+
if (value) {
40+
context.getMethod().removeModifiers(0x0001);
41+
} else {
42+
context.getMethod().addModifiers(0x0001);
43+
}
3844
}
3945
context.setState(CompileState.MEMBER_BODY);
4046
}
@@ -51,9 +57,7 @@ public Pattern.Match match(String thing, Context context) {
5157

5258
@Override
5359
public boolean allowedIn(State state, Context context) {
54-
return context.getState() == CompileState.MEMBER_BODY
55-
&& context.hasFlag(AreaFlag.IN_TYPE)
56-
&& context.hasFlag(AreaFlag.IN_PROPERTY);
60+
return context.getState() == CompileState.MEMBER_BODY;
5761
}
5862

5963

src/main/java/org/byteskript/skript/lang/syntax/type/property/PropertyMember.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
examples = """
2727
type Square:
2828
property sides:
29-
type Integer
29+
type: Integer
3030
"""
3131
)
3232
public class PropertyMember extends Member {

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,29 @@ public static CallSite createBridge(MethodHandles.Lookup caller, String name, Me
4444
}
4545

4646
private static Method findTarget(MethodHandles.Lookup caller, String name, Class<?> owner, Class<?>... parameters) {
47-
final Method[] methods = owner.getMethods();
48-
for (final Method method : methods) {
49-
if (!method.getName().equals(name)) continue;
50-
if (Arrays.equals(method.getParameterTypes(), parameters)) return method;
47+
{
48+
final Method[] methods = owner.getMethods();
49+
for (final Method method : methods) {
50+
if (!method.getName().equals(name)) continue;
51+
if (Arrays.equals(method.getParameterTypes(), parameters)) return method;
52+
}
53+
for (final Method method : methods) {
54+
if (!Modifier.isStatic(method.getModifiers())) continue; // can only hit statics for now
55+
if (!method.getName().equals(name)) continue;
56+
if (parameters.length == method.getParameterCount()) return method;
57+
}
5158
}
52-
for (final Method method : methods) {
53-
if (!Modifier.isStatic(method.getModifiers())) continue; // can only hit statics for now
54-
if (!method.getName().equals(name)) continue;
55-
if (parameters.length == method.getParameterCount()) return method;
59+
{
60+
final Method[] methods = owner.getDeclaredMethods();
61+
for (final Method method : methods) {
62+
if (!method.getName().equals(name)) continue;
63+
if (Arrays.equals(method.getParameterTypes(), parameters)) return method;
64+
}
65+
for (final Method method : methods) {
66+
if (!Modifier.isStatic(method.getModifiers())) continue; // can only hit statics for now
67+
if (!method.getName().equals(name)) continue;
68+
if (parameters.length == method.getParameterCount()) return method;
69+
}
5670
}
5771
throw new ScriptRuntimeError("Unable to find function '" + name + Arrays.toString(parameters).replace('[', '(')
5872
.replace(']', ')') + "' from " + owner.getSimpleName());

src/test/resources/tests/every.bsk

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
every 5 minutes: // parse test only
3+
trigger:
4+
assert true
5+
6+
function test:
7+
trigger:
8+
return true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
type Box:
3+
property test:
4+
final: true
5+
6+
function test:
7+
trigger:
8+
set {var} to a new box
9+
assert {var} exists
10+
assert {var} is a box
11+
try:
12+
set test of {var} to "hello"
13+
catch {error}:
14+
assert {error} exists
15+
return true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
function blob:
3+
local: true
4+
trigger:
5+
return 4
6+
7+
function test:
8+
trigger:
9+
assert blob() is 4
10+
return true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
type Square:
3+
property sides:
4+
type: Integer
5+
6+
function test:
7+
trigger:
8+
set {var} to a new square
9+
set sides of {var} to 1
10+
assert sides of {var} is 1
11+
try:
12+
set sides of {var} to "hello"
13+
catch {error}:
14+
assert {error} exists
15+
return true

src/test/resources/tests/sleep.bsk

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
function test:
3+
trigger:
4+
set {thread} to the current process
5+
set {@thing} to false
6+
set {var} to a new runnable:
7+
wait 1 milliseconds
8+
set {@thing} to true
9+
wake {thread}
10+
assert {@thing} is false
11+
run {var} in the background
12+
delete {@thing}
13+
assert {@thing} is null
14+
sleep
15+
assert {@thing} is true
16+
delete {@thing}
17+
return true
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
template type Foo:
3+
function box:
4+
trigger:
5+
return 3
6+
7+
type Bar:
8+
template: Foo
9+
10+
type Cee:
11+
template: Foo
12+
function box:
13+
trigger:
14+
return 2
15+
16+
function test:
17+
trigger:
18+
set {bar} to a new bar
19+
assert {bar} is a bar
20+
assert {bar} is a foo
21+
assert box() from {bar} is 3
22+
set {cee} to a new cee
23+
assert {cee} is a cee
24+
assert {cee} is a foo
25+
assert box() from {cee} is 2
26+
return true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
function test:
3+
trigger:
4+
set {thread} to the current process
5+
set {@thing} to false
6+
set {var} to a new runnable:
7+
wait 1 milliseconds
8+
set {@thing} to true
9+
wake {thread}
10+
assert {@thing} is false
11+
run {var} in the background
12+
delete {@thing}
13+
assert {@thing} is null
14+
sleep
15+
assert {@thing} is true
16+
delete {@thing}
17+
return true

0 commit comments

Comments
 (0)