From e68536389a8aff38d49b140c1444656d5bb481cd Mon Sep 17 00:00:00 2001 From: Phill310 Date: Sun, 14 Apr 2024 23:40:51 -0700 Subject: [PATCH 01/10] Create the clamp expression --- .../ch/njol/skript/expressions/ExprClamp.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/main/java/ch/njol/skript/expressions/ExprClamp.java diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java new file mode 100644 index 00000000000..54977864fa0 --- /dev/null +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -0,0 +1,100 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.expressions; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.ExpressionType; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +/** + * @author Phill310 + */ +@Name("Clamp") +@Description("Clamps one or more values between two numbers.") +@Examples({ + "clamp 5 between 0 and 10 = 5", + "clamp 5.5 between 0 and 5 = 5", + "clamp 0.25 between 0 and 0.5 = 0.25", + "clamp 5 between 7 and 10 = 7", + "clamp (5, 0, 10, 9, 13) between 7 and 10 = (7, 7, 10, 9, 10)", + "", + "set {_clamped::*} to clamp {_values::*} between 0 and 10" +}) +@Since("version") +public class ExprClamp extends SimpleExpression { + + static { + Skript.registerExpression(ExprClamp.class, Number.class, ExpressionType.COMBINED, + "clamp %numbers% between %number% and %number%"); + } + + private Expression values, min, max; + + @Override + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + values = (Expression) expressions[0]; + min = ((Expression) expressions[1]); + max = (Expression) expressions[2]; + return true; + } + + @Nullable + @Override + protected Number[] get(Event event) { + Number[] numbers = values.getArray(event); + Double[] clampedValues = new Double[numbers.length]; + double min = this.min.getSingle(event).doubleValue(); + double max = this.max.getSingle(event).doubleValue(); + // Make sure the min and max are in the correct order + double trueMin = Math.min(min, max); + double trueMax = Math.max(min, max); + for (int i = 0; i < numbers.length; i++) { + double value = numbers[i].doubleValue(); + clampedValues[i] = Math.max(Math.min(value, trueMax), trueMin); + } + return clampedValues; + } + + @Override + public boolean isSingle() { + return false; + } + + @Override + public Class getReturnType() { + return Number.class; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "clamp " + values.toString(event, debug) + " between " + min.toString(event, debug) + " and " + + max.toString(event, debug); + } + + +} From c325893ecca45a87160ca236584af4f9d257bb5a Mon Sep 17 00:00:00 2001 From: Phill310 Date: Sun, 14 Apr 2024 23:53:21 -0700 Subject: [PATCH 02/10] Apply suggestions from code review Co-authored-by: Fusezion --- src/main/java/ch/njol/skript/expressions/ExprClamp.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java index 54977864fa0..6e9fa941c5b 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprClamp.java +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -45,7 +45,7 @@ "", "set {_clamped::*} to clamp {_values::*} between 0 and 10" }) -@Since("version") +@Since("INSERT VERSION") public class ExprClamp extends SimpleExpression { static { @@ -82,7 +82,7 @@ protected Number[] get(Event event) { @Override public boolean isSingle() { - return false; + return values.isSingle(); } @Override From 24c52bfa81a57ed4e81c2c448136b3c8f2d9d26f Mon Sep 17 00:00:00 2001 From: Phill310 Date: Mon, 15 Apr 2024 00:03:44 -0700 Subject: [PATCH 03/10] Remove author and clean up examples --- .../java/ch/njol/skript/expressions/ExprClamp.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java index 6e9fa941c5b..7ac3a43691f 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprClamp.java +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -31,17 +31,14 @@ import org.bukkit.event.Event; import org.eclipse.jdt.annotation.Nullable; -/** - * @author Phill310 - */ @Name("Clamp") @Description("Clamps one or more values between two numbers.") @Examples({ - "clamp 5 between 0 and 10 = 5", - "clamp 5.5 between 0 and 5 = 5", - "clamp 0.25 between 0 and 0.5 = 0.25", - "clamp 5 between 7 and 10 = 7", - "clamp (5, 0, 10, 9, 13) between 7 and 10 = (7, 7, 10, 9, 10)", + "clamp 5 between 0 and 10 # result = 5", + "clamp 5.5 between 0 and 5 # result = 5", + "clamp 0.25 between 0 and 0.5 # result = 0.25", + "clamp 5 between 7 and 10 # result = 7", + "clamp (5, 0, 10, 9, 13) between 7 and 10 # result = (7, 7, 10, 9, 10)", "", "set {_clamped::*} to clamp {_values::*} between 0 and 10" }) From 15d41e2ca16c1bdcee7b86d375acbc0ff151d722 Mon Sep 17 00:00:00 2001 From: Phill310 Date: Tue, 16 Apr 2024 11:43:49 -0700 Subject: [PATCH 04/10] Change syntax to resemble an expression Fix examples Check if getSingle is null --- .../ch/njol/skript/expressions/ExprClamp.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java index 7ac3a43691f..2b96fd0a7aa 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprClamp.java +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -25,7 +25,7 @@ import ch.njol.skript.doc.Since; import ch.njol.skript.lang.Expression; import ch.njol.skript.lang.ExpressionType; -import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.lang.SkriptParser.ParseResult; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.util.Kleenean; import org.bukkit.event.Event; @@ -34,28 +34,28 @@ @Name("Clamp") @Description("Clamps one or more values between two numbers.") @Examples({ - "clamp 5 between 0 and 10 # result = 5", - "clamp 5.5 between 0 and 5 # result = 5", - "clamp 0.25 between 0 and 0.5 # result = 0.25", - "clamp 5 between 7 and 10 # result = 7", - "clamp (5, 0, 10, 9, 13) between 7 and 10 # result = (7, 7, 10, 9, 10)", + "5 clamped between 0 and 10 # result = 5", + "5.5 clamped between 0 and 5 # result = 5", + "0.25 clamped between 0 and 0.5 # result = 0.25", + "5 clamped between 7 and 10 # result = 7", + "(5, 0, 10, 9, 13) clamped between 7 and 10 # result = (7, 7, 10, 9, 10)", "", - "set {_clamped::*} to clamp {_values::*} between 0 and 10" + "set {_clamped::*} to {_values::*} clamped between 0 and 10" }) @Since("INSERT VERSION") public class ExprClamp extends SimpleExpression { static { Skript.registerExpression(ExprClamp.class, Number.class, ExpressionType.COMBINED, - "clamp %numbers% between %number% and %number%"); + "%numbers% clamped between %number% and %number%"); } private Expression values, min, max; @Override - public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { values = (Expression) expressions[0]; - min = ((Expression) expressions[1]); + min = (Expression) expressions[1]; max = (Expression) expressions[2]; return true; } @@ -65,8 +65,8 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is protected Number[] get(Event event) { Number[] numbers = values.getArray(event); Double[] clampedValues = new Double[numbers.length]; - double min = this.min.getSingle(event).doubleValue(); - double max = this.max.getSingle(event).doubleValue(); + double min = this.min.getSingle(event) != null ? this.min.getSingle(event).doubleValue() : 0; + double max = this.max.getSingle(event) != null ? this.max.getSingle(event).doubleValue() : 0; // Make sure the min and max are in the correct order double trueMin = Math.min(min, max); double trueMax = Math.max(min, max); @@ -93,5 +93,4 @@ public String toString(@Nullable Event event, boolean debug) { + max.toString(event, debug); } - } From 5ce67088883eac811ddaf0fb883e11f34116e36c Mon Sep 17 00:00:00 2001 From: Phill310 Date: Tue, 16 Apr 2024 12:05:50 -0700 Subject: [PATCH 05/10] Use #getOptionalSingle instead of #getSingle --- src/main/java/ch/njol/skript/expressions/ExprClamp.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java index 2b96fd0a7aa..141cef5f315 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprClamp.java +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -65,8 +65,8 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is protected Number[] get(Event event) { Number[] numbers = values.getArray(event); Double[] clampedValues = new Double[numbers.length]; - double min = this.min.getSingle(event) != null ? this.min.getSingle(event).doubleValue() : 0; - double max = this.max.getSingle(event) != null ? this.max.getSingle(event).doubleValue() : 0; + double min = this.min.getOptionalSingle(event).orElse(0).doubleValue(); + double max = this.max.getOptionalSingle(event).orElse(0).doubleValue(); // Make sure the min and max are in the correct order double trueMin = Math.min(min, max); double trueMax = Math.max(min, max); From 30b66ea488259a1241cf05be34b48ad2a3c3380b Mon Sep 17 00:00:00 2001 From: Phill310 Date: Wed, 17 Apr 2024 00:51:41 -0700 Subject: [PATCH 06/10] Add upper and lower bounds --- .../ch/njol/skript/expressions/ExprClamp.java | 66 ++++++++++++++++--- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java index 141cef5f315..1006a0a17dc 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprClamp.java +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -37,26 +37,64 @@ "5 clamped between 0 and 10 # result = 5", "5.5 clamped between 0 and 5 # result = 5", "0.25 clamped between 0 and 0.5 # result = 0.25", - "5 clamped between 7 and 10 # result = 7", "(5, 0, 10, 9, 13) clamped between 7 and 10 # result = (7, 7, 10, 9, 10)", + "set {_clamped::*} to {_values::*} clamped between 0 and 10", "", - "set {_clamped::*} to {_values::*} clamped between 0 and 10" + "3 clamped below 5 # result = 3", + "3.2 clamped below 2 # result = 2", + "(-1, 3, 0.5, 9) clamped below 3 # result = (-1, 3, 0.5, 3)", + "", + "6.5 clamped above 6 # result = 6.5", + "4 clamped above 5.5 # result = 5.5", + "(3.14, 1, -9.8, 2.7) clamped above 2.5 # result = (3.14, 2.5, 2.5, 2.7)", + "set {_not negative} to {_input} clamped above 0" }) @Since("INSERT VERSION") public class ExprClamp extends SimpleExpression { static { Skript.registerExpression(ExprClamp.class, Number.class, ExpressionType.COMBINED, - "%numbers% clamped between %number% and %number%"); + "%numbers% clamped between %number% and %number%", + "%numbers% clamped below %number%", + "%numbers% clamped above %number%"); + } + + private enum Mode { + BOTH { + @Override + public String toString() { + return "between"; + } + }, + BELOW { + @Override + public String toString() { + return "below"; + } + }, + ABOVE { + @Override + public String toString() { + return "above"; + } + } } - private Expression values, min, max; + private Expression values, minExpr, maxExpr; + private Mode mode; @Override public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + mode = Mode.values()[matchedPattern]; values = (Expression) expressions[0]; - min = (Expression) expressions[1]; - max = (Expression) expressions[2]; + if (mode == Mode.BOTH) { + minExpr = (Expression) expressions[1]; + maxExpr = (Expression) expressions[2]; + } else if (mode == Mode.BELOW) { + maxExpr = (Expression) expressions[1]; + } else if (mode == Mode.ABOVE) { + minExpr = (Expression) expressions[1]; + } return true; } @@ -65,8 +103,14 @@ public boolean init(Expression[] expressions, int matchedPattern, Kleenean is protected Number[] get(Event event) { Number[] numbers = values.getArray(event); Double[] clampedValues = new Double[numbers.length]; - double min = this.min.getOptionalSingle(event).orElse(0).doubleValue(); - double max = this.max.getOptionalSingle(event).orElse(0).doubleValue(); + double min = Double.NEGATIVE_INFINITY; + double max = Double.POSITIVE_INFINITY; + if (mode == Mode.ABOVE || mode == Mode.BOTH) { + min = minExpr.getOptionalSingle(event).orElse(Double.NEGATIVE_INFINITY).doubleValue(); + } + if (mode == Mode.BELOW || mode == Mode.BOTH) { + max = maxExpr.getOptionalSingle(event).orElse(Double.POSITIVE_INFINITY).doubleValue(); + } // Make sure the min and max are in the correct order double trueMin = Math.min(min, max); double trueMax = Math.max(min, max); @@ -89,8 +133,10 @@ public Class getReturnType() { @Override public String toString(@Nullable Event event, boolean debug) { - return "clamp " + values.toString(event, debug) + " between " + min.toString(event, debug) + " and " - + max.toString(event, debug); + return values.toString(event, debug) + " clamped " + mode + " " + + ((mode == Mode.ABOVE || mode == Mode.BOTH) ? minExpr.toString(event, debug) : "") + + ((mode == Mode.BOTH) ? " and " : "") + + ((mode == Mode.BELOW || mode == Mode.BOTH) ? maxExpr.toString(event, debug) : ""); } } From 2aa6ffac85698ec47837d5a863c1a489b916dda6 Mon Sep 17 00:00:00 2001 From: Phill310 Date: Sat, 27 Apr 2024 08:04:34 -0700 Subject: [PATCH 07/10] Use switch statement --- .../ch/njol/skript/expressions/ExprClamp.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/ch/njol/skript/expressions/ExprClamp.java b/src/main/java/ch/njol/skript/expressions/ExprClamp.java index 1006a0a17dc..41be6e369b6 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprClamp.java +++ b/src/main/java/ch/njol/skript/expressions/ExprClamp.java @@ -87,13 +87,14 @@ public String toString() { public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { mode = Mode.values()[matchedPattern]; values = (Expression) expressions[0]; - if (mode == Mode.BOTH) { - minExpr = (Expression) expressions[1]; - maxExpr = (Expression) expressions[2]; - } else if (mode == Mode.BELOW) { - maxExpr = (Expression) expressions[1]; - } else if (mode == Mode.ABOVE) { - minExpr = (Expression) expressions[1]; + switch (mode) { + case BOTH: + maxExpr = (Expression) expressions[2]; + case ABOVE: + minExpr = (Expression) expressions[1]; + break; + case BELOW: + maxExpr = (Expression) expressions[1]; } return true; } From 4aca0af293dd74e16eed682ff6557bb65e70dc73 Mon Sep 17 00:00:00 2001 From: Phill310 Date: Sat, 27 Apr 2024 09:59:10 -0700 Subject: [PATCH 08/10] Create test for clamp expression --- .../tests/syntaxes/expressions/ExprClamp.sk | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/test/skript/tests/syntaxes/expressions/ExprClamp.sk diff --git a/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk b/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk new file mode 100644 index 00000000000..5d157f2b86c --- /dev/null +++ b/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk @@ -0,0 +1,119 @@ +test "clamp expression": + # Upper and lower limits + + # Normal Cases + assert 1 clamped between 0 and 2 is 1 with error "(single ints) min < value < max", expected 1, got 1 clamped between 0 and 2 + assert 1 clamped between 1 and 2 is 1 with error "(single ints) min = value < max", expected 1, got 1 clamped between 1 and 2 + assert 2 clamped between 1 and 2 is 2 with error "(single ints) min < value = max", expected 2, got 2 clamped between 1 and 2 + assert 0 clamped between 1 and 2 is 1 with error "(single ints) value < min < max", expected 1, got 0 clamped between 1 and 2 + assert 3 clamped between 1 and 2 is 2 with error "(single ints) min < max < value", expected 2, got 3 clamped between 1 and 2 + assert 3 clamped between 2 and 1 is 2 with error "(single ints) max < min < value", expected 2, got 3 clamped between 2 and 1 + + assert 1.999 clamped between 0.0 and 2.0 is 1.999 with error "(single floats) min < value < max", expected 1.999, got 1.999 clamped between 0.0 and 2.0 + assert 1.999 clamped between 1.999 and 2.0 is 1.999 with error "(single floats) min = value < max", expected 1.999, got 1.999 clamped between 1.999 and 2.0 + assert 2.0 clamped between 1.999 and 2.0 is 2.0 with error "(single floats) min < value = max", expected 2.0, got 2.0 clamped between 1.999 and 2.0 + assert 0.0 clamped between 1.999 and 2.0 is 1.999 with error "(single floats) value < min < max", expected 1.999, got 0.0 clamped between 1.999 and 2.0 + assert 3.0 clamped between 1.999 and 2.0 is 2.0 with error "(single floats) min < max < value", expected 2.0, got 3.0 clamped between 1.999 and 2.0 + assert 2.999 clamped between 2.0 and 1.999 is 2.0 with error "(single floats) max < min < value", expected 2.0, got 2.999 clamped between 2.0 and 1.999 + + # Lists + set {_expected::*} to (0, 0, 1, 2, 2, and 2) + # this is dumb but comparing the lists directly didn't work + set {_got::*} to (-1, 0, 1, 2, 3, and 4) clamped between 0 and 2 + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with error "(multiple ints)", expected loop-value, got {_got::%loop-index%} + set {_got::*} to (-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0) clamped between 0.0 and 2.0 + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with error "(multiple floats)", expected loop-value, got {_got::%loop-index%} + + # Edge Cases + assert 1 clamped between {_null} and 2 is 1 with error "(single ints) min = null" + assert 2 clamped between 1 and {_null} is 2 with error "(single ints) max = null" + assert {_null} clamped between 1 and 2 is not set with error "(single ints) value = null" + assert isNaN(1 clamped between 0 and NaN value) is true with error "(single ints) min < value < NaN" + assert isNaN(1 clamped between NaN value and 2) is true with error "(single ints) NaN < value < max" + assert isNaN(NaN value clamped between 1 and 2) is true with error "(single ints) min < NaN < max" + assert infinity value clamped between 1 and 2 is 2 with error "(single ints) min < infinity < max", expected 2, got infinity value clamped between 1 and 2 + assert -infinity value clamped between 1 and 2 is 1 with error "(single ints) min < -infinity < max", expected 1, got -infinity value clamped between 1 and 2 + assert 1 clamped between 0 and infinity value is 1 with error "(single ints) min < value < infinity", expected 1, got 1 clamped between 0 and infinity value + assert 1 clamped between -infinity value and 2 is 1 with error "(single ints) -infinity < value < max", expected 1, got 1 clamped between -infinity value and 2 + + set {_expected::*} to (NaN value, 0.0, and 2.0) + set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped between 0.0 and 2.0 + assert isNaN({_got::1}) is true with error "(edge cases list) NaN" + assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity", expected {_expected::2}, got {_got::2} + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity", expected {_expected::3}, got {_got::3} + + + # Upper limits only (below) + + # Normal Cases + assert 1 clamped below 2 is 1 with error "(single ints) value < max", expected 1, got 1 clamped below 2 + assert 2 clamped below 2 is 2 with error "(single ints) value = max", expected 2, got 2 clamped below 2 + assert 3 clamped below 2 is 2 with error "(single ints) max < value", expected 2, got 3 clamped below 2 + + assert 1.999 clamped below 2.0 is 1.999 with error "(single floats) value < max", expected 1.999, got 1.999 clamped below 2.0 + assert 2.0 clamped below 2.0 is 2.0 with error "(single floats) value = max", expected 2.0, got 2.0 clamped below 2.0 + assert 3.0 clamped below 2.0 is 2.0 with error "(single floats) min < max < value", expected 2.0, got 3.0 clamped below 2.0 + + # Lists + set {_expected::*} to (-1, 0, 1, 2, 2, and 2) + # this is dumb but comparing the lists directly didn't work + set {_got::*} to (-1, 0, 1, 2, 3, and 4) clamped below 2 + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with error "(multiple ints)", expected loop-value, got {_got::%loop-index%} + set {_expected::*} to (-1.999, 0, 1, 2, 2, and 2) + set {_got::*} to (-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0) clamped below 2.0 + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with error "(multiple floats)", expected loop-value, got {_got::%loop-index%} + + # Edge Cases + assert 2 clamped below {_null} is 2 with error "(single ints) max = null" + assert {_null} clamped below 2 is not set with error "(single ints) value = null" + assert isNaN(1 clamped below NaN value) is true with error "(single ints) value < NaN" + assert isNaN(NaN value clamped below 2) is true with error "(single ints) NaN < max" + assert infinity value clamped below 2 is 2 with error "(single ints) infinity < max", expected 2, got infinity value clamped below 2 + assert -infinity value clamped below 2 is -infinity value with error "(single ints) -infinity < max", expected -infinity value, got -infinity value clamped below 2 + assert 1 clamped below infinity value is 1 with error "(single ints) value < infinity", expected 1, got 1 clamped below infinity value + + set {_expected::*} to (NaN value, -infinity value, and 2.0) + set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped below 2.0 + assert isNaN({_got::1}) is true with error "(edge cases list) NaN" + assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity", expected {_expected::2}, got {_got::2} + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity", expected {_expected::3}, got {_got::3} + + # Lower limits only (above) + + # Normal Cases + assert 1 clamped above 0 is 1 with error "(single ints) min < value", expected 1, got 1 clamped above 0 + assert 1 clamped above 1 is 1 with error "(single ints) min = value", expected 1, got 1 clamped above 1 + assert 0 clamped above 1 is 1 with error "(single ints) value < min", expected 1, got 0 clamped above 1 + + assert 1.999 clamped above 0.0 is 1.999 with error "(single floats) min < value", expected 1.999, got 1.999 clamped above 0.0 + assert 1.999 clamped above 1.999 is 1.999 with error "(single floats) min = value", expected 1.999, got 1.999 clamped above 1.999 + assert 0.0 clamped above 1.999 is 1.999 with error "(single floats) value < min", expected 1.999, got 0.0 clamped above 1.999 + + # Lists + set {_expected::*} to (0, 0, 1, 2, 3, and 4) + # this is dumb but comparing the lists directly didn't work + set {_got::*} to (-1, 0, 1, 2, 3, and 4) clamped above 0 + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with error "(multiple ints)", expected loop-value, got {_got::%loop-index%} + set {_got::*} to (-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0) clamped above 0.0 + loop {_expected::*}: + assert {_got::%loop-index%} is loop-value with error "(multiple floats)", expected loop-value, got {_got::%loop-index%} + + # Edge Cases + assert 1 clamped above {_null} is 1 with error "(single ints) min = null" + assert {_null} clamped above 1 is not set with error "(single ints) value = null" + assert isNaN(1 clamped above NaN value) is true with error "(single ints) NaN < value" + assert isNaN(NaN value clamped above 1) is true with error "(single ints) min < NaN" + assert infinity value clamped above 1 is infinity value with error "(single ints) min < infinity", expected infinity value, got infinity value clamped above 1 + assert -infinity value clamped above 1 is 1 with error "(single ints) min < -infinity", expected 1, got -infinity value clamped above 1 + assert 1 clamped above -infinity value is 1 with error "(single ints) -infinity < value", expected 1, got 1 clamped above -infinity value + + set {_expected::*} to (NaN value, 0.0, and infinity value) + set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped above 0.0 + assert isNaN({_got::1}) is true with error "(edge cases list) NaN" + assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity", expected {_expected::2}, got {_got::2} + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity", expected {_expected::3}, got {_got::3} \ No newline at end of file From 2ccbd88acb2c5acb8afd026e7292303c7afae87b Mon Sep 17 00:00:00 2001 From: Phill310 Date: Wed, 1 May 2024 20:36:14 -0700 Subject: [PATCH 09/10] Remove redundant "expected x, got y" --- .../tests/syntaxes/expressions/ExprClamp.sk | 94 +++++++++---------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk b/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk index 5d157f2b86c..746f32386f5 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk @@ -2,29 +2,29 @@ test "clamp expression": # Upper and lower limits # Normal Cases - assert 1 clamped between 0 and 2 is 1 with error "(single ints) min < value < max", expected 1, got 1 clamped between 0 and 2 - assert 1 clamped between 1 and 2 is 1 with error "(single ints) min = value < max", expected 1, got 1 clamped between 1 and 2 - assert 2 clamped between 1 and 2 is 2 with error "(single ints) min < value = max", expected 2, got 2 clamped between 1 and 2 - assert 0 clamped between 1 and 2 is 1 with error "(single ints) value < min < max", expected 1, got 0 clamped between 1 and 2 - assert 3 clamped between 1 and 2 is 2 with error "(single ints) min < max < value", expected 2, got 3 clamped between 1 and 2 - assert 3 clamped between 2 and 1 is 2 with error "(single ints) max < min < value", expected 2, got 3 clamped between 2 and 1 - - assert 1.999 clamped between 0.0 and 2.0 is 1.999 with error "(single floats) min < value < max", expected 1.999, got 1.999 clamped between 0.0 and 2.0 - assert 1.999 clamped between 1.999 and 2.0 is 1.999 with error "(single floats) min = value < max", expected 1.999, got 1.999 clamped between 1.999 and 2.0 - assert 2.0 clamped between 1.999 and 2.0 is 2.0 with error "(single floats) min < value = max", expected 2.0, got 2.0 clamped between 1.999 and 2.0 - assert 0.0 clamped between 1.999 and 2.0 is 1.999 with error "(single floats) value < min < max", expected 1.999, got 0.0 clamped between 1.999 and 2.0 - assert 3.0 clamped between 1.999 and 2.0 is 2.0 with error "(single floats) min < max < value", expected 2.0, got 3.0 clamped between 1.999 and 2.0 - assert 2.999 clamped between 2.0 and 1.999 is 2.0 with error "(single floats) max < min < value", expected 2.0, got 2.999 clamped between 2.0 and 1.999 + assert 1 clamped between 0 and 2 is 1 with error "(single ints) min < value < max" + assert 1 clamped between 1 and 2 is 1 with error "(single ints) min = value < max" + assert 2 clamped between 1 and 2 is 2 with error "(single ints) min < value = max" + assert 0 clamped between 1 and 2 is 1 with error "(single ints) value < min < max" + assert 3 clamped between 1 and 2 is 2 with error "(single ints) min < max < value" + assert 3 clamped between 2 and 1 is 2 with error "(single ints) max < min < value" + + assert 1.999 clamped between 0.0 and 2.0 is 1.999 with error "(single floats) min < value < max" + assert 1.999 clamped between 1.999 and 2.0 is 1.999 with error "(single floats) min = value < max" + assert 2.0 clamped between 1.999 and 2.0 is 2.0 with error "(single floats) min < value = max" + assert 0.0 clamped between 1.999 and 2.0 is 1.999 with error "(single floats) value < min < max" + assert 3.0 clamped between 1.999 and 2.0 is 2.0 with error "(single floats) min < max < value" + assert 2.999 clamped between 2.0 and 1.999 is 2.0 with error "(single floats) max < min < value" # Lists set {_expected::*} to (0, 0, 1, 2, 2, and 2) # this is dumb but comparing the lists directly didn't work set {_got::*} to (-1, 0, 1, 2, 3, and 4) clamped between 0 and 2 loop {_expected::*}: - assert {_got::%loop-index%} is loop-value with error "(multiple ints)", expected loop-value, got {_got::%loop-index%} + assert {_got::%loop-index%} is loop-value with error "(multiple ints)" set {_got::*} to (-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0) clamped between 0.0 and 2.0 loop {_expected::*}: - assert {_got::%loop-index%} is loop-value with error "(multiple floats)", expected loop-value, got {_got::%loop-index%} + assert {_got::%loop-index%} is loop-value with error "(multiple floats)" # Edge Cases assert 1 clamped between {_null} and 2 is 1 with error "(single ints) min = null" @@ -33,87 +33,87 @@ test "clamp expression": assert isNaN(1 clamped between 0 and NaN value) is true with error "(single ints) min < value < NaN" assert isNaN(1 clamped between NaN value and 2) is true with error "(single ints) NaN < value < max" assert isNaN(NaN value clamped between 1 and 2) is true with error "(single ints) min < NaN < max" - assert infinity value clamped between 1 and 2 is 2 with error "(single ints) min < infinity < max", expected 2, got infinity value clamped between 1 and 2 - assert -infinity value clamped between 1 and 2 is 1 with error "(single ints) min < -infinity < max", expected 1, got -infinity value clamped between 1 and 2 - assert 1 clamped between 0 and infinity value is 1 with error "(single ints) min < value < infinity", expected 1, got 1 clamped between 0 and infinity value - assert 1 clamped between -infinity value and 2 is 1 with error "(single ints) -infinity < value < max", expected 1, got 1 clamped between -infinity value and 2 + assert infinity value clamped between 1 and 2 is 2 with error "(single ints) min < infinity < max" + assert -infinity value clamped between 1 and 2 is 1 with error "(single ints) min < -infinity < max" + assert 1 clamped between 0 and infinity value is 1 with error "(single ints) min < value < infinity" + assert 1 clamped between -infinity value and 2 is 1 with error "(single ints) -infinity < value < max" set {_expected::*} to (NaN value, 0.0, and 2.0) set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped between 0.0 and 2.0 assert isNaN({_got::1}) is true with error "(edge cases list) NaN" - assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity", expected {_expected::2}, got {_got::2} - assert {_got::3} is {_expected::3} with error "(edge cases list) infinity", expected {_expected::3}, got {_got::3} + assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity" + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity" # Upper limits only (below) # Normal Cases - assert 1 clamped below 2 is 1 with error "(single ints) value < max", expected 1, got 1 clamped below 2 - assert 2 clamped below 2 is 2 with error "(single ints) value = max", expected 2, got 2 clamped below 2 - assert 3 clamped below 2 is 2 with error "(single ints) max < value", expected 2, got 3 clamped below 2 + assert 1 clamped below 2 is 1 with error "(single ints) value < max" + assert 2 clamped below 2 is 2 with error "(single ints) value = max" + assert 3 clamped below 2 is 2 with error "(single ints) max < value" - assert 1.999 clamped below 2.0 is 1.999 with error "(single floats) value < max", expected 1.999, got 1.999 clamped below 2.0 - assert 2.0 clamped below 2.0 is 2.0 with error "(single floats) value = max", expected 2.0, got 2.0 clamped below 2.0 - assert 3.0 clamped below 2.0 is 2.0 with error "(single floats) min < max < value", expected 2.0, got 3.0 clamped below 2.0 + assert 1.999 clamped below 2.0 is 1.999 with error "(single floats) value < max" + assert 2.0 clamped below 2.0 is 2.0 with error "(single floats) value = max" + assert 3.0 clamped below 2.0 is 2.0 with error "(single floats) min < max < value" # Lists set {_expected::*} to (-1, 0, 1, 2, 2, and 2) # this is dumb but comparing the lists directly didn't work set {_got::*} to (-1, 0, 1, 2, 3, and 4) clamped below 2 loop {_expected::*}: - assert {_got::%loop-index%} is loop-value with error "(multiple ints)", expected loop-value, got {_got::%loop-index%} + assert {_got::%loop-index%} is loop-value with error "(multiple ints)" set {_expected::*} to (-1.999, 0, 1, 2, 2, and 2) set {_got::*} to (-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0) clamped below 2.0 loop {_expected::*}: - assert {_got::%loop-index%} is loop-value with error "(multiple floats)", expected loop-value, got {_got::%loop-index%} + assert {_got::%loop-index%} is loop-value with error "(multiple floats)" # Edge Cases assert 2 clamped below {_null} is 2 with error "(single ints) max = null" assert {_null} clamped below 2 is not set with error "(single ints) value = null" assert isNaN(1 clamped below NaN value) is true with error "(single ints) value < NaN" assert isNaN(NaN value clamped below 2) is true with error "(single ints) NaN < max" - assert infinity value clamped below 2 is 2 with error "(single ints) infinity < max", expected 2, got infinity value clamped below 2 - assert -infinity value clamped below 2 is -infinity value with error "(single ints) -infinity < max", expected -infinity value, got -infinity value clamped below 2 - assert 1 clamped below infinity value is 1 with error "(single ints) value < infinity", expected 1, got 1 clamped below infinity value + assert infinity value clamped below 2 is 2 with error "(single ints) infinity < max" + assert -infinity value clamped below 2 is -infinity value with error "(single ints) -infinity < max" + assert 1 clamped below infinity value is 1 with error "(single ints) value < infinity" set {_expected::*} to (NaN value, -infinity value, and 2.0) set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped below 2.0 assert isNaN({_got::1}) is true with error "(edge cases list) NaN" - assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity", expected {_expected::2}, got {_got::2} - assert {_got::3} is {_expected::3} with error "(edge cases list) infinity", expected {_expected::3}, got {_got::3} + assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity" + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity" # Lower limits only (above) # Normal Cases - assert 1 clamped above 0 is 1 with error "(single ints) min < value", expected 1, got 1 clamped above 0 - assert 1 clamped above 1 is 1 with error "(single ints) min = value", expected 1, got 1 clamped above 1 - assert 0 clamped above 1 is 1 with error "(single ints) value < min", expected 1, got 0 clamped above 1 + assert 1 clamped above 0 is 1 with error "(single ints) min < value" + assert 1 clamped above 1 is 1 with error "(single ints) min = value" + assert 0 clamped above 1 is 1 with error "(single ints) value < min" - assert 1.999 clamped above 0.0 is 1.999 with error "(single floats) min < value", expected 1.999, got 1.999 clamped above 0.0 - assert 1.999 clamped above 1.999 is 1.999 with error "(single floats) min = value", expected 1.999, got 1.999 clamped above 1.999 - assert 0.0 clamped above 1.999 is 1.999 with error "(single floats) value < min", expected 1.999, got 0.0 clamped above 1.999 + assert 1.999 clamped above 0.0 is 1.999 with error "(single floats) min < value" + assert 1.999 clamped above 1.999 is 1.999 with error "(single floats) min = value" + assert 0.0 clamped above 1.999 is 1.999 with error "(single floats) value < min" # Lists set {_expected::*} to (0, 0, 1, 2, 3, and 4) # this is dumb but comparing the lists directly didn't work set {_got::*} to (-1, 0, 1, 2, 3, and 4) clamped above 0 loop {_expected::*}: - assert {_got::%loop-index%} is loop-value with error "(multiple ints)", expected loop-value, got {_got::%loop-index%} + assert {_got::%loop-index%} is loop-value with error "(multiple ints)" set {_got::*} to (-1.999, 0.0, 1.0, 2.0, 3.0, and 4.0) clamped above 0.0 loop {_expected::*}: - assert {_got::%loop-index%} is loop-value with error "(multiple floats)", expected loop-value, got {_got::%loop-index%} + assert {_got::%loop-index%} is loop-value with error "(multiple floats)" # Edge Cases assert 1 clamped above {_null} is 1 with error "(single ints) min = null" assert {_null} clamped above 1 is not set with error "(single ints) value = null" assert isNaN(1 clamped above NaN value) is true with error "(single ints) NaN < value" assert isNaN(NaN value clamped above 1) is true with error "(single ints) min < NaN" - assert infinity value clamped above 1 is infinity value with error "(single ints) min < infinity", expected infinity value, got infinity value clamped above 1 - assert -infinity value clamped above 1 is 1 with error "(single ints) min < -infinity", expected 1, got -infinity value clamped above 1 - assert 1 clamped above -infinity value is 1 with error "(single ints) -infinity < value", expected 1, got 1 clamped above -infinity value + assert infinity value clamped above 1 is infinity value with error "(single ints) min < infinity" + assert -infinity value clamped above 1 is 1 with error "(single ints) min < -infinity" + assert 1 clamped above -infinity value is 1 with error "(single ints) -infinity < value" set {_expected::*} to (NaN value, 0.0, and infinity value) set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped above 0.0 assert isNaN({_got::1}) is true with error "(edge cases list) NaN" - assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity", expected {_expected::2}, got {_got::2} - assert {_got::3} is {_expected::3} with error "(edge cases list) infinity", expected {_expected::3}, got {_got::3} \ No newline at end of file + assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity" + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity" \ No newline at end of file From df8ddd70e187c249613af032a886627e0e87c9e4 Mon Sep 17 00:00:00 2001 From: Moderocky Date: Thu, 2 May 2024 17:06:41 +0100 Subject: [PATCH 10/10] Apply suggestions from code review Co-authored-by: sovdee <10354869+sovdeeth@users.noreply.github.com> --- src/test/skript/tests/syntaxes/expressions/ExprClamp.sk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk b/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk index 746f32386f5..83ee267e7c3 100644 --- a/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk +++ b/src/test/skript/tests/syntaxes/expressions/ExprClamp.sk @@ -116,4 +116,4 @@ test "clamp expression": set {_got::*} to ({_null}, NaN value, -infinity value, infinity value) clamped above 0.0 assert isNaN({_got::1}) is true with error "(edge cases list) NaN" assert {_got::2} is {_expected::2} with error "(edge cases list) -infinity" - assert {_got::3} is {_expected::3} with error "(edge cases list) infinity" \ No newline at end of file + assert {_got::3} is {_expected::3} with error "(edge cases list) infinity"