Skip to content

Commit

Permalink
Fix( Issue #1073): Bug fix for the issue with mocking value classes w…
Browse files Browse the repository at this point in the history
…ith coEvery.

Enable few disabled tests for support nested value classes (issue: 859)
  • Loading branch information
Kishore Padhi committed Jan 3, 2025
1 parent 1b3b01e commit 2ddd49d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import kotlin.reflect.jvm.kotlinFunction

actual object ValueClassSupport {

private val unboxValueReturnTypes = setOf("class kotlin.Result")

/**
* Unboxes the underlying property value of a **`value class`** or self, as long the unboxed value is appropriate
* for the given method's return type.
Expand All @@ -34,11 +36,18 @@ actual object ValueClassSupport {
val isReturnNullable = kFunction.returnType.isMarkedNullable
val isPrimitive = resultType.innermostBoxedClass.java.isPrimitive
return if (
!(kFunction.isSuspend && isPrimitive) &&
!kFunction.isSuspend &&
resultType == expectedReturnType &&
!(isReturnNullable && isPrimitive)
) {
this.boxedValue
} else if (
(kFunction.isSuspend
&& !(isReturnNullable || isPrimitive))
&& (this.javaClass.toString() == expectedReturnType.toString()
&& !unboxValueReturnTypes.contains(expectedReturnType.toString()))
) {
this.boxedValue
} else {
this
}
Expand All @@ -54,8 +63,10 @@ actual object ValueClassSupport {
val isPrimitive = resultType.innermostBoxedClass.java.isPrimitive
return if (resultType == expectedReturnType && !(isReturnNullable && isPrimitive)) {
this.boxedValue
} else if (!(isReturnNullable && isPrimitive)) {
this.boxedValue
} else {
this.boxedValue ?: this
this
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions modules/mockk/src/commonTest/kotlin/io/mockk/it/ValueClassTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.assertTimeoutPreemptively
import java.time.Duration
import java.util.UUID
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ValueClassTest {

Expand All @@ -24,6 +27,29 @@ class ValueClassTest {
private val dummyValueClassReturn get() = DummyValue(202)
private val dummyComplexValueClassReturn get() = ComplexValue(UUID.fromString("25581db2-4cdb-48cd-a6c9-e087aee31f0b"))

private interface Action<Params, ReturnType> {
suspend fun execute(params: Params): ReturnType
}

private class ResultTest : Action<Unit, Result<String>> {
override suspend fun execute(params: Unit): Result<String> {
return Result.success("some result")
}
}

@Nested
inner class TestWithCoEvery {
private val resultTest = mockk<ResultTest> {
coEvery { execute(Unit) } coAnswers { Result.success("abc") }
}

@Test
fun `given a test when mocking value classes with coEvery Result returns then success`() = runTest {
val result = resultTest.execute(Unit)
assertTrue(result.isSuccess)
}
}

//<editor-fold desc="arg=Value Class, return=ValueClass">
@Test
fun `arg is ValueClass, returns ValueClass`() {
Expand Down Expand Up @@ -303,7 +329,6 @@ class ValueClassTest {
}

@Test
@Ignore // TODO support nested value classes https://github.com/mockk/mockk/issues/859
fun `arg is any(Wrapper), returns ValueClass`() {
val mock = mockk<DummyService> {
every { argWrapperReturnValueClass(any()) } returns dummyValueClassReturn
Expand Down Expand Up @@ -343,7 +368,6 @@ class ValueClassTest {
}

@Test
@Ignore // TODO support nested value classes https://github.com/mockk/mockk/issues/859
fun `arg is any(Wrapper), answers ValueClass`() {
val mock = mockk<DummyService> {
every { argWrapperReturnValueClass(any()) } answers { dummyValueClassReturn }
Expand Down Expand Up @@ -386,7 +410,6 @@ class ValueClassTest {
}

@Test
@Ignore // TODO support nested value classes https://github.com/mockk/mockk/issues/859
fun `arg is any(Wrapper), returns Wrapper`() {
val mock = mockk<DummyService> {
every { argWrapperReturnWrapper(any()) } returns dummyValueWrapperReturn
Expand Down Expand Up @@ -426,7 +449,6 @@ class ValueClassTest {
}

@Test
@Ignore // TODO support nested value classes https://github.com/mockk/mockk/issues/859
fun `arg is any(Wrapper), answers Wrapper`() {
val mock = mockk<DummyService> {
every { argWrapperReturnWrapper(any()) } answers { dummyValueWrapperReturn }
Expand Down

0 comments on commit 2ddd49d

Please sign in to comment.