From a283ec48221f12f60ef76d8613a070e86908847c Mon Sep 17 00:00:00 2001 From: traceyyoshima Date: Mon, 6 Mar 2023 09:18:55 -0700 Subject: [PATCH] Fix Assertions#kotlin(before, after) fixes #30 --- .../org/openrewrite/kotlin/Assertions.java | 2 +- .../openrewrite/kotlin/AssertionsTest.java | 46 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/openrewrite/kotlin/AssertionsTest.java diff --git a/src/main/java/org/openrewrite/kotlin/Assertions.java b/src/main/java/org/openrewrite/kotlin/Assertions.java index 0bf48c38a..a17954680 100644 --- a/src/main/java/org/openrewrite/kotlin/Assertions.java +++ b/src/main/java/org/openrewrite/kotlin/Assertions.java @@ -46,7 +46,7 @@ public static SourceSpecs kotlin(@Language("kotlin") @Nullable String before, @L public static SourceSpecs kotlin(@Language("kotlin") @Nullable String before, @Language("kotlin") String after, Consumer> spec) { - SourceSpec kotlin = new SourceSpec<>(K.CompilationUnit.class, null, KotlinParser.builder(), before, null); + SourceSpec kotlin = new SourceSpec<>(K.CompilationUnit.class, null, KotlinParser.builder(), before, s -> after); spec.accept(kotlin); return kotlin; } diff --git a/src/test/java/org/openrewrite/kotlin/AssertionsTest.java b/src/test/java/org/openrewrite/kotlin/AssertionsTest.java new file mode 100644 index 000000000..9624b776b --- /dev/null +++ b/src/test/java/org/openrewrite/kotlin/AssertionsTest.java @@ -0,0 +1,46 @@ +package org.openrewrite.kotlin; + +import org.junit.jupiter.api.Test; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Issue; +import org.openrewrite.java.tree.J; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.kotlin.Assertions.kotlin; +import static org.openrewrite.test.RewriteTest.toRecipe; + +// This class may be removed after recipes with Assertions are added. +public class AssertionsTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(toRecipe(() -> new KotlinIsoVisitor<>() { + @Override + public J visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext executionContext) { + if ("a".equals(variable.getSimpleName())) { + return variable.withName(variable.getName().withSimpleName("b")); + } + return variable; + } + })); + } + + @Issue("https://github.com/openrewrite/rewrite-kotlin/issues/30") + @Test + void isChanged() { + rewriteRun( + kotlin( + """ + class A { + val a = 1 + } + """, + """ + class A { + val b = 1 + } + """ + ) + ); + } +}