Skip to content

Commit

Permalink
move NativeError.kotlinCause from main to test
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushg committed Feb 18, 2022
1 parent c2ac88a commit 7de411f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ internal actual fun Throwable.asNativeError(): NativeError {
}
return NSError.errorWithDomain("KotlinException", 0.convert(), userInfo)
}

internal actual val NativeError.kotlinCause
get() = this.userInfo["KotlinException"] as? Throwable
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ package com.rickclephas.kmp.nativecoroutines

import kotlinx.cinterop.UnsafeNumber
import kotlinx.cinterop.convert
import kotlin.native.concurrent.isFrozen
import kotlin.test.*

class NSErrorTests {

@Test
fun `ensure frozen`() {
val exception = RandomException()
assertFalse(exception.isFrozen, "Exception shouldn't be frozen yet")
val nsError = exception.asNativeError()
assertTrue(nsError.isFrozen, "NSError should be frozen")
assertTrue(exception.isFrozen, "Exception should be frozen")
}
internal actual val NativeError.kotlinCause
get() = this.userInfo["KotlinException"] as? Throwable

class NSErrorTests {
@Test
@OptIn(UnsafeNumber::class)
fun `ensure NSError domain and code are correct`() {
Expand All @@ -32,11 +24,4 @@ class NSErrorTests {
assertEquals(exception.message, nsError.localizedDescription,
"Localized description isn't set to message")
}

@Test
fun `ensure exception is part of user info`() {
val exception = RandomException()
val nsError = exception.asNativeError()
assertSame(exception, nsError.kotlinCause, "Exception isn't part of the user info")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ expect class NativeError
* Converts a [Throwable] to a [NativeError].
*/
internal expect fun Throwable.asNativeError(): NativeError

internal expect val NativeError.kotlinCause: Throwable?
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rickclephas.kmp.nativecoroutines

import kotlin.native.concurrent.isFrozen
import kotlin.test.*

/**
* Get the [Throwable] that is represented by the given [NativeError]
*/
internal expect val NativeError.kotlinCause: Throwable?

class NativeErrorTests {
@Test
fun `ensure frozen`() {
val exception = RandomException()
assertFalse(exception.isFrozen, "Exception shouldn't be frozen yet")
val nsError = exception.asNativeError()
assertTrue(nsError.isFrozen, "NSError should be frozen")
assertTrue(exception.isFrozen, "Exception should be frozen")
}

@Test
fun `ensure exception is part of user info`() {
val exception = RandomException()
val nsError = exception.asNativeError()
assertSame(exception, nsError.kotlinCause, "Exception isn't part of the NativeError")
}
}

0 comments on commit 7de411f

Please sign in to comment.