|
| 1 | +package ch.njol.skript.expressions.arithmetic; |
| 2 | + |
| 3 | +import ch.njol.skript.lang.SkriptParser; |
| 4 | +import ch.njol.skript.lang.Variable; |
| 5 | +import ch.njol.skript.lang.util.ContextlessEvent; |
| 6 | +import ch.njol.skript.test.runner.SkriptJUnitTest; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.Test; |
| 9 | +import org.skriptlang.skript.lang.simplification.SimplifiedLiteral; |
| 10 | + |
| 11 | +/** |
| 12 | + * Check if arithmetic expressions are simplified correctly. |
| 13 | + */ |
| 14 | +public class ArithmeticSimplificationTest extends SkriptJUnitTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void test() { |
| 18 | + //noinspection unchecked |
| 19 | + var arithmetic = new SkriptParser("5 * 2 - 3 + 4").parseExpression(Number.class); |
| 20 | + Assert.assertTrue(arithmetic instanceof SimplifiedLiteral<? extends Number>); |
| 21 | + Assert.assertEquals(5 * 2 - 3 + 4, ((SimplifiedLiteral<? extends Number>) arithmetic).getSingle().intValue()); |
| 22 | + |
| 23 | + //noinspection unchecked |
| 24 | + arithmetic = new SkriptParser("5 + 4").parseExpression(Number.class); |
| 25 | + Assert.assertTrue(arithmetic instanceof SimplifiedLiteral<? extends Number>); |
| 26 | + Assert.assertEquals(5 + 4, ((SimplifiedLiteral<? extends Number>) arithmetic).getSingle().intValue()); |
| 27 | + |
| 28 | + //noinspection unchecked |
| 29 | + arithmetic = new SkriptParser("5 - 10 + {_loc}").parseExpression(Number.class); |
| 30 | + Assert.assertTrue(arithmetic instanceof ExprArithmetic<?,?,?>); |
| 31 | + var leftArith = ((ExprArithmetic<?,?,?>) arithmetic).getFirst(); |
| 32 | + var rightArith = ((ExprArithmetic<?,?,?>) arithmetic).getSecond(); |
| 33 | + Assert.assertTrue(leftArith instanceof SimplifiedLiteral<?>); |
| 34 | + Assert.assertTrue(rightArith instanceof Variable<?>); |
| 35 | + //noinspection unchecked,DataFlowIssue |
| 36 | + Assert.assertEquals(5 - 10, ((ExprArithmetic<?,?,Number>) arithmetic).getSingle(ContextlessEvent.get()).intValue()); |
| 37 | + } |
| 38 | + |
| 39 | +} |
0 commit comments