Skip to content

Commit

Permalink
Flesh out tests for DeleteStatement and ensure that statements on (#2627
Browse files Browse the repository at this point in the history
)

G.CompilationUnit can be removed
  • Loading branch information
shanman190 authored Jan 11, 2023
1 parent 24ca597 commit c0ea0ae
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 43 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.groovy;

import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.groovy.tree.G;
import org.openrewrite.java.DeleteStatement;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.test.RewriteTest;

import java.util.List;

import static org.openrewrite.groovy.Assertions.groovy;
import static org.openrewrite.test.RewriteTest.toRecipe;

class DeleteStatementTest implements RewriteTest {
@Test
void deleteStatement() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new GroovyIsoVisitor<>() {
@Override
public G.CompilationUnit visitCompilationUnit(G.CompilationUnit cu, ExecutionContext ctx) {
G.CompilationUnit g = super.visitCompilationUnit(cu, ctx);
List<Statement> statements = g.getStatements();
for (int i = 0; i < statements.size(); i++) {
Statement s = statements.get(i);
if (i == 1) {
g = (G.CompilationUnit) new DeleteStatement<>(s).visit(cu, ctx);
}
}
return g;
}
})),
groovy(
"""
int i = 0
i = 1
""",
"""
int i = 0
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,263 @@ public class A {
)
);
}

@Test
void deleteIfThenBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.If visitIf(J.If iff, ExecutionContext ctx) {
J.If i = super.visitIf(iff, ctx);
if (!((J.Block) i.getThenPart()).getStatements().isEmpty()) {
doAfterVisit(new DeleteStatement<>(i.getThenPart()));
}
return i;
}
})),
java(
"""
public class A {
public void a() {
if (true) {
int i = 0;
}
}
}
""",
"""
public class A {
public void a() {
if (true){}
}
}
"""
)
);
}

@Test
void deleteIfElseBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.If visitIf(J.If iff, ExecutionContext ctx) {
J.If i = super.visitIf(iff, ctx);
if (!((J.Block) i.getElsePart().getBody()).getStatements().isEmpty()) {
doAfterVisit(new DeleteStatement<>(i.getElsePart().getBody()));
}
return i;
}
})),
java(
"""
public class A {
public void a() {
if (false) {
int i = 0;
} else {
int i = 0;
}
}
}
""",
"""
public class A {
public void a() {
if (false) {
int i = 0;
} else{}
}
}
"""
)
);
}

@Test
void deleteForBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
J.ForLoop f = super.visitForLoop(forLoop, ctx);
if (!((J.Block) f.getBody()).getStatements().isEmpty()) {
doAfterVisit(new DeleteStatement<>(f.getBody()));
}
return f;
}
})),
java(
"""
public class A {
public void a() {
for (int i = 0; i < 1; i++) {
int j = 0;
}
}
}
""",
"""
public class A {
public void a() {
for (int i = 0; i < 1; i++){}
}
}
"""
)
);
}

@Test
void dontDeleteForControl() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ForLoop visitForLoop(J.ForLoop forLoop, ExecutionContext ctx) {
J.ForLoop f = super.visitForLoop(forLoop, ctx);
doAfterVisit(new DeleteStatement<>(f.getControl().getInit().get(0)));
doAfterVisit(new DeleteStatement<>(f.getControl().getUpdate().get(0)));
return f;
}
})),
java(
"""
public class A {
public void a() {
for (int i = 0; i < 1; i++) {
int j = 0;
}
}
}
"""
)
);
}

@Test
void deleteForEachBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ForEachLoop visitForEachLoop(J.ForEachLoop forLoop, ExecutionContext ctx) {
J.ForEachLoop f = super.visitForEachLoop(forLoop, ctx);
doAfterVisit(new DeleteStatement<>(f.getControl().getVariable()));
return f;
}
})),
java(
"""
public class A {
public void a() {
for (int i : new int[]{ 1 }) {
int j = 0;
}
}
}
"""
)
);
}

@Test
void dontDeleteForEachControl() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.ForEachLoop visitForEachLoop(J.ForEachLoop forLoop, ExecutionContext ctx) {
J.ForEachLoop f = super.visitForEachLoop(forLoop, ctx);
if (!((J.Block) f.getBody()).getStatements().isEmpty()) {
doAfterVisit(new DeleteStatement<>(f.getBody()));
}
return f;
}
})),
java(
"""
public class A {
public void a() {
for (int i : new int[]{ 1 }) {
int j = 0;
}
}
}
""",
"""
public class A {
public void a() {
for (int i : new int[]{ 1 }){}
}
}
"""
)
);
}

@Test
void deleteWhileBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.WhileLoop visitWhileLoop(J.WhileLoop whileLoop, ExecutionContext ctx) {
J.WhileLoop w = super.visitWhileLoop(whileLoop, ctx);
if (!((J.Block) w.getBody()).getStatements().isEmpty()) {
doAfterVisit(new DeleteStatement<>(w.getBody()));
}
return w;
}
})),
java(
"""
public class A {
public void a() {
while (true) {
int i = 0;
}
}
}
""",
"""
public class A {
public void a() {
while (true){}
}
}
"""
)
);
}

@Test
void deleteDoWhileBody() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaIsoVisitor<>() {
@Override
public J.DoWhileLoop visitDoWhileLoop(J.DoWhileLoop doWhileLoop, ExecutionContext ctx) {
J.DoWhileLoop w = super.visitDoWhileLoop(doWhileLoop, ctx);
if (!((J.Block) w.getBody()).getStatements().isEmpty()) {
doAfterVisit(new DeleteStatement<>(w.getBody()));
}
return w;
}
})),
java(
"""
public class A {
public void a() {
do {
int i = 0;
} while (true);
}
}
""",
"""
public class A {
public void a() {
do{} while (true);
}
}
"""
)
);
}
}
Loading

0 comments on commit c0ea0ae

Please sign in to comment.