Skip to content

Commit

Permalink
Add unit tests for KotlinNullabilityDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
drubanovich-soti committed Jan 27, 2025
1 parent 4810fc3 commit 44e102b
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
7 changes: 7 additions & 0 deletions rhino-kotlin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
plugins {
id 'rhino.library-conventions'
id 'org.jetbrains.kotlin.jvm'
}

dependencies {
implementation project(':rhino')
implementation "org.jetbrains.kotlin:kotlin-metadata-jvm:2.1.0"

testImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.1.0"
}

kotlin {
jvmToolchain(11)
}

publishing {
Expand Down
15 changes: 15 additions & 0 deletions rhino-kotlin/src/test/java/org/mozilla/kotlin/JavaClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mozilla.kotlin;

public class JavaClass {
private final String property1;
private final String property2;

public JavaClass(String param1, String param2) {
property1 = param1;
property2 = param2;
}

public void function(Integer param1, Long param2) {
// Do nothing
}
}
14 changes: 14 additions & 0 deletions rhino-kotlin/src/test/java/org/mozilla/kotlin/KotlinClass.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mozilla.kotlin

class KotlinClass(
val nonNullProperty: String,
val nullableProperty: String?
) {
fun function(nullableParam: Int?, nonNullParam: Int, anotherNullableParam: KotlinClass?) {
// Do nothing
}

fun function(nonNullParam: Int, anotherNonNullParam: Int) {
// Do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.mozilla.kotlin

class KotlinClassWithOverloadedFunction {
fun function(nullableParam: Int?, nonNullParam: Int, anotherNullableParam: KotlinClass?) {
// Do nothing
}

fun function(nullableParam: Int?, nonNullParam: Int, anotherNonNullParam: String) {
// Do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.mozilla.kotlin;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;

public class KotlinNullabilityDetectorTest {
private final KotlinNullabilityDetector detector = new KotlinNullabilityDetector();

@Test
public void testKotlinFunction() {
boolean[] nullability =
detector.getParameterNullability(findMethod(KotlinClass.class, "function", 3));

boolean[] expectedNullability = new boolean[] {true, false, true};
assertThat(nullability, is(expectedNullability));
}

@Test
public void testKotlinConstructor() {
boolean[] nullability =
detector.getParameterNullability(findConstructor(KotlinClass.class, 2));

boolean[] expectedNullability = new boolean[] {false, true};
assertThat(nullability, is(expectedNullability));
}

@Test
public void testJavaFunction() {
boolean[] nullability =
detector.getParameterNullability(findMethod(JavaClass.class, "function", 2));

boolean[] expectedNullability = new boolean[] {false, false};
assertThat(nullability, is(expectedNullability));
}

@Test
public void testJavaConstructor() {
boolean[] nullability =
detector.getParameterNullability(findConstructor(JavaClass.class, 2));

boolean[] expectedNullability = new boolean[] {false, false};
assertThat(nullability, is(expectedNullability));
}

@Test
public void testKotlinOverloadedFunction() {
List<Method> overloadedMethods =
findMethods(KotlinClassWithOverloadedFunction.class, "function");

assertThat(overloadedMethods.size(), is(2));
// Since we cannot distinguish overloads with same number of params, we have to fallback to
// no-op
boolean[] expectedNullability = new boolean[] {false, false, false};
overloadedMethods.forEach(
method ->
assertThat(
detector.getParameterNullability(method), is(expectedNullability)));
}

private Method findMethod(Class<?> clazz, String methodName, int paramCount) {
return Arrays.stream(clazz.getMethods())
.filter(
method ->
method.getName().equals(methodName)
&& method.getParameterTypes().length == paramCount)
.findFirst()
.orElse(null);
}

private Constructor<?> findConstructor(Class<?> clazz, int paramCount) {
return Arrays.stream(clazz.getConstructors())
.filter(constructor -> constructor.getParameterTypes().length == paramCount)
.findFirst()
.orElse(null);
}

private List<Method> findMethods(Class<?> clazz, String methodName) {
return Arrays.stream(clazz.getMethods())
.filter(method -> method.getName().equals(methodName))
.collect(Collectors.toList());
}
}
4 changes: 4 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
plugins {
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0'
id 'org.jetbrains.kotlin.jvm' version '2.1.0' apply false
}
rootProject.name = 'rhino-root'
include 'rhino', 'rhino-engine', 'rhino-tools', 'rhino-xml', 'rhino-all', 'examples', 'testutils', 'tests', 'benchmarks'
include 'rhino-kotlin'

0 comments on commit 44e102b

Please sign in to comment.