kosi-libs / MocKMP

A mocking processor for Kotlin Multiplatform
https://kosi-libs.org/mockmp
MIT License
189 stars 12 forks source link

Invalid mock generation for suspending functions with default implementation #32

Closed xpathexception closed 2 years ago

xpathexception commented 2 years ago

Tested with:

mockmp 1.6.0, 1.8.1 kotlin 1.6.20, 1.7.10

For interfaces, where suspending functions have default implementations, @Mock generates invalid mock implementation.

interface Sample {
    suspend fun sampleSuspending(): String = throw NotImplementedError()
}

Generated code (formatted for readability):

import org.kodein.mock.Mocker

internal class MockSample(
    private val mocker: Mocker,
) : Sample {
    public override suspend fun sampleSuspending(): String {
        return this.mocker.registerSuspend(
            receiver = this,
            method = "sampleSuspending()",
            default = { super.sampleSuspending() } // <=== Suspension functions can be called only within coroutine body
        )
    }

    public override fun toString(): String {
        return this.mocker.register(
            receiver = this,
            method = "toString()",
            default = { super.toString() }
        )
    }
}
SalomonBrys commented 2 years ago

Version 1.9.0 published with the fix.