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

Commit

Permalink
refactoring and matching Kotlin code-conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusAmshove committed Feb 10, 2016
1 parent cd4deba commit 844eaee
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package org.amshove.kluent

import org.junit.Assert.*
import org.junit.ComparisonFailure
import kotlin.reflect.KClass
import org.junit.Assert.*
import java.util.*

infix fun Any.`should equal`(theOther: Any) = assertEquals(theOther, this)
infix fun Any.`should not equal`(theOther: Any) = assertNotEquals(theOther, this)
Expand All @@ -25,12 +24,12 @@ infix fun <T : Exception> (() -> Unit).`should throw`(expectedException: KClass<
this.invoke()
fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None")
} catch (e: Exception) {
if (expectedException.javaObjectType == AnyException::class.javaObjectType) {
if (expectedException.isAnyException()) {
return
}
if (e.javaClass == expectedException.javaObjectType) {
Unit
} else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")
if (e.javaClass !== expectedException.javaObjectType) {
throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")
}
}
}

Expand All @@ -48,15 +47,13 @@ infix fun <T : Exception> (() -> Unit).`should throw the Exception`(expectedExce
infix fun <T : Exception> (() -> Unit).`should not throw`(expectedException: KClass<T>) {
try {
this.invoke()
Unit
} catch (e: Exception) {
if (expectedException.javaObjectType == AnyException::class.javaObjectType) {
if (expectedException.isAnyException()) {
fail("Expected no Exception to be thrown", "No Exception", "${e.javaClass}")
}
if (e.javaClass == expectedException.javaObjectType) {
fail("Expected ${expectedException.javaObjectType} to not be thrown", "${e.javaClass}", "${expectedException.javaObjectType}")
}
Unit
}
}

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

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

0 comments on commit 844eaee

Please sign in to comment.