Skip to content

Commit

Permalink
test: Move Foo.java and ToEvaluate.java to testclasses (#2164)
Browse files Browse the repository at this point in the history
  • Loading branch information
zielint0 authored and monperrus committed Jul 4, 2018
1 parent 40a0fb8 commit dbc7cb7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 90 deletions.
27 changes: 13 additions & 14 deletions src/test/java/spoon/test/eval/EvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtVariable;
import spoon.reflect.visitor.AccessibleVariablesFinder;
import spoon.reflect.visitor.CtInheritanceScanner;
import spoon.reflect.visitor.CtScanner;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.reflect.eval.InlinePartialEvaluator;
import spoon.support.reflect.eval.VisitorPartialEvaluator;
import spoon.test.eval.testclasses.Foo;

import java.util.List;

Expand All @@ -27,7 +26,7 @@ public class EvalTest {

@Test
public void testStringConcatenation() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("testStrings").get(0).getBody();
Expand All @@ -41,7 +40,7 @@ public void testStringConcatenation() throws Exception {

@Test
public void testArrayLength() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("testArray").get(0).getBody();
Expand All @@ -52,7 +51,7 @@ public void testArrayLength() throws Exception {

@Test
public void testDoNotSimplify() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("testDoNotSimplify").get(0).getBody();
Expand All @@ -63,19 +62,19 @@ public void testDoNotSimplify() throws Exception {

@Test
public void testDoNotSimplifyCasts() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("testDoNotSimplifyCasts").get(0).getBody();
assertEquals(1, b.getStatements().size());
b = b.partiallyEvaluate();
assertEquals("return ((U) ((java.lang.Object) (spoon.test.eval.ToEvaluate.castTarget(element).getClass())))", b.getStatements().get(0).toString());
assertEquals("return ((U) ((java.lang.Object) (spoon.test.eval.testclasses.ToEvaluate.castTarget(element).getClass())))", b.getStatements().get(0).toString());
}

@Test
public void testScanAPartiallyEvaluatedElement() throws Exception {
// contract: once partially evaluated a code element should be still visitable to find variables
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("testDoNotSimplifyCasts").get(0).getBody();
Expand All @@ -89,7 +88,7 @@ public void testScanAPartiallyEvaluatedElement() throws Exception {

@Test
public void testTryCatchAndStatement() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("tryCatchAndStatement").get(0).getBody();
Expand All @@ -100,15 +99,15 @@ public void testTryCatchAndStatement() throws Exception {

@Test
public void testDoNotSimplifyToExpressionWhenStatementIsExpected() throws Exception {
CtClass<?> type = build("spoon.test.eval", "ToEvaluate");
CtClass<?> type = build("spoon.test.eval.testclasses", "ToEvaluate");
assertEquals("ToEvaluate", type.getSimpleName());

CtBlock<?> b = type.getMethodsByName("simplifyOnlyWhenPossible").get(0).getBody();
assertEquals(3, b.getStatements().size());
b = b.partiallyEvaluate();
assertEquals("spoon.test.eval.ToEvaluate.class.getName()", b.getStatements().get(0).toString());
assertEquals("java.lang.System.out.println(spoon.test.eval.ToEvaluate.getClassLoader())", b.getStatements().get(1).toString());
assertEquals("return \"spoon.test.eval.ToEvaluate\"", b.getStatements().get(2).toString());
assertEquals("spoon.test.eval.testclasses.ToEvaluate.class.getName()", b.getStatements().get(0).toString());
assertEquals("java.lang.System.out.println(spoon.test.eval.testclasses.ToEvaluate.getClassLoader())", b.getStatements().get(1).toString());
assertEquals("return \"spoon.test.eval.testclasses.ToEvaluate\"", b.getStatements().get(2).toString());
}

@Test
Expand Down Expand Up @@ -162,7 +161,7 @@ public void testVisitorPartialEvaluator_if() throws Exception {
@Test
public void testVisitorPartialEvaluatorScanner() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/java/spoon/test/eval/Foo.java");
launcher.addInputResource("src/test/java/spoon/test/eval/testclasses/Foo.java");
launcher.buildModel();
VisitorPartialEvaluator eval = new VisitorPartialEvaluator();
CtType<?> foo = launcher.getFactory().Type().get((Class<?>) Foo.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package spoon.test.eval;
package spoon.test.eval.testclasses;

public class Foo {
final boolean b0 = true && false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
package spoon.test.eval;

import spoon.reflect.declaration.CtElement;

public class ToEvaluate {

static final String S1 = "S1";
static final String S2 = "S2";
static final String S1S2_1 = S1 + "S2";
static final String S1S2_2 = S1 + S2;
static final int I1 = 10;

public static void testStrings() {
// all the following code will be removed by the partial evaluator
if (!S1S2_1.equals(S1 + S2)) {
System.out.println("dead code");
}
if (!S1S2_1.equals("S1S2")) {
System.out.println("dead code");
}
if (!S1S2_2.equals("S1S2")) {
System.out.println("dead code");
}
if (!S1S2_1.equals(S1S2_2)) {
System.out.println("dead code");
}
}

@SuppressWarnings("unused")
public static void testInts() {
// all the following code will be removed by the partial evaluator
if (I1 + 1 != 11) {
System.out.println("dead code");
}
}

public static void testArray() {
// all the following code will be removed by the partial evaluator
if (new String[]{"a", null, "b"}.length == 11) {
System.out.println("dead code");
}
}

public static void testDoNotSimplify(String className, String methodName) {
// this code must not be simplified
java.lang.System.out.println(((("enter: " + className) + " - ") + methodName));
}

public static <U> U testDoNotSimplifyCasts(CtElement element) {
// this code must not be simplified
return ((U) ((Object) (castTarget(element).getClass())));
}

private static <T> T castTarget(CtElement element) {
return (T) element;
}

private static String tryCatchAndStatement(CtElement element) {
try {
element.getClass();
} catch (RuntimeException e) {
throw e;
}
return "This must not be removed";
}

private static String simplifyOnlyWhenPossible(CtElement element) {
//this must not be simplified because literal is not a statement.
ToEvaluate.class.getName();
//this must not be simplified because ClassLoader instance is not a literal
System.out.println(ToEvaluate.class.getClassLoader());
//this can be simplified because return expects expression
return ToEvaluate.class.getName();
}
}
package spoon.test.eval.testclasses;

import spoon.reflect.declaration.CtElement;

public class ToEvaluate {

static final String S1 = "S1";
static final String S2 = "S2";
static final String S1S2_1 = S1 + "S2";
static final String S1S2_2 = S1 + S2;
static final int I1 = 10;

public static void testStrings() {
// all the following code will be removed by the partial evaluator
if (!S1S2_1.equals(S1 + S2)) {
System.out.println("dead code");
}
if (!S1S2_1.equals("S1S2")) {
System.out.println("dead code");
}
if (!S1S2_2.equals("S1S2")) {
System.out.println("dead code");
}
if (!S1S2_1.equals(S1S2_2)) {
System.out.println("dead code");
}
}

@SuppressWarnings("unused")
public static void testInts() {
// all the following code will be removed by the partial evaluator
if (I1 + 1 != 11) {
System.out.println("dead code");
}
}

public static void testArray() {
// all the following code will be removed by the partial evaluator
if (new String[]{"a", null, "b"}.length == 11) {
System.out.println("dead code");
}
}

public static void testDoNotSimplify(String className, String methodName) {
// this code must not be simplified
java.lang.System.out.println(((("enter: " + className) + " - ") + methodName));
}

public static <U> U testDoNotSimplifyCasts(CtElement element) {
// this code must not be simplified
return ((U) ((Object) (castTarget(element).getClass())));
}

private static <T> T castTarget(CtElement element) {
return (T) element;
}

private static String tryCatchAndStatement(CtElement element) {
try {
element.getClass();
} catch (RuntimeException e) {
throw e;
}
return "This must not be removed";
}

private static String simplifyOnlyWhenPossible(CtElement element) {
//this must not be simplified because literal is not a statement.
ToEvaluate.class.getName();
//this must not be simplified because ClassLoader instance is not a literal
System.out.println(ToEvaluate.class.getClassLoader());
//this can be simplified because return expects expression
return ToEvaluate.class.getName();
}
}

0 comments on commit dbc7cb7

Please sign in to comment.