Skip to content

Commit

Permalink
Fix Difference.apply() with redundant Unindent
Browse files Browse the repository at this point in the history
Redundant unindent may be discarded just like other whitespaces
  • Loading branch information
jooyunghan committed May 29, 2018
1 parent f1faa07 commit 0a54807
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Expand Up @@ -26,9 +26,12 @@
import org.junit.Test; import org.junit.Test;


import com.github.javaparser.JavaParser; import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.IntegerLiteralExpr; import com.github.javaparser.ast.expr.IntegerLiteralExpr;
import com.github.javaparser.ast.expr.NameExpr; import com.github.javaparser.ast.expr.NameExpr;
import com.github.javaparser.ast.stmt.ExpressionStmt;
import com.github.javaparser.ast.stmt.Statement; import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.ModifierVisitor;
import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest; import com.github.javaparser.printer.lexicalpreservation.AbstractLexicalPreservingTest;
import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter; import com.github.javaparser.printer.lexicalpreservation.LexicalPreservingPrinter;


Expand All @@ -45,9 +48,17 @@ Statement consider(String code) {


@Test @Test
public void ifStmtTransformation() throws IOException { public void ifStmtTransformation() throws IOException {
Statement ifStmt = consider("if (a) {} else {}"); Statement stmt = consider("if (a) {} else {}");
ifStmt.asIfStmt().setCondition(new NameExpr("b")); stmt.asIfStmt().setCondition(new NameExpr("b"));
assertTransformedToString("if (b) {} else {}", ifStmt); assertTransformedToString("if (b) {} else {}", stmt);
}

@Test
public void switchEntryCsmHasTrailingUnindent() throws IOException {
Statement stmt = consider("switch (a) { case 1: a; a; }");
NodeList<Statement> statements = stmt.asSwitchStmt().getEntry(0).getStatements();
statements.set(1, statements.get(1).clone()); // clone() to force replacement
assertTransformedToString("switch (a) { case 1: a; a; }", stmt);
} }


} }
Expand Up @@ -127,7 +127,7 @@ void apply() {
if (diffElement instanceof Kept) { if (diffElement instanceof Kept) {
Kept kept = (Kept) diffElement; Kept kept = (Kept) diffElement;


if (kept.isWhiteSpaceOrComment()) { if (kept.isWhiteSpaceOrComment() || kept.isIndent() || kept.isUnindent()) {
diffIndex++; diffIndex++;
} else { } else {
throw new IllegalStateException("Cannot keep element because we reached the end of nodetext: " throw new IllegalStateException("Cannot keep element because we reached the end of nodetext: "
Expand Down

0 comments on commit 0a54807

Please sign in to comment.