From 06668d28f8f65777ac9b232b50bed13c7e45409e Mon Sep 17 00:00:00 2001 From: Victor Reventos Date: Tue, 4 Apr 2017 15:59:20 -0500 Subject: [PATCH 1/2] Exception Result now has the type of the exception and converted the withCause and withMessage to be fluent. --- .../org/amshove/kluent/ExceptionResult.kt | 4 +- .../kotlin/org/amshove/kluent/Exceptions.kt | 23 ++++++---- .../ShouldThrowTheExceptionTests.kt | 43 ++++++++++++++++--- 3 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/org/amshove/kluent/ExceptionResult.kt b/src/main/kotlin/org/amshove/kluent/ExceptionResult.kt index ba5a3843..7384d354 100644 --- a/src/main/kotlin/org/amshove/kluent/ExceptionResult.kt +++ b/src/main/kotlin/org/amshove/kluent/ExceptionResult.kt @@ -1,6 +1,6 @@ package org.amshove.kluent -class ExceptionResult(val exception: Exception) { +class ExceptionResult(val exception: T) { val exceptionMessage = exception.message ?: "" val exceptionCause = exception.cause } @@ -8,4 +8,4 @@ class ExceptionResult(val exception: Exception) { class NotThrowExceptionResult(val exception: Exception) { val exceptionMessage = exception.message ?: "" val exceptionCause = exception.cause -} \ No newline at end of file +} diff --git a/src/main/kotlin/org/amshove/kluent/Exceptions.kt b/src/main/kotlin/org/amshove/kluent/Exceptions.kt index 9604cf03..d17b49b7 100644 --- a/src/main/kotlin/org/amshove/kluent/Exceptions.kt +++ b/src/main/kotlin/org/amshove/kluent/Exceptions.kt @@ -31,17 +31,18 @@ infix fun (() -> Any).`should not throw`(expectedException: KCla infix fun (() -> Any).shouldNotThrow(expectedException: KClass) = this `should not throw` expectedException -infix fun (() -> Any).`should throw the Exception`(expectedException: KClass): ExceptionResult { +infix fun (() -> Any).`should throw the Exception`(expectedException: KClass): ExceptionResult { try { this.invoke() fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None") } catch (e: Exception) { - if (e.isA(expectedException)) return ExceptionResult(e) + @Suppress("UNCHECKED_CAST") + if (e.isA(expectedException)) return ExceptionResult(e as T) else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}") } } -infix fun (() -> Any).shouldThrowTheException(expectedException: KClass): ExceptionResult = this `should throw the Exception` expectedException +infix fun (() -> Any).shouldThrowTheException(expectedException: KClass): ExceptionResult = this `should throw the Exception` expectedException infix fun (() -> Any).`should not throw the Exception`(expectedException: KClass): NotThrowExceptionResult { @@ -59,29 +60,33 @@ infix fun (() -> Any).`should not throw the Exception`(expectedE infix fun (() -> Any).shouldNotThrowTheException(expectedException: KClass): NotThrowExceptionResult = this `should not throw the Exception` expectedException -infix fun ExceptionResult.`with message`(theMessage: String) { +infix fun ExceptionResult.`with message`(theMessage: String): ExceptionResult { this.exceptionMessage `should equal` theMessage + return this } -infix fun ExceptionResult.withMessage(theMessage: String) = this `with message` theMessage +infix fun ExceptionResult.withMessage(theMessage: String) = this `with message` theMessage -infix fun NotThrowExceptionResult.`with message`(theMessage: String) { +infix fun NotThrowExceptionResult.`with message`(theMessage: String): NotThrowExceptionResult { this.exceptionMessage `should not equal` theMessage + return this; } infix fun NotThrowExceptionResult.withMessage(theMessage: String) = this `with message` theMessage -infix fun ExceptionResult.`with cause`(expectedCause: KClass) { +infix fun ExceptionResult.`with cause`(expectedCause: KClass): ExceptionResult { this.exceptionCause `should be instance of` expectedCause.java + return this } -infix fun ExceptionResult.withCause(expectedCause: KClass) = this `with cause` expectedCause +infix fun ExceptionResult.withCause(expectedCause: KClass) = this `with cause` expectedCause -infix fun NotThrowExceptionResult.`with cause`(expectedCause: KClass) { +infix fun NotThrowExceptionResult.`with cause`(expectedCause: KClass): NotThrowExceptionResult { this.exceptionCause `should not be instance of` expectedCause.java + return this } infix fun NotThrowExceptionResult.withCause(expectedCause: KClass) = this `with cause` expectedCause diff --git a/src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldThrowTheExceptionTests.kt b/src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldThrowTheExceptionTests.kt index 797ef70b..632b07fa 100644 --- a/src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldThrowTheExceptionTests.kt +++ b/src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldThrowTheExceptionTests.kt @@ -1,10 +1,6 @@ package org.amshove.kluent.tests.assertions -import org.amshove.kluent.AnyException -import org.amshove.kluent.shouldThrow -import org.amshove.kluent.shouldThrowTheException -import org.amshove.kluent.withCause -import org.amshove.kluent.withMessage +import org.amshove.kluent.* import org.jetbrains.spek.api.Spek import java.io.IOException import kotlin.test.assertFails @@ -53,6 +49,43 @@ class ShouldThrowTheExceptionTests : Spek({ assertFails({ func shouldThrow AnyException }) } } + on("being fluent asserting both a cause and a message") { + on("both the message and cause being right") { + it("should pass") { + val func = { throw IllegalArgumentException("hello", IOException()) } + func shouldThrowTheException IllegalArgumentException::class withCause IOException::class withMessage "hello" + } + } + + on("on the message being wrong") { + it("should fail") { + val func = { throw IllegalArgumentException("not hello", IOException()) } + assertFails { func shouldThrowTheException IllegalArgumentException::class withCause IOException::class withMessage "hello" } + } + } + } + + given("a custom exception") { + class CustomException(val code: Int) : Exception("code is $code") + + on("throwing an exception of the right type") { + it("should return the exception result with the given type") { + + val func = { throw CustomException(10) } + + func.shouldThrowTheException(CustomException::class).exception.code.shouldEqualTo(10) + } + } + on("throwing an exception of the wrong type") { + it("should fail") { + val func = { throw IllegalArgumentException() } + assertFails { func.shouldThrowTheException(CustomException::class).exception.code.shouldEqualTo(10) } + } + } + } + } }) + + From 0af7a783e1f88a0f58588eea2fad82d647ced2ce Mon Sep 17 00:00:00 2001 From: Victor Reventos Date: Tue, 4 Apr 2017 16:09:02 -0500 Subject: [PATCH 2/2] backtick test --- .../ShouldThrowTheExceptionTests.kt | 40 ++++++++++++++++--- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/ShouldThrowTheExceptionTests.kt b/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/ShouldThrowTheExceptionTests.kt index cc335ef3..1c41d341 100644 --- a/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/ShouldThrowTheExceptionTests.kt +++ b/src/test/kotlin/org/amshove/kluent/tests/backtickassertions/ShouldThrowTheExceptionTests.kt @@ -1,10 +1,6 @@ package org.amshove.kluent.tests.backtickassertions -import org.amshove.kluent.AnyException -import org.amshove.kluent.`should throw the Exception` -import org.amshove.kluent.`should throw` -import org.amshove.kluent.`with cause` -import org.amshove.kluent.`with message` +import org.amshove.kluent.* import org.jetbrains.spek.api.Spek import java.io.IOException import kotlin.test.assertFails @@ -53,5 +49,39 @@ class ShouldThrowTheExceptionTests : Spek({ assertFails({ func `should throw` AnyException }) } } + on("being fluent asserting both a cause and a message") { + on("both the message and cause being right") { + it("should pass") { + val func = { throw IllegalArgumentException("hello", IOException()) } + func `should throw the Exception` IllegalArgumentException::class `with cause` IOException::class `with message` "hello" + } + } + + on("on the message being wrong") { + it("should fail") { + val func = { throw IllegalArgumentException("not hello", IOException()) } + assertFails { func `should throw the Exception` IllegalArgumentException::class `with cause` IOException::class `with message` "hello" } + } + } + } + + given("a custom exception") { + class CustomException(val code: Int) : Exception("code is $code") + + on("throwing an exception of the right type") { + it("should return the exception result with the given type") { + + val func = { throw CustomException(10) } + + func.`should throw the Exception`(CustomException::class).exception.code.shouldEqualTo(10) + } + } + on("throwing an exception of the wrong type") { + it("should fail") { + val func = { throw IllegalArgumentException() } + assertFails { func.`should throw the Exception`(CustomException::class).exception.code.shouldEqualTo(10) } + } + } + } } })