Skip to content

Commit

Permalink
Add alias import ordering test
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Sep 28, 2023
1 parent 93bd2eb commit ff579e9
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/test/java/org/openrewrite/kotlin/AddImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,62 @@ class A {
);
}

@Test
void importOrdering() {
rewriteRun(
spec -> spec.recipe(
importTypeRecipe("java.util.LinkedList")
),
kotlin(
"""
import java.util.HashMap
import java.util.StringJoiner
class A {
}
""",
"""
import java.util.HashMap
import java.util.LinkedList
import java.util.StringJoiner
class A {
}
"""
)
);
}

@Test
void importOrderingWithAlias() {
rewriteRun(
spec -> spec.recipe(
importTypeRecipe("java.util", "LinkedList", "MyList")
),
kotlin(
"""
import java.util.Calendar
import java.util.HashMap as MyHashMap
import java.util.StringJoiner as MyStringJoiner
class A {
}
""",
"""
import java.util.Calendar
import java.util.HashMap as MyHashMap
import java.util.LinkedList as MyList
import java.util.StringJoiner as MyStringJoiner
class A {
}
"""
)
);
}

@Test
void noImportOfImplicitTypes() {
rewriteRun(
Expand Down Expand Up @@ -244,12 +300,23 @@ static Recipe importTypeRecipe(String type) {
return toRecipe(() -> new KotlinIsoVisitor<>() {
@Override
public K.CompilationUnit visitCompilationUnit(K.CompilationUnit cu, ExecutionContext ctx) {

maybeAddImport(type, null, false);
return cu;
}
});
}

static Recipe importTypeRecipe(String packageName, String typeName, String alias) {
return toRecipe(() -> new KotlinIsoVisitor<>() {
@Override
public K.CompilationUnit visitCompilationUnit(K.CompilationUnit cu, ExecutionContext ctx) {
maybeAddImport(packageName, typeName, null, alias, false);
return cu;
}
});
}

static Recipe importMemberRecipe(String type, String member) {
return toRecipe(() -> new KotlinIsoVisitor<>() {
@Override
Expand Down

0 comments on commit ff579e9

Please sign in to comment.