From dde43c95c0051894e030c01fe1773cbf56495731 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Wed, 9 Nov 2016 14:28:11 +0100 Subject: [PATCH] test(VisitorPartialEvaluator): add tests for VisitorPartialEvaluator --- .../reflect/eval/VisitorPartialEvaluator.java | 2 + src/test/java/spoon/test/eval/EvalTest.java | 58 +++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) 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..4a8b13933c6 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("{" + System.lineSeparator() + + " java.lang.System.out.println(\"bar\");" + System.lineSeparator() + + "}", elnew.toString()); + } + + } }