-
-
Notifications
You must be signed in to change notification settings - Fork 475
Expand file tree
/
Copy pathCompileOnlyCompatTest.kt
More file actions
79 lines (69 loc) · 2.2 KB
/
Copy pathCompileOnlyCompatTest.kt
File metadata and controls
79 lines (69 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package io.sentry.util
import io.sentry.util.CompileOnlyCompat.CompileOnlyCall
import io.sentry.util.CompileOnlyCompat.Fallback
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class CompileOnlyCompatTest {
@Test
fun `ifAbsent returns call result when method exists`() {
val result = CompileOnlyCall { "hello" }.ifAbsent("fallback")
assertEquals("hello", result)
}
@Test
fun `ifAbsent returns constant fallback on LinkageError`() {
val result = CompileOnlyCall<String> { throw NoSuchMethodError() }.ifAbsent("fallback")
assertEquals("fallback", result)
}
@Test
fun `ifAbsent does not catch non-LinkageErrors`() {
assertFailsWith<IllegalStateException> {
CompileOnlyCall<String> { throw IllegalStateException() }.ifAbsent("fallback")
}
}
@Test
fun `ifAbsent with Fallback returns call result when method exists`() {
val result = CompileOnlyCall { "hello" }.ifAbsent(Fallback { _ -> "fallback" })
assertEquals("hello", result)
}
@Test
fun `ifAbsent with Fallback invokes fallback on LinkageError`() {
var fallbackInvoked = false
val result =
CompileOnlyCall<String> { throw NoSuchMethodError() }
.ifAbsent { _ ->
fallbackInvoked = true
"fallback"
}
assertEquals("fallback", result)
assertTrue(fallbackInvoked)
}
@Test
fun `ifAbsent with Fallback passes the LinkageError`() {
var captured: LinkageError? = null
CompileOnlyCall<String> { throw NoSuchMethodError("test") }
.ifAbsent { error ->
captured = error
"fallback"
}
assertTrue(captured is NoSuchMethodError)
assertEquals("test", captured!!.message)
}
@Test
fun `ifAbsent with Fallback does not invoke fallback on success`() {
var fallbackInvoked = false
CompileOnlyCall { "hello" }
.ifAbsent { _ ->
fallbackInvoked = true
"fallback"
}
assertTrue(!fallbackInvoked)
}
@Test
fun `ifAbsent with Fallback does not catch non-LinkageErrors`() {
assertFailsWith<IllegalStateException> {
CompileOnlyCall<String> { throw IllegalStateException() }.ifAbsent { _ -> "fallback" }
}
}
}