Skip to content

Commit

Permalink
test(SniperPrettyPrinter): modification on first statement of a block…
Browse files Browse the repository at this point in the history
… should not remove new line (INRIA#3401) (INRIA#3402)
  • Loading branch information
fermadeiral authored and monperrus committed Jun 12, 2020
1 parent 793fabf commit 8cb1073
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/test/java/spoon/test/prettyprinter/TestSniperPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import spoon.SpoonException;
import spoon.processing.Processor;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtConstructorCall;
import spoon.reflect.code.CtCodeSnippetExpression;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtStatement;
import spoon.reflect.code.CtThrow;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtField;
Expand All @@ -44,6 +46,7 @@
import spoon.support.modelobs.SourceFragmentCreator;
import spoon.support.sniper.SniperJavaPrettyPrinter;
import spoon.test.prettyprinter.testclasses.OneLineMultipleVariableDeclaration;
import spoon.test.prettyprinter.testclasses.Throw;
import spoon.test.prettyprinter.testclasses.InvocationReplacement;
import spoon.test.prettyprinter.testclasses.ToBeChanged;

Expand Down Expand Up @@ -71,6 +74,23 @@
public class TestSniperPrinter {

@Test
public void testPrintInsertedThrow() {
testSniper(Throw.class.getName(), type -> {
CtConstructorCall ctConstructorCall = (CtConstructorCall) type.getMethodsByName("foo").get(0).getBody().getStatements().get(0);
CtThrow ctThrow = type.getFactory().createCtThrow(ctConstructorCall.toString());
ctConstructorCall.replace(ctThrow);
}, (type, printed) -> {
assertIsPrintedWithExpectedChanges(type, printed,
"\\Qvoid foo(int x) {\n" +
"\t\tnew IllegalArgumentException(\"x must be nonnegative\");\n" +
"\t}",
"void foo(int x) {\n" +
"\t\tthrow new java.lang.IllegalArgumentException(\"x must be nonnegative\");\n" +
"\t}");
});
}

@Test
public void testPrintReplacementOfInvocation() {
testSniper(InvocationReplacement.class.getName(), type -> {
CtLocalVariable localVariable = (CtLocalVariable) type.getMethodsByName("main").get(0).getBody().getStatements().get(0);
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/spoon/test/prettyprinter/testclasses/Throw.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package spoon.test.prettyprinter.testclasses;

public class Throw {
void foo(int x) {
new IllegalArgumentException("x must be nonnegative");
}
}

0 comments on commit 8cb1073

Please sign in to comment.