From 348296b2a2cb7b15e8b41c4441e69953b0bc735d Mon Sep 17 00:00:00 2001 From: "l.s.andringa1" Date: Sat, 6 Jul 2024 00:50:54 +0200 Subject: [PATCH] Fixed tests, added more tests and simplified OrNull --- .../kotlin/org/mockito/kotlin/Matchers.kt | 3 --- tests/src/test/kotlin/test/Classes.kt | 6 ++++- tests/src/test/kotlin/test/MatchersTest.kt | 27 ++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt index b2d4611..48bdb0c 100644 --- a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt +++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/Matchers.kt @@ -50,9 +50,6 @@ inline fun any(): T { /** Matches anything, including nulls. */ inline fun anyOrNull(): T { - if(T::class.isValue) - return anyValueClass() - return ArgumentMatchers.any() ?: createInstance() } diff --git a/tests/src/test/kotlin/test/Classes.kt b/tests/src/test/kotlin/test/Classes.kt index fdfad57..6d78a05 100644 --- a/tests/src/test/kotlin/test/Classes.kt +++ b/tests/src/test/kotlin/test/Classes.kt @@ -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 { diff --git a/tests/src/test/kotlin/test/MatchersTest.kt b/tests/src/test/kotlin/test/MatchersTest.kt index 344605c..10aeffc 100644 --- a/tests/src/test/kotlin/test/MatchersTest.kt +++ b/tests/src/test/kotlin/test/MatchersTest.kt @@ -327,6 +327,22 @@ class MatchersTest : TestBase() { } } + @Test + fun anyOrNull_forValueClass() { + mock().apply { + valueClass(ValueClass("Content")) + verify(this).valueClass(anyOrNull()) + } + } + + @Test + fun anyOrNull_forValueClass_withNull() { + mock().apply { + valueClass(null) + verify(this).valueClass(anyOrNull()) + } + } + @Test fun anyValueClass_withValueClass() { mock().apply { @@ -340,11 +356,20 @@ class MatchersTest : TestBase() { expectErrorWithMessage("kotlin.Float is not a value class.") on { mock().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().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.