icerockdev / moko-errors

Automated exceptions handler for mobile (android & ios) Kotlin Multiplatform development.
https://moko.icerock.dev/
Apache License 2.0
52 stars 6 forks source link

Add errors-test module with test utilities #14

Open Alex009 opened 3 years ago

Alex009 commented 3 years ago

When i try to test ViewModel that use ExceptionHandler i need to create test version of ExceptionHandler. here draft:

import dev.icerock.moko.errors.handler.ExceptionHandler
import dev.icerock.moko.errors.handler.ExceptionMapper

expect class TestsExceptionHandler<T : Any>(
    exceptionMapper: ExceptionMapper<T>
) : ExceptionHandler {
    val catchedExceptions: List<Throwable>
}

android:

import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import dev.icerock.moko.errors.handler.ExceptionHandler
import dev.icerock.moko.errors.handler.ExceptionHandlerContext
import dev.icerock.moko.errors.handler.ExceptionMapper
import dev.icerock.moko.mvvm.dispatcher.EventsDispatcher

actual class TestsExceptionHandler<T : Any> actual constructor(
    private val exceptionMapper: ExceptionMapper<T>
) : ExceptionHandler {

    private val _catchedExceptions = mutableListOf<Throwable>()
    actual val catchedExceptions: List<Throwable> = _catchedExceptions

    override fun bind(lifecycleOwner: LifecycleOwner, activity: FragmentActivity) {
        TODO("Not yet implemented")
    }

    override fun <R> handle(block: suspend () -> R): ExceptionHandlerContext<R> {
        return ExceptionHandlerContext.invoke(
            exceptionMapper = exceptionMapper,
            eventsDispatcher = EventsDispatcher(),
            onCatch = _catchedExceptions::add,
            block = block
        )
    }
}

ios:

import dev.icerock.moko.errors.handler.ExceptionHandler
import dev.icerock.moko.errors.handler.ExceptionHandlerContext
import dev.icerock.moko.errors.handler.ExceptionMapper
import dev.icerock.moko.mvvm.dispatcher.EventsDispatcher
import platform.UIKit.UIViewController

actual class TestsExceptionHandler<T : Any> actual constructor(
    private val exceptionMapper: ExceptionMapper<T>
) : ExceptionHandler {
    private val _catchedExceptions = mutableListOf<Throwable>()
    actual val catchedExceptions: List<Throwable> = _catchedExceptions

    override fun <R> handle(block: suspend () -> R): ExceptionHandlerContext<R> {
        return ExceptionHandlerContext.invoke(
            exceptionMapper = exceptionMapper,
            eventsDispatcher = EventsDispatcher(),
            onCatch = _catchedExceptions::add,
            block = block
        )
    }

    override fun bind(viewController: UIViewController) {
        TODO("Not yet implemented")
    }
}