Made ExpressionTest invoke the optimizer every time it evaluates an expression.

This way that thing at least gets SOME coverage.
Dieser Commit ist enthalten in:
TomyLobo 2011-11-24 20:53:34 +01:00
Ursprung c1e151ac19
Commit 5ec6276674

Datei anzeigen

@ -7,6 +7,7 @@ import org.junit.*;
import com.sk89q.worldedit.expression.lexer.LexerException; import com.sk89q.worldedit.expression.lexer.LexerException;
import com.sk89q.worldedit.expression.parser.ParserException; import com.sk89q.worldedit.expression.parser.ParserException;
import com.sk89q.worldedit.expression.runtime.EvaluationException;
public class ExpressionTest { public class ExpressionTest {
@Test @Test
@ -24,14 +25,14 @@ public class ExpressionTest {
assertEquals(atan2(3, 4), simpleEval("atan2(3, 4)"), 0); assertEquals(atan2(3, 4), simpleEval("atan2(3, 4)"), 0);
// check variables // 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 @Test
public void testErrors() throws ExpressionException { public void testErrors() throws ExpressionException {
// test lexer errors // test lexer errors
try { try {
Expression.compile("#"); compile("#");
fail("Error expected"); fail("Error expected");
} catch (LexerException e) { } catch (LexerException e) {
assertEquals("Error position", 0, e.getPosition()); assertEquals("Error position", 0, e.getPosition());
@ -39,30 +40,30 @@ public class ExpressionTest {
// test parser errors // test parser errors
try { try {
Expression.compile("x"); compile("x");
fail("Error expected"); fail("Error expected");
} catch (ParserException e) { } catch (ParserException e) {
assertEquals("Error position", 0, e.getPosition()); assertEquals("Error position", 0, e.getPosition());
} }
try { try {
Expression.compile("x()"); compile("x()");
fail("Error expected"); fail("Error expected");
} catch (ParserException e) { } catch (ParserException e) {
assertEquals("Error position", 0, e.getPosition()); assertEquals("Error position", 0, e.getPosition());
} }
try { try {
Expression.compile("("); compile("(");
fail("Error expected"); fail("Error expected");
} catch (ParserException e) {} } catch (ParserException e) {}
try { try {
Expression.compile("x("); compile("x(");
fail("Error expected"); fail("Error expected");
} catch (ParserException e) {} } catch (ParserException e) {}
} }
@Test @Test
public void testAssign() throws ExpressionException { 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); foo.evaluate(2, 3, 5);
assertEquals(2, foo.getVariable("a").getValue(), 0); assertEquals(2, foo.getVariable("a").getValue(), 0);
assertEquals(3, foo.getVariable("b").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); assertEquals(5, simpleEval("if (0) x=4; else y=5; x*10+y;"), 0);
// test 'dangling else' // 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); expression1.evaluate(1, 2);
assertEquals(1, expression1.getVariable("x").getValue(), 0); assertEquals(1, expression1.getVariable("x").getValue(), 0);
assertEquals(5, expression1.getVariable("y").getValue(), 0); assertEquals(5, expression1.getVariable("y").getValue(), 0);
// test if the if construct is correctly recognized as a statement // 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); expression2.evaluate(1, 2);
assertEquals(4, expression2.getVariable("y").getValue(), 0); 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); assertEquals(12345, simpleEval("y=0; for (i=1,5) { y *= 10; y += i; } y"), 0);
} }
private double simpleEval(String expression) throws ExpressionException { private double simpleEval(String expressionString) throws ExpressionException {
return Expression.compile(expression).evaluate(); 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;
} }
} }