Skip to content

Commit

Permalink
Fixed tests, added more tests and simplified OrNull
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsSven committed Jul 5, 2024
1 parent b039fb1 commit 348296b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 0 additions & 3 deletions mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ inline fun <reified T : Any> any(): T {

/** Matches anything, including nulls. */
inline fun <reified T : Any> anyOrNull(): T {
if(T::class.isValue)
return anyValueClass()

return ArgumentMatchers.any<T>() ?: createInstance()
}

Expand Down
6 changes: 5 additions & 1 deletion tests/src/test/kotlin/test/Classes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ interface Methods {

fun nonDefaultReturnType(): ExtraInterface

fun valueClass(v: ValueClass)
fun valueClass(v: ValueClass?)
fun nestedValueClass(v: NestedValueClass)
}

@JvmInline
value class ValueClass(private val content: String)

@JvmInline
value class NestedValueClass(val value: ValueClass)

interface ExtraInterface

abstract class ThrowingConstructor {
Expand Down
27 changes: 26 additions & 1 deletion tests/src/test/kotlin/test/MatchersTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,22 @@ class MatchersTest : TestBase() {
}
}

@Test
fun anyOrNull_forValueClass() {
mock<Methods>().apply {
valueClass(ValueClass("Content"))
verify(this).valueClass(anyOrNull())
}
}

@Test
fun anyOrNull_forValueClass_withNull() {
mock<Methods>().apply {
valueClass(null)
verify(this).valueClass(anyOrNull())
}
}

@Test
fun anyValueClass_withValueClass() {
mock<Methods>().apply {
Expand All @@ -340,11 +356,20 @@ class MatchersTest : TestBase() {
expectErrorWithMessage("kotlin.Float is not a value class.") on {
mock<Methods>().apply {
float(10f)
verify(this).float(anyValueClass())
// Should throw an error because Float is not a value class
float(anyValueClass())
}
}
}

@Test
fun anyValueClass_withNestedValueClass() {
mock<Methods>().apply {
nestedValueClass(NestedValueClass(ValueClass("Content")))
verify(this).nestedValueClass(anyValueClass())
}
}

/**
* a VarargMatcher implementation for varargs of type [T] that will answer with type [R] if any of the var args
* matched. Needs to keep state between matching invocations.
Expand Down

0 comments on commit 348296b

Please sign in to comment.