Skip to content

Commit

Permalink
Added type mapping tests for top level fields and methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Oct 9, 2023
1 parent 5a30966 commit 9420545
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
52 changes: 42 additions & 10 deletions src/test/java/org/openrewrite/kotlin/KotlinTypeMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
*/
package org.openrewrite.kotlin;

import org.jetbrains.kotlin.fir.declarations.FirProperty;
import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.Parser;
import org.openrewrite.internal.StringUtils;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Flag;
Expand All @@ -28,9 +31,13 @@
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.test.RewriteTest;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Collections.singletonList;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.ExecutionContext.REQUIRE_PRINT_EQUALS_INPUT;
Expand All @@ -40,26 +47,26 @@
@SuppressWarnings("ConstantConditions")
public class KotlinTypeMappingTest {
private static final String goat = StringUtils.readFully(KotlinTypeMappingTest.class.getResourceAsStream("/KotlinTypeGoat.kt"));

private static final K.CompilationUnit cu;
private static final K.ClassDeclaration goatClassDeclaration;

static {
InMemoryExecutionContext ctx = new InMemoryExecutionContext();
ctx.putMessage(REQUIRE_PRINT_EQUALS_INPUT, false);
//noinspection OptionalGetWithoutIsPresent
goatClassDeclaration = requireNonNull(((K.CompilationUnit) KotlinParser.builder()
.logCompilationWarningsAndErrors(true)
.build()
.parse(ctx, goat)
.findFirst()
.get())
cu = (K.CompilationUnit) KotlinParser.builder()
.logCompilationWarningsAndErrors(true)
.build()
.parseInputs(singletonList(new Parser.Input(Paths.get("KotlinTypeGoat.kt"), () -> new ByteArrayInputStream(goat.getBytes(StandardCharsets.UTF_8)))), null, ctx)
.findFirst()
.orElseThrow();

goatClassDeclaration = cu
.getStatements()
.stream()
.filter(K.ClassDeclaration.class::isInstance)
.findFirst()
.map(K.ClassDeclaration.class::cast)
.orElseThrow()
);
.orElseThrow();
}

private static final JavaType.Parameterized goatType =
Expand Down Expand Up @@ -128,6 +135,31 @@ void fieldType() {
assertThat(property.getSetter().getMethodType().toString().substring(declaringType.toString().length())).isEqualTo("{name=accessor,return=kotlin.Unit,parameters=[kotlin.Int]}");
}

@Test
void fileField() {
J.VariableDeclarations.NamedVariable nv = cu.getStatements().stream()
.filter(it -> it instanceof J.VariableDeclarations)
.flatMap(it -> ((J.VariableDeclarations) it).getVariables().stream())
.filter(it -> "field".equals(it.getSimpleName())).findFirst().orElseThrow();

assertThat(nv.getName().getType().toString()).isEqualTo("kotlin.Int");
assertThat(nv.getName().getFieldType()).isEqualTo(nv.getVariableType());
assertThat(nv.getVariableType().toString())
.isEqualTo("KotlinTypeGoatKt{name=field,type=kotlin.Int}");
}

@Test
void fileFunction() {
J.MethodDeclaration md = cu.getStatements().stream()
.filter(it -> it instanceof J.MethodDeclaration)
.map(J.MethodDeclaration.class::cast)
.filter(it -> "function".equals(it.getSimpleName())).findFirst().orElseThrow();

assertThat(md.getName().getType()).isEqualTo(md.getMethodType());
assertThat(md.getMethodType().toString())
.isEqualTo("KotlinTypeGoatKt{name=function,return=kotlin.Unit,parameters=[]}");
}

@Test
void kotlinAnyHasNoSuperType() {
assertThat(goatType.getSupertype().getSupertype()).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class KotlinTypeSignatureBuilderTest {
.moduleName("test")
.build()
.parse(singletonList(new Parser.Input(Paths.get("KotlinTypeGoat.kt"), () -> new ByteArrayInputStream(goat.getBytes(StandardCharsets.UTF_8)))), disposable,
new ParsingExecutionContextView(new InMemoryExecutionContext(Throwable::printStackTrace)));;
new ParsingExecutionContextView(new InMemoryExecutionContext(Throwable::printStackTrace)));

@AfterAll
static void afterAll() {
Expand Down

0 comments on commit 9420545

Please sign in to comment.