|
| 1 | +@file:OptIn(ExperimentalForeignApi::class) |
| 2 | + |
| 3 | +package co.touchlab.crashkios.crashlytics |
| 4 | + |
| 5 | +import co.touchlab.crashkios.crashlytics.objc.CrashKiOSCrashlyticsSinkProtocol |
| 6 | +import kotlin.test.Test |
| 7 | +import kotlin.test.assertEquals |
| 8 | +import kotlin.test.assertTrue |
| 9 | +import kotlinx.cinterop.ExperimentalForeignApi |
| 10 | +import platform.Foundation.NSException |
| 11 | +import platform.darwin.NSObject |
| 12 | + |
| 13 | +// Kotlin stand-in for the Swift sink: exercises the same ObjC-protocol boundary and |
| 14 | +// proves the module links with zero linker flags (the old -U / RND-91 failure mode). |
| 15 | +private class FakeCrashlyticsSink : |
| 16 | + NSObject(), |
| 17 | + CrashKiOSCrashlyticsSinkProtocol { |
| 18 | + val logs = mutableListOf<String>() |
| 19 | + val handled = mutableListOf<Triple<String, String, List<*>>>() |
| 20 | + val fatals = mutableListOf<NSException>() |
| 21 | + val custom = mutableMapOf<String, Any?>() |
| 22 | + var userId: String? = null |
| 23 | + var collectionEnabled: Boolean? = null |
| 24 | + |
| 25 | + override fun logMessage(message: String) { |
| 26 | + logs += message |
| 27 | + } |
| 28 | + |
| 29 | + override fun recordHandledExceptionWithName(name: String, reason: String, stackAddresses: List<*>) { |
| 30 | + handled += Triple(name, reason, stackAddresses) |
| 31 | + } |
| 32 | + |
| 33 | + override fun recordFatalException(exception: NSException) { |
| 34 | + fatals += exception |
| 35 | + } |
| 36 | + |
| 37 | + override fun setCustomValue(value: Any?, forKey: String) { |
| 38 | + custom[forKey] = value |
| 39 | + } |
| 40 | + |
| 41 | + override fun setUserId(identifier: String) { |
| 42 | + userId = identifier |
| 43 | + } |
| 44 | + |
| 45 | + override fun setCollectionEnabled(enabled: Boolean) { |
| 46 | + collectionEnabled = enabled |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +class CrashlyticsSinkTest { |
| 51 | + @Test |
| 52 | + fun facadeForwardsToRegisteredSink() { |
| 53 | + val sink = FakeCrashlyticsSink() |
| 54 | + registerCrashlyticsSink(sink) |
| 55 | + |
| 56 | + CrashlyticsKotlin.logMessage("hello") |
| 57 | + CrashlyticsKotlin.setCustomValue("answer", "42") |
| 58 | + CrashlyticsKotlin.setUserId("user1") |
| 59 | + CrashlyticsKotlin.setCollectionEnabled(true) |
| 60 | + CrashlyticsKotlin.sendHandledException(RuntimeException("boom")) |
| 61 | + CrashlyticsKotlin.sendFatalException(RuntimeException("fatal", IllegalStateException("root cause"))) |
| 62 | + |
| 63 | + assertEquals(listOf("hello"), sink.logs) |
| 64 | + assertEquals("42", sink.custom["answer"]?.toString()) |
| 65 | + assertEquals("user1", sink.userId) |
| 66 | + assertEquals(true, sink.collectionEnabled) |
| 67 | + |
| 68 | + val (name, reason, addresses) = sink.handled.single() |
| 69 | + assertEquals("kotlin.RuntimeException", name) |
| 70 | + assertEquals("boom", reason) |
| 71 | + assertTrue(addresses.isNotEmpty(), "handled exception should carry stack addresses") |
| 72 | + |
| 73 | + val fatal = sink.fatals.single() |
| 74 | + assertEquals("kotlin.RuntimeException", fatal.name) |
| 75 | + assertTrue(fatal.reason!!.contains("fatal")) |
| 76 | + assertTrue(fatal.reason!!.contains("Caused by: kotlin.IllegalStateException: root cause")) |
| 77 | + assertTrue(fatal.callStackReturnAddresses.isNotEmpty(), "fatal exception should carry stack addresses") |
| 78 | + } |
| 79 | +} |
0 commit comments