-
Notifications
You must be signed in to change notification settings - Fork 863
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for KotlinNullabilityDetector
- Loading branch information
1 parent
4810fc3
commit 44e102b
Showing
6 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
rhino-kotlin/src/test/java/org/mozilla/kotlin/JavaClass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
rhino-kotlin/src/test/java/org/mozilla/kotlin/KotlinClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
rhino-kotlin/src/test/java/org/mozilla/kotlin/KotlinClassWithOverloadedFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
rhino-kotlin/src/test/java/org/mozilla/kotlin/KotlinNullabilityDetectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |