From 5ec6276674fb1334f9627a871cc3b642128a2e95 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Thu, 24 Nov 2011 20:53:34 +0100 Subject: [PATCH] Made ExpressionTest invoke the optimizer every time it evaluates an expression. This way that thing at least gets SOME coverage. --- .../worldedit/expression/ExpressionTest.java | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java b/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java index bb420f0ae..00e2a8046 100644 --- a/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java +++ b/src/test/java/com/sk89q/worldedit/expression/ExpressionTest.java @@ -7,6 +7,7 @@ import org.junit.*; import com.sk89q.worldedit.expression.lexer.LexerException; import com.sk89q.worldedit.expression.parser.ParserException; +import com.sk89q.worldedit.expression.runtime.EvaluationException; public class ExpressionTest { @Test @@ -24,14 +25,14 @@ public class ExpressionTest { assertEquals(atan2(3, 4), simpleEval("atan2(3, 4)"), 0); // check variables - assertEquals(8, Expression.compile("foo+bar", "foo", "bar").evaluate(5, 3), 0); + assertEquals(8, compile("foo+bar", "foo", "bar").evaluate(5, 3), 0); } @Test public void testErrors() throws ExpressionException { // test lexer errors try { - Expression.compile("#"); + compile("#"); fail("Error expected"); } catch (LexerException e) { assertEquals("Error position", 0, e.getPosition()); @@ -39,30 +40,30 @@ public class ExpressionTest { // test parser errors try { - Expression.compile("x"); + compile("x"); fail("Error expected"); } catch (ParserException e) { assertEquals("Error position", 0, e.getPosition()); } try { - Expression.compile("x()"); + compile("x()"); fail("Error expected"); } catch (ParserException e) { assertEquals("Error position", 0, e.getPosition()); } try { - Expression.compile("("); + compile("("); fail("Error expected"); } catch (ParserException e) {} try { - Expression.compile("x("); + compile("x("); fail("Error expected"); } catch (ParserException e) {} } @Test public void testAssign() throws ExpressionException { - Expression foo = Expression.compile("{a=x} b=y; c=z", "x", "y", "z", "a", "b", "c"); + Expression foo = compile("{a=x} b=y; c=z", "x", "y", "z", "a", "b", "c"); foo.evaluate(2, 3, 5); assertEquals(2, foo.getVariable("a").getValue(), 0); assertEquals(3, foo.getVariable("b").getValue(), 0); @@ -75,13 +76,13 @@ public class ExpressionTest { assertEquals(5, simpleEval("if (0) x=4; else y=5; x*10+y;"), 0); // test 'dangling else' - final Expression expression1 = Expression.compile("if (1) if (0) x=4; else y=5;", "x", "y"); + final Expression expression1 = compile("if (1) if (0) x=4; else y=5;", "x", "y"); expression1.evaluate(1, 2); assertEquals(1, expression1.getVariable("x").getValue(), 0); assertEquals(5, expression1.getVariable("y").getValue(), 0); // test if the if construct is correctly recognized as a statement - final Expression expression2 = Expression.compile("if (0) if (1) x=5; y=4;", "x", "y"); + final Expression expression2 = compile("if (0) if (1) x=5; y=4;", "x", "y"); expression2.evaluate(1, 2); assertEquals(4, expression2.getVariable("y").getValue(), 0); } @@ -98,7 +99,14 @@ public class ExpressionTest { assertEquals(12345, simpleEval("y=0; for (i=1,5) { y *= 10; y += i; } y"), 0); } - private double simpleEval(String expression) throws ExpressionException { - return Expression.compile(expression).evaluate(); + private double simpleEval(String expressionString) throws ExpressionException { + final Expression expression = compile(expressionString); + return expression.evaluate(); + } + + private Expression compile(String expressionString, String... variableNames) throws ExpressionException, EvaluationException { + final Expression expression = Expression.compile(expressionString, variableNames); + expression.optimize(); + return expression; } }