@@ -12,7 +12,7 @@ inline fun throwIf(throwCondition: Boolean, exProvider: () -> Exception) {
12
12
}
13
13
14
14
inline fun <T : Any ?> T.mustHave (
15
- otherwiseThrow : (T ) -> Exception = { IllegalStateException ("mustHave: check failed") },
15
+ otherwiseThrow : (T ) -> Exception = { IllegalStateException ("mustHave check failed: $it ") },
16
16
require : (T ) -> Boolean
17
17
): T {
18
18
if (! require(this )) throw otherwiseThrow(this )
@@ -29,7 +29,8 @@ class ConditionalThrowingUnitTest {
29
29
val str = " a b c"
30
30
assertThrows<IllegalArgumentException > {
31
31
require(str.length > 10 ) { " The string is too short." }
32
- }.also { ex -> assertEquals(" The string is too short." , ex.message) }
32
+ null .toString()
33
+ }
33
34
34
35
val upperStr = str.also {
35
36
require(it.split(" " ).size == 3 ) { " Format not supported" }
@@ -40,7 +41,7 @@ class ConditionalThrowingUnitTest {
40
41
var nullableValue: String? = null
41
42
assertThrows<IllegalArgumentException > {
42
43
requireNotNull(nullableValue) { " Null is not allowed" }
43
- }. also { ex -> assertEquals( " Null is not allowed " , ex.message) }
44
+ }
44
45
45
46
nullableValue = " a b c"
46
47
val uppercaseValue = requireNotNull(nullableValue).uppercase()
@@ -52,12 +53,12 @@ class ConditionalThrowingUnitTest {
52
53
val str = " a b c"
53
54
assertThrows<IllegalStateException > {
54
55
check(str.length > 10 ) { " The string is too short." }
55
- }. also { ex -> assertEquals( " The string is too short. " , ex.message) }
56
+ }
56
57
57
58
var nullableValue: String? = null
58
59
assertThrows<IllegalStateException > {
59
60
checkNotNull(nullableValue) { " Null is not allowed" }
60
- }. also { ex -> assertEquals( " Null is not allowed " , ex.message) }
61
+ }
61
62
62
63
nullableValue = " a b c"
63
64
val uppercaseValue = checkNotNull(nullableValue).uppercase()
@@ -70,21 +71,21 @@ class ConditionalThrowingUnitTest {
70
71
val str = " a b c"
71
72
assertThrows<MyException > {
72
73
str.takeIf { it.length > 10 } ? : throw MyException (" The string is too short." )
73
- }. also { ex -> assertEquals( " The string is too short. " , ex.message) }
74
+ }
74
75
75
76
val nullIsValid: String? = null
76
77
// we don't expect the exception
77
78
assertThrows<MyException > {
78
- nullIsValid.takeIf { it == null || it.length > 10 } ? : throw MyException (" The string is too short." )
79
- }. also { ex -> assertEquals( " The string is too short. " , ex.message) }
79
+ nullIsValid.takeIf { true } ? : throw MyException (" The string is too short." )
80
+ }
80
81
}
81
82
82
83
@Test
83
84
fun `when using throwIf() then get expected results` () {
84
85
val str = " a b c"
85
86
assertThrows<MyException > {
86
87
throwIf(str.length <= 10 ) { MyException (" The string is too short." ) }
87
- }. also { ex -> assertEquals( " The string is too short. " , ex.message) }
88
+ }
88
89
89
90
val uppercaseValue = str.also {
90
91
throwIf(it.split(" " ).size != 3 ) { MyException (" Format not supported" ) }
@@ -97,7 +98,7 @@ class ConditionalThrowingUnitTest {
97
98
fun `when using mustHave() then get expected results` () {
98
99
val kai = Player (1 , " Kai" , - 5 )
99
100
assertThrows<IllegalStateException > { kai.mustHave { it.score >= 0 } }
100
- .also { ex -> assertEquals(" mustHave: check failed" , ex.message) }
101
+ .also { ex -> assertEquals(" mustHave check failed: Player(id=1, name=Kai, score=-5) " , ex.message) }
101
102
102
103
assertThrows<InvalidPlayerException > {
103
104
kai.mustHave(
0 commit comments