diff --git a/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java b/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java index 2bfc1a97bd5..8b7492c4b76 100644 --- a/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java +++ b/src/main/java/spoon/support/reflect/eval/VisitorPartialEvaluator.java @@ -112,6 +112,8 @@ import java.util.List; /** + * + * Simplifies an AST by performing all operations that are statically known and changes the AST accordingly (eg "0+1" -> "1") * This visitor implements a simple partial evaluator for the program * compile-time metamodel. */ diff --git a/src/test/java/spoon/test/eval/EvalTest.java b/src/test/java/spoon/test/eval/EvalTest.java index 743cc5f81a5..b9ac2c2aa0b 100644 --- a/src/test/java/spoon/test/eval/EvalTest.java +++ b/src/test/java/spoon/test/eval/EvalTest.java @@ -1,12 +1,15 @@ package spoon.test.eval; -import static org.junit.Assert.assertEquals; -import static spoon.testing.utils.ModelUtils.build; - import org.junit.Test; - +import spoon.Launcher; import spoon.reflect.code.CtBlock; +import spoon.reflect.code.CtCodeElement; import spoon.reflect.declaration.CtClass; +import spoon.reflect.declaration.CtElement; +import spoon.support.reflect.eval.VisitorPartialEvaluator; + +import static org.junit.Assert.assertEquals; +import static spoon.testing.utils.ModelUtils.build; public class EvalTest { @@ -24,5 +27,52 @@ public void testStringConcatenation() throws Exception { assertEquals(0, b.getStatements().size()); } + @Test + public void testVisitorPartialEvaluator_binary() throws Exception { + Launcher launcher = new Launcher(); + + { // binary operator + CtCodeElement el = launcher.getFactory().Code().createCodeSnippetExpression("0+1").compile(); + VisitorPartialEvaluator eval = new VisitorPartialEvaluator(); + CtElement elnew = eval.evaluate(null, el); + assertEquals("1", elnew.toString()); + } + + { // binary operator + CtCodeElement el = launcher.getFactory().Code().createCodeSnippetExpression("(0+1)*3").compile(); + VisitorPartialEvaluator eval = new VisitorPartialEvaluator(); + CtElement elnew = eval.evaluate(null, el); + assertEquals("3", elnew.toString()); + } + + { // binary operator + CtCodeElement el = launcher.getFactory().Code().createCodeSnippetExpression("(0+1)*3>0").compile(); + VisitorPartialEvaluator eval = new VisitorPartialEvaluator(); + CtElement elnew = eval.evaluate(null, el); + assertEquals("true", elnew.toString()); + } + + { // binary operator + CtCodeElement el = launcher.getFactory().Code().createCodeSnippetExpression("(0+3-1)*3<=0").compile(); + VisitorPartialEvaluator eval = new VisitorPartialEvaluator(); + CtElement elnew = eval.evaluate(null, el); + assertEquals("false", elnew.toString()); + } + + } + + @Test + public void testVisitorPartialEvaluator_if() throws Exception { + Launcher launcher = new Launcher(); + { // the untaken branch is removed + CtCodeElement el = launcher.getFactory().Code().createCodeSnippetStatement("if (false) {System.out.println(\"foo\");} else {System.out.println(\"bar\");} ").compile(); + VisitorPartialEvaluator eval = new VisitorPartialEvaluator(); + CtElement elnew = eval.evaluate(null, el); + assertEquals("{\n" + + " java.lang.System.out.println(\"bar\");\n" + + "}", elnew.toString()); + } + + } }