Skip to content

Commit

Permalink
test(VisitorPartialEvaluator): add tests for VisitorPartialEvaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored and tdurieux committed Nov 9, 2016
1 parent e5fda1b commit dde43c9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
58 changes: 54 additions & 4 deletions src/test/java/spoon/test/eval/EvalTest.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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());
}

}

}

0 comments on commit dde43c9

Please sign in to comment.