Skip to content

Commit 7dad407

Browse files
sovdeethEfnilite
andauthored
Make ExprExcept tests more reliable, fixes simplification bug (#8026)
* Make ExprExcept tests more reliable, fixes simplification bug * Update src/test/skript/tests/syntaxes/expressions/ExprExcept.sk Co-authored-by: Efnilite <[email protected]>
1 parent d80b0bf commit 7dad407

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

src/main/java/ch/njol/skript/expressions/ExprExcept.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import ch.njol.skript.doc.Name;
77
import ch.njol.skript.doc.Since;
88
import ch.njol.skript.expressions.base.WrapperExpression;
9-
import ch.njol.skript.lang.Expression;
10-
import ch.njol.skript.lang.ExpressionList;
11-
import ch.njol.skript.lang.ExpressionType;
9+
import ch.njol.skript.lang.*;
1210
import ch.njol.skript.lang.SkriptParser.ParseResult;
13-
import ch.njol.skript.lang.SyntaxStringBuilder;
11+
import ch.njol.skript.lang.simplification.SimplifiedLiteral;
1412
import ch.njol.skript.util.LiteralUtils;
1513
import ch.njol.util.Kleenean;
1614
import org.bukkit.event.Event;
@@ -70,6 +68,14 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
7068
.toArray(o -> (Object[]) Array.newInstance(getReturnType(), o));
7169
}
7270

71+
@Override
72+
public Expression<?> simplify() {
73+
setExpr(getExpr().simplify());
74+
if (getExpr() instanceof Literal<?> && exclude instanceof Literal<?>)
75+
return SimplifiedLiteral.fromExpression(this);
76+
return this;
77+
}
78+
7379
@Override
7480
public String toString(@Nullable Event event, boolean debug) {
7581
return (new SyntaxStringBuilder(event, debug))

src/main/java/ch/njol/skript/expressions/base/WrapperExpression.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
import ch.njol.skript.lang.Literal;
66
import ch.njol.skript.lang.SkriptParser.ParseResult;
77
import ch.njol.skript.lang.SyntaxElement;
8-
import ch.njol.skript.lang.util.ConvertedExpression;
8+
import ch.njol.skript.lang.simplification.SimplifiedLiteral;
99
import ch.njol.skript.lang.util.SimpleExpression;
1010
import ch.njol.util.Kleenean;
1111
import org.bukkit.event.Event;
1212
import org.jetbrains.annotations.Nullable;
13-
import org.skriptlang.skript.lang.converter.ConverterInfo;
14-
import org.skriptlang.skript.lang.converter.Converters;
15-
import ch.njol.skript.lang.simplification.SimplifiedLiteral;
1613

1714
import java.util.Iterator;
1815

@@ -99,6 +96,14 @@ public boolean isDefault() {
9996
return expr.isDefault();
10097
}
10198

99+
/**
100+
* This method must be overridden if the subclass uses more than one expression.
101+
* i.e. if only {@link #setExpr(Expression)} is used, this method does not need to be overridden.
102+
* But if a second expression is stored, this method must be overridden to account for that.
103+
* <br>
104+
* <br>
105+
* {@inheritDoc}
106+
*/
102107
@Override
103108
public Expression<? extends T> simplify() {
104109
setExpr(expr.simplify());

src/test/skript/tests/syntaxes/expressions/ExprExcept.sk

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,42 @@ test "except items":
1313
assert {_except::*} is not set with "Failed to exclude all"
1414

1515
test "except entities":
16-
spawn 5 zombies at test-location
17-
set {_zombie} to last spawned zombie
18-
add {_zombie} to {_exclude::*}
16+
set {_nums::*} to 1, 2, 3, 4, and 5
1917

20-
assert size of all zombies except {_zombie} is 4 with "Failed to exclude zombie"
21-
assert size of all entities except {_exclude::*} is 4 with "Failed to exclude list (zombie)"
18+
assert size of {_nums::*} except 5 is 4 with "Failed to exclude 5"
19+
assert size of {_nums::*} except 1 and 5 is 3 with "Failed to exclude 1 and 5"
20+
assert size of {_nums::*} excluding -1 is 5 with "Excluding -1 should not change the list"
2221

23-
spawn 5 skeletons at test-location
24-
set {_skeleton} to last spawned skeleton
25-
add {_skeleton} to {_exclude::*}
22+
set {_exclude::*} to 3, 7, and 1.1
23+
assert size of {_nums::*} excluding {_exclude::*} is 4 with "Failed to exclude multiple numbers"
2624

27-
assert size of all skeletons excluding {_skeleton} is 4 with "Failed to exclude skeleton"
28-
assert size of all entities excluding {_exclude::*} is 8 with "Failed to exclude list (zombie, skeleton)"
2925

26+
set {_types::*} to a zombie, a skeleton, and a villager
27+
assert size of {_types::*} excluding a zombie is 2 with "Failed to exclude zombie"
28+
assert size of {_types::*} excluding a zombie and a skeleton is 1 with "Failed to exclude zombie and skeleton"
29+
30+
delete all villagers
3031
spawn 5 villagers at test-location
3132
set {_villager} to last spawned villager
3233
add {_villager} to {_exclude::*}
3334

3435
assert size of all villagers not including {_villager} is 4 with "Failed to exclude villager"
35-
assert size of all entities not including {_exclude::*} is 12 with "Failed to exclude list (zombie, skeleton, villager)"
3636

3737
clear all entities
3838

39+
set {exprexcept::count} to 0
40+
41+
assert ("a", "z", and 3) excluding excludes() is "z" and 3 with "Failed to exclude the result of a function call"
42+
43+
assert {exprexcept::count} is 1 with "Function call was not executed exactly once"
44+
45+
3946
test "except 'or' objects":
4047
assert (1 or 2) except 2 is 1 with "Failed to exclude '2' from an 'or' list"
4148

4249
set {_item} to (a diamond or an iron ingot or an emerald) excluding a diamond
4350
assert {_item} is (an iron ingot or an emerald) with "Failed to exclude 'diamond' from an 'or' item list"
51+
52+
local function excludes() returns strings:
53+
add 1 to {exprexcept::count}
54+
return "a", "b", "c", "d", and "e"

0 commit comments

Comments
 (0)