Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge shouldThrowTheException into shouldThrow #54

Merged
merged 2 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/main/kotlin/org/amshove/kluent/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package org.amshove.kluent
import org.junit.ComparisonFailure
import kotlin.reflect.KClass

infix fun <T : Throwable> (() -> Any).`should throw`(expectedException: KClass<T>) {
infix fun <T : Throwable> (() -> Any).`should throw`(expectedException: KClass<T>): ExceptionResult<T> {
try {
this.invoke()
fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None")
} catch (e: Throwable) {
if(e.isA(ComparisonFailure::class)) {
throw e
}
if (!e.isA(expectedException))
throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")
@Suppress("UNCHECKED_CAST")
if (e.isA(ComparisonFailure::class)) throw e
else if (e.isA(expectedException)) return ExceptionResult(e as T)
else throw ComparisonFailure("Expected ${expectedException.javaObjectType} to be thrown", "${expectedException.javaObjectType}", "${e.javaClass}")
}
}

Expand All @@ -33,20 +32,12 @@ infix fun <T : Throwable> (() -> Any).`should not throw`(expectedException: KCla

infix fun <T : Throwable> (() -> Any).shouldNotThrow(expectedException: KClass<T>) = this `should not throw` expectedException

@Deprecated("Use `should throw` instead", ReplaceWith("x `should throw` expectedException"))
infix fun <T : Throwable> (() -> Any).`should throw the Exception`(expectedException: KClass<T>): ExceptionResult<T>
= this `should throw` expectedException

infix fun <T : Throwable> (() -> Any).`should throw the Exception`(expectedException: KClass<T>): ExceptionResult<T> {
try {
this.invoke()
fail("There was an Exception expected to be thrown, but nothing was thrown", "$expectedException", "None")
} catch (e: Throwable) {
@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 <T : Throwable> (() -> Any).shouldThrowTheException(expectedException: KClass<T>): ExceptionResult<T> = this `should throw the Exception` expectedException

@Deprecated("User shouldThrow instead", ReplaceWith("x shouldThrow expectedException"))
infix fun <T : Throwable> (() -> Any).shouldThrowTheException(expectedException: KClass<T>): ExceptionResult<T> = this shouldThrow expectedException

infix fun <T : Throwable> (() -> Any).`should not throw the Exception`(expectedException: KClass<T>): NotThrowExceptionResult {
try {
Expand All @@ -63,12 +54,12 @@ infix fun <T : Throwable> (() -> Any).`should not throw the Exception`(expectedE
infix fun <T : Throwable> (() -> Any).shouldNotThrowTheException(expectedException: KClass<T>): NotThrowExceptionResult = this `should not throw the Exception` expectedException


infix fun <T: Throwable> ExceptionResult<T>.`with message`(theMessage: String): ExceptionResult<T> {
infix fun <T : Throwable> ExceptionResult<T>.`with message`(theMessage: String): ExceptionResult<T> {
this.exceptionMessage `should equal` theMessage
return this
}

infix fun <T: Throwable> ExceptionResult<T>.withMessage(theMessage: String) = this `with message` theMessage
infix fun <T : Throwable> ExceptionResult<T>.withMessage(theMessage: String) = this `with message` theMessage


infix fun NotThrowExceptionResult.`with message`(theMessage: String): NotThrowExceptionResult {
Expand All @@ -79,12 +70,12 @@ infix fun NotThrowExceptionResult.`with message`(theMessage: String): NotThrowEx
infix fun NotThrowExceptionResult.withMessage(theMessage: String) = this `with message` theMessage


infix fun <T: Throwable> ExceptionResult<T>.`with cause`(expectedCause: KClass<out Throwable>): ExceptionResult<T> {
infix fun <T : Throwable> ExceptionResult<T>.`with cause`(expectedCause: KClass<out Throwable>): ExceptionResult<T> {
this.exceptionCause `should be instance of` expectedCause.java
return this
}

infix fun <T: Throwable> ExceptionResult<T>.withCause(expectedCause: KClass<out Throwable>) = this `with cause` expectedCause
infix fun <T : Throwable> ExceptionResult<T>.withCause(expectedCause: KClass<out Throwable>) = this `with cause` expectedCause


infix fun NotThrowExceptionResult.`with cause`(expectedCause: KClass<out Throwable>): NotThrowExceptionResult {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.amshove.kluent.tests.assertions

import org.amshove.kluent.shouldThrow
import org.amshove.kluent.*
import org.jetbrains.spek.api.Spek
import java.io.IOException
import java.util.*
import kotlin.test.assertFails

Expand Down Expand Up @@ -32,5 +33,81 @@ class ShouldThrowTests : Spek({
func shouldThrow RuntimeException::class
}
}
on("throwing an exception with a message") {
it("should pass") {
val func = { throw Exception("Hello World!") }
func shouldThrow Exception::class withMessage "Hello World!"
}
}
on("throwing an exception with a wrong message") {
it("should fail") {
val func = { throw Exception("Hello World!") }
assertFails({ func shouldThrow Exception::class withMessage "Hello" })
}
}
on("throwing an exception with a cause") {
it("should pass") {
val func = { throw Exception(RuntimeException()) }
func shouldThrow Exception::class withCause RuntimeException::class
}
}
on("throwing an exception with a wrong cause") {
it("should fail") {
val func = { throw Exception(RuntimeException()) }
assertFails({ func shouldThrow Exception::class withCause IOException::class })
}
}
on("throwing another exception") {
it("should fail") {
val func = { throw IllegalArgumentException() }
assertFails({ func shouldThrow IndexOutOfBoundsException::class })
}
}
on("expecting any exception when any exception is thrown") {
it("should pass") {
val func = { throw Exception() }
func shouldThrow AnyException
}
}
on("expecting any exception when no exception is thrown") {
it("should fail") {
val func = { Unit }
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 shouldThrow 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 shouldThrow 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.shouldThrow(CustomException::class).exception.code.shouldEqualTo(10)
}
}
on("throwing an exception of the wrong type") {
it("should fail") {
val func = { throw IllegalArgumentException() }
assertFails { func.shouldThrow(CustomException::class).exception.code.shouldEqualTo(10) }
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.io.IOException
import kotlin.test.assertFails

class ShouldThrowTheExceptionTests : Spek({
given("the shouldThrowTheException method") {
given("the shouldThrow method") {
on("throwing an exception with a message") {
it("should pass") {
val func = { throw Exception("Hello World!") }
Expand Down Expand Up @@ -34,19 +34,19 @@ class ShouldThrowTheExceptionTests : Spek({
on("throwing another exception") {
it("should fail") {
val func = { throw IllegalArgumentException() }
assertFails({ func shouldThrow IndexOutOfBoundsException::class })
assertFails({ func shouldThrowTheException IndexOutOfBoundsException::class })
}
}
on("expecting any exception when any exception is thrown") {
it("should pass") {
val func = { throw Exception() }
func shouldThrow AnyException
func shouldThrowTheException AnyException
}
}
on("expecting any exception when no exception is thrown") {
it("should fail") {
val func = { Unit }
assertFails({ func shouldThrow AnyException })
assertFails({ func shouldThrowTheException AnyException })
}
}
on("being fluent asserting both a cause and a message") {
Expand Down Expand Up @@ -83,7 +83,6 @@ class ShouldThrowTheExceptionTests : Spek({
}
}
}

}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.amshove.kluent.tests.backtickassertions

import org.amshove.kluent.`should throw`
import org.amshove.kluent.*
import org.jetbrains.spek.api.Spek
import java.io.IOException
import java.util.*
import kotlin.test.assertFails

Expand Down Expand Up @@ -31,5 +32,81 @@ class ShouldThrowTests : Spek({
func `should throw` RuntimeException::class
}
}
on("throwing an exception with a message") {
it("should pass") {
val func = { throw Exception("Hello World!") }
func `should throw` Exception::class `with message` "Hello World!"
}
}
on("throwing an exception with a wrong message") {
it("should fail") {
val func = { throw Exception("Hello World!") }
assertFails({ func `should throw` Exception::class `with message` "Hello" })
}
}
on("throwing an exception with a cause") {
it("should pass") {
val func = { throw Exception(RuntimeException()) }
func `should throw` Exception::class `with cause` RuntimeException::class
}
}
on("throwing an exception with a wrong cause") {
it("should fail") {
val func = { throw Exception(RuntimeException()) }
assertFails({ func `should throw` Exception::class `with cause` IOException::class })
}
}
on("throwing another exception") {
it("should fail") {
val func = { throw IllegalArgumentException() }
assertFails({ func `should throw` IndexOutOfBoundsException::class })
}
}
on("expecting any exception when any exception is thrown") {
it("should pass") {
val func = { throw Exception() }
func `should throw` AnyException
}
}
on("expecting any exception when no exception is thrown") {
it("should fail") {
val func = { Unit }
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` 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` 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`(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`(CustomException::class).exception.code.shouldEqualTo(10) }
}
}
}
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ class ShouldThrowTheExceptionTests : Spek({
on("throwing another exception") {
it("should fail") {
val func = { throw IllegalArgumentException() }
assertFails({ func `should throw` IndexOutOfBoundsException::class })
assertFails({ func `should throw the Exception` IndexOutOfBoundsException::class })
}
}
on("expecting any exception when any exception is thrown") {
it("should pass") {
val func = { throw Exception() }
func `should throw` AnyException
func `should throw the Exception` AnyException
}
}
on("expecting any exception when no exception is thrown") {
it("should fail") {
val func = { Unit }
assertFails({ func `should throw` AnyException })
assertFails({ func `should throw the Exception` 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"
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" }
assertFails { func `should throw the Exception` IllegalArgumentException::class `with cause` IOException::class `with message` "hello" }
}
}
}
Expand Down