Skip to content
This repository was archived by the owner on Jan 3, 2025. It is now read-only.

Commit 844eaee

Browse files
committed
refactoring and matching Kotlin code-conventions
1 parent cd4deba commit 844eaee

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package org.amshove.kluent
22

3+
import org.junit.Assert.*
34
import org.junit.ComparisonFailure
45
import kotlin.reflect.KClass
5-
import org.junit.Assert.*
6-
import java.util.*
76

87
infix fun Any.`should equal`(theOther: Any) = assertEquals(theOther, this)
98
infix fun Any.`should not equal`(theOther: Any) = assertNotEquals(theOther, this)
@@ -25,12 +24,12 @@ infix fun <T : Exception> (() -> Unit).`should throw`(expectedException: KClass<
2524
this.invoke()
2625
fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None")
2726
} catch (e: Exception) {
28-
if (expectedException.javaObjectType == AnyException::class.javaObjectType) {
27+
if (expectedException.isAnyException()) {
2928
return
3029
}
31-
if (e.javaClass == expectedException.javaObjectType) {
32-
Unit
33-
} else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")
30+
if (e.javaClass !== expectedException.javaObjectType) {
31+
throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")
32+
}
3433
}
3534
}
3635

@@ -48,15 +47,13 @@ infix fun <T : Exception> (() -> Unit).`should throw the Exception`(expectedExce
4847
infix fun <T : Exception> (() -> Unit).`should not throw`(expectedException: KClass<T>) {
4948
try {
5049
this.invoke()
51-
Unit
5250
} catch (e: Exception) {
53-
if (expectedException.javaObjectType == AnyException::class.javaObjectType) {
51+
if (expectedException.isAnyException()) {
5452
fail("Expected no Exception to be thrown", "No Exception", "${e.javaClass}")
5553
}
5654
if (e.javaClass == expectedException.javaObjectType) {
5755
fail("Expected ${expectedException.javaObjectType} to not be thrown", "${e.javaClass}", "${expectedException.javaObjectType}")
5856
}
59-
Unit
6057
}
6158
}
6259

@@ -65,7 +62,7 @@ infix fun <T : Exception> (() -> Unit).`should not throw the Exception`(expected
6562
this.invoke()
6663
return NotThrowExceptionResult(noException)
6764
} catch (e: Exception) {
68-
if (expectedException.javaObjectType == AnyException::class.javaObjectType) {
65+
if (expectedException.isAnyException()) {
6966
fail("Expected no Exception to be thrown", "No Exception", "${e.javaClass}")
7067
}
7168
return NotThrowExceptionResult(e)
@@ -85,6 +82,7 @@ val AnyException = AnyException::class
8582
class AnyException : Exception() {}
8683

8784
private val noException = Exception("None")
85+
private fun <T : Exception> KClass<T>.isAnyException() = this.javaObjectType == AnyException.javaObjectType
8886
private fun fail(message: String, expected: String, actual: String): Nothing = throw ComparisonFailure(message, expected, actual)
8987
private fun <T> join(theArray: Array<T>): String = theArray.joinToString(", ")
9088
private fun <T> join(theIterable: Iterable<T>): String = theIterable.joinToString(", ")

0 commit comments

Comments
 (0)