forked from badoo/Reaktive
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
767 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
reaktive/src/commonTest/kotlin/com/badoo/reaktive/test/TestErrorCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.badoo.reaktive.test | ||
|
||
import com.badoo.reaktive.base.ErrorCallback | ||
|
||
class TestErrorCallback( | ||
private val onError: (Throwable) -> Unit = {}, | ||
) : ErrorCallback { | ||
|
||
var error: Throwable? = null | ||
|
||
override fun onError(error: Throwable) { | ||
onError.invoke(error) | ||
this.error = error | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
reaktive/src/commonTest/kotlin/com/badoo/reaktive/utils/TryCatchAndHandleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.badoo.reaktive.utils | ||
|
||
import com.badoo.reaktive.base.exceptions.CompositeException | ||
import com.badoo.reaktive.base.tryCatchAndHandle | ||
import com.badoo.reaktive.test.mockUncaughtExceptionHandler | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertSame | ||
|
||
class TryCatchAndHandleTest { | ||
|
||
@AfterTest | ||
fun after() { | ||
resetReaktiveUncaughtErrorHandler() | ||
} | ||
|
||
@Test | ||
fun calls_reaktiveUncaughtErrorHandler_WHEN_block_thrown_exception() { | ||
val caughtException = mockUncaughtExceptionHandler() | ||
val error = Exception() | ||
|
||
tryCatchAndHandle { throw error } | ||
|
||
assertSame(error, caughtException.value) | ||
} | ||
|
||
@Test | ||
fun calls_reaktiveUncaughtErrorHandler_WHEN_errorTransformer_thrown_exception() { | ||
val caughtException = mockUncaughtExceptionHandler() | ||
val error1 = Exception() | ||
val error2 = Exception() | ||
|
||
tryCatchAndHandle( | ||
errorTransformer = { throw error2 }, | ||
block = { throw error1 }, | ||
) | ||
|
||
val error = caughtException.value | ||
assertIs<CompositeException>(error) | ||
assertSame(error1, error.cause1) | ||
assertSame(error2, error.cause2) | ||
} | ||
|
||
@Test | ||
fun swallows_WHEN_reaktiveUncaughtErrorHandler_thrown_exception() { | ||
reaktiveUncaughtErrorHandler = { throw Exception() } | ||
tryCatchAndHandle { throw Exception() } | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
reaktive/src/commonTest/kotlin/com/badoo/reaktive/utils/TryCatchTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.badoo.reaktive.utils | ||
|
||
import com.badoo.reaktive.base.exceptions.CompositeException | ||
import com.badoo.reaktive.base.tryCatch | ||
import com.badoo.reaktive.test.TestErrorCallback | ||
import com.badoo.reaktive.test.mockUncaughtExceptionHandler | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertSame | ||
|
||
class TryCatchTest { | ||
|
||
@AfterTest | ||
fun after() { | ||
resetReaktiveUncaughtErrorHandler() | ||
} | ||
|
||
@Test | ||
fun calls_ErrorCallback_WHEN_block_thrown_exception() { | ||
val errorCallback = TestErrorCallback() | ||
val error = Exception() | ||
|
||
errorCallback.tryCatch { throw error } | ||
|
||
assertSame(error, errorCallback.error) | ||
} | ||
|
||
@Test | ||
fun calls_ErrorCallback_WHEN_errorTransformer_thrown_exception() { | ||
val errorCallback = TestErrorCallback() | ||
val error1 = Exception() | ||
val error2 = Exception() | ||
|
||
errorCallback.tryCatch( | ||
errorTransformer = { throw error2 }, | ||
block = { throw error1 }, | ||
) | ||
|
||
val error = errorCallback.error | ||
assertIs<CompositeException>(error) | ||
assertSame(error1, error.cause1) | ||
assertSame(error2, error.cause2) | ||
} | ||
|
||
@Test | ||
fun calls_reaktiveUncaughtErrorHandler_WHEN_ErrorCallback_thrown_exception() { | ||
val caughtException = mockUncaughtExceptionHandler() | ||
val error1 = Exception() | ||
val error2 = Exception() | ||
|
||
val errorCallback = TestErrorCallback { throw error2 } | ||
errorCallback.tryCatch { throw error1 } | ||
|
||
val error = caughtException.value | ||
assertIs<CompositeException>(error) | ||
assertSame(error1, error.cause1) | ||
assertSame(error2, error.cause2) | ||
} | ||
|
||
@Test | ||
fun swallows_WHEN_reaktiveUncaughtErrorHandler_thrown_exception() { | ||
reaktiveUncaughtErrorHandler = { throw Exception() } | ||
val errorCallback = TestErrorCallback { throw Exception() } | ||
errorCallback.tryCatch { throw Exception() } | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
reaktive/src/commonTest/kotlin/com/badoo/reaktive/utils/TryCatchWithOnSuccessTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.badoo.reaktive.utils | ||
|
||
import com.badoo.reaktive.base.exceptions.CompositeException | ||
import com.badoo.reaktive.base.tryCatch | ||
import com.badoo.reaktive.test.TestErrorCallback | ||
import com.badoo.reaktive.test.mockUncaughtExceptionHandler | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertIs | ||
import kotlin.test.assertSame | ||
|
||
class TryCatchWithOnSuccessTest { | ||
|
||
@AfterTest | ||
fun after() { | ||
resetReaktiveUncaughtErrorHandler() | ||
} | ||
|
||
@Test | ||
fun calls_onSuccess_WHEN_block_not_thrown() { | ||
val errorCallback = TestErrorCallback() | ||
var result: Int? = null | ||
|
||
errorCallback.tryCatch(block = { 0 }, onSuccess = { result = 0 }) | ||
|
||
assertEquals(0, result) | ||
} | ||
|
||
@Test | ||
fun calls_ErrorCallback_WHEN_block_thrown_exception() { | ||
val errorCallback = TestErrorCallback() | ||
val error = Exception() | ||
|
||
errorCallback.tryCatch(block = { throw error }, onSuccess = {}) | ||
|
||
assertSame(error, errorCallback.error) | ||
} | ||
|
||
@Test | ||
fun calls_ErrorCallback_WHEN_errorTransformer_thrown_exception() { | ||
val errorCallback = TestErrorCallback() | ||
val error1 = Exception() | ||
val error2 = Exception() | ||
|
||
errorCallback.tryCatch( | ||
block = { throw error1 }, | ||
errorTransformer = { throw error2 }, | ||
onSuccess = {}, | ||
) | ||
|
||
val error = errorCallback.error | ||
assertIs<CompositeException>(error) | ||
assertSame(error1, error.cause1) | ||
assertSame(error2, error.cause2) | ||
} | ||
|
||
@Test | ||
fun calls_reaktiveUncaughtErrorHandler_WHEN_ErrorCallback_thrown_exception() { | ||
val caughtException = mockUncaughtExceptionHandler() | ||
val error1 = Exception() | ||
val error2 = Exception() | ||
|
||
val errorCallback = TestErrorCallback { throw error2 } | ||
errorCallback.tryCatch(block = { throw error1 }, onSuccess = {}) | ||
|
||
val error = caughtException.value | ||
assertIs<CompositeException>(error) | ||
assertSame(error1, error.cause1) | ||
assertSame(error2, error.cause2) | ||
} | ||
|
||
@Test | ||
fun swallows_WHEN_reaktiveUncaughtErrorHandler_thrown_exception() { | ||
reaktiveUncaughtErrorHandler = { throw Exception() } | ||
val errorCallback = TestErrorCallback { throw Exception() } | ||
errorCallback.tryCatch(block = { throw Exception() }, onSuccess = {}) | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
reaktive/src/jsCommonMain/kotlin/com/badoo/reaktive/utils/HandleSourceError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.badoo.reaktive.utils | ||
|
||
actual fun Throwable.isFatal(): Boolean = false |
6 changes: 6 additions & 0 deletions
6
reaktive/src/jvmCommonMain/kotlin/com/badoo/reaktive/utils/HandleSourceError.jvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.badoo.reaktive.utils | ||
|
||
internal actual fun Throwable.isFatal(): Boolean = | ||
(this is VirtualMachineError) || | ||
(this is ThreadDeath) || | ||
(this is LinkageError) |
Oops, something went wrong.