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

Implement top level "invoking" function to chain throwable assertions #132

Merged
merged 1 commit into from
Jan 26, 2019
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
61 changes: 31 additions & 30 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
# Maintainers

1. Markus Amshove - [@MarkusAmshove](https://github.com/MarkusAmshove) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=MarkusAmshove))

# Contributors

1. Andreas Volkmann - [@goreRatzete](https://github.com/goreRatzete) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=goreRatzete))
1. Egor Andreevici - [@Egorand](https://github.com/Egorand) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Egorand))
1. Karun Japhet - [@javatarz](https://github.com/javatarz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=javatarz))
1. Eric Burke - [@eburke56](https://github.com/eburke56) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=eburke56))
1. Günther Grill - [@guenhter](https://github.com/guenhter) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=guenhter))
1. jpopadak -[@jpopadak](https://github.com/jpopadak) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=jpopadak))
1. Nikolai Hellwig - [@nhellwig](https://github.com/nhellwig) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=nhellwig))
1. Greg Woodfill - [@gregwoodfill](https://github.com/gregwoodfill) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=gregwoodfill))
1. cketti -[@cketti](https://github.com/cketti) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=cketti))
1. Grzegorz Miszewski - [@miszmaniac](https://github.com/miszmaniac) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=miszmaniac))
1. Yahor Berdnikau - [@Tapchicoma](https://github.com/Tapchicoma) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Tapchicoma))
1. Alan Evans - [@westonal](https://github.com/westonal) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=westonal))
1. Victor J - Reventos [@vjames19](https://github.com/vjames19) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=vjames19))
1. B. K. Oxley - [@binkley](https://github.com/binkley) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=binkley))
1. Vasili Chyrvon - [@Jeevuz](https://github.com/Jeevuz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Jeevuz))
1. Yannick - [@fishb6nes](https://github.com/fishb6nes) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=fishb6nes))
1. Ole Kristian - Sandum [@okkero](https://github.com/okkero) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=okkero))
1. Gabriel Aumala - [@GAumala](https://github.com/GAumala) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=GAumala))
1. Deconinck Alban - [@neyb](https://github.com/neyb) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=neyb))
1. Fabrício Rissetto - [@fabriciorissetto](https://github.com/fabriciorissetto) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=fabriciorissetto))
1. Vitus Ortner - [@vitusortner](https://github.com/vitusortner) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=vitusortner))
1. Caleb Brinkman - [@floralvikings](https://github.com/floralvikings) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=floralvikings))
1. Jonathan Cornaz - [@jcornaz](https://github.com/jcornaz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=jcornaz))
1. Ivan Atanasov - [@IvanAtanasov89](https://github.com/IvanAtanasov89) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=IvanAtanasov89))
# Maintainers

1. Markus Amshove - [@MarkusAmshove](https://github.com/MarkusAmshove) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=MarkusAmshove))

# Contributors

1. Andreas Volkmann - [@goreRatzete](https://github.com/goreRatzete) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=goreRatzete))
1. Egor Andreevici - [@Egorand](https://github.com/Egorand) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Egorand))
1. Karun Japhet - [@javatarz](https://github.com/javatarz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=javatarz))
1. Eric Burke - [@eburke56](https://github.com/eburke56) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=eburke56))
1. Günther Grill - [@guenhter](https://github.com/guenhter) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=guenhter))
1. jpopadak -[@jpopadak](https://github.com/jpopadak) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=jpopadak))
1. Nikolai Hellwig - [@nhellwig](https://github.com/nhellwig) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=nhellwig))
1. Greg Woodfill - [@gregwoodfill](https://github.com/gregwoodfill) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=gregwoodfill))
1. cketti -[@cketti](https://github.com/cketti) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=cketti))
1. Grzegorz Miszewski - [@miszmaniac](https://github.com/miszmaniac) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=miszmaniac))
1. Yahor Berdnikau - [@Tapchicoma](https://github.com/Tapchicoma) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Tapchicoma))
1. Alan Evans - [@westonal](https://github.com/westonal) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=westonal))
1. Victor J - Reventos [@vjames19](https://github.com/vjames19) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=vjames19))
1. B. K. Oxley - [@binkley](https://github.com/binkley) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=binkley))
1. Vasili Chyrvon - [@Jeevuz](https://github.com/Jeevuz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=Jeevuz))
1. Yannick - [@fishb6nes](https://github.com/fishb6nes) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=fishb6nes))
1. Ole Kristian - Sandum [@okkero](https://github.com/okkero) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=okkero))
1. Gabriel Aumala - [@GAumala](https://github.com/GAumala) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=GAumala))
1. Deconinck Alban - [@neyb](https://github.com/neyb) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=neyb))
1. Fabrício Rissetto - [@fabriciorissetto](https://github.com/fabriciorissetto) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=fabriciorissetto))
1. Vitus Ortner - [@vitusortner](https://github.com/vitusortner) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=vitusortner))
1. Caleb Brinkman - [@floralvikings](https://github.com/floralvikings) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=floralvikings))
1. Jonathan Cornaz - [@jcornaz](https://github.com/jcornaz) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=jcornaz))
1. Ivan Atanasov - [@IvanAtanasov89](https://github.com/IvanAtanasov89) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=IvanAtanasov89))
1. Ruben Gees - [@rubengees](https://github.com/rubengees) ([Contributions](https://github.com/MarkusAmshove/Kluent/commits?author=rubengees))
2 changes: 2 additions & 0 deletions jvm/src/main/kotlin/org/amshove/kluent/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.amshove.kluent
import org.junit.ComparisonFailure
import kotlin.reflect.KClass

fun invoking(function: () -> Any?): () -> Any? = function

infix fun <T : Throwable> (() -> Any?).shouldThrow(expectedException: KClass<T>): ExceptionResult<T> {
try {
this.invoke()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,57 @@ package org.amshove.kluent.tests.assertions.exceptions

import org.amshove.kluent.*
import org.junit.Test
import java.io.IOException
import kotlin.test.assertFails

class ShouldNotThrowShould {
@Test
fun passWhenTestingAFunctionThatDoesNotThrowAnException() {
val func = { Unit }
func shouldNotThrow AnyException
invoking { Unit } shouldNotThrow AnyException
}

@Test
fun failWhenTestingAFunctionThatDoesThrowAnException() {
val func = { throw IllegalArgumentException() }
assertFails { func shouldNotThrow AnyException }
assertFails {
invoking { throw IllegalArgumentException() } shouldNotThrow AnyException
}
}

@Test
fun passWhenTestingAFunctionThatDoesNotThrowTheExpectedException() {
val func = { throw IllegalArgumentException() }
func shouldNotThrow ArrayIndexOutOfBoundsException::class
invoking { throw IllegalArgumentException() } shouldNotThrow ArrayIndexOutOfBoundsException::class
}

@Test
fun passWhenTestingAFunctionThatReturnsNull() {
val func = { null }
func shouldNotThrow AnyException
invoking { null } shouldNotThrow AnyException
}

@Test
fun passWhenTestingAFunctionThatThrowsAnExceptionWithADifferentMessage() {
val func = { throw IllegalArgumentException("Actual Message") }
func shouldNotThrow IllegalAccessException::class withMessage "Expected Message"
invoking { throw IllegalArgumentException("Actual Message") } shouldNotThrow
IllegalAccessException::class withMessage "Expected Message"
}

@Test
fun failWhenTestingAFunctionThatThrowsAnExceptionWithTheSameMessage() {
val func = { throw IllegalArgumentException("Actual Message") }
assertFails { func shouldNotThrow IllegalAccessException::class withMessage "Actual Message" }
assertFails {
invoking { throw IllegalArgumentException("Actual Message") } shouldNotThrow
IllegalAccessException::class withMessage "Actual Message"
}
}

@Test
fun failWhenTestingAFunctionThatDoesThrowAnExceptionWithTheExpectedCause() {
val func = { throw Exception(RuntimeException()) }
assertFails { func shouldNotThrow Exception::class withCause RuntimeException::class }
assertFails {
invoking { throw Exception(RuntimeException()) } shouldNotThrow
Exception::class withCause RuntimeException::class
}
}

@Test
fun failWhenExpectingAnExceptionThatWasThrown() {
val func = { throw Exception() }
assertFails { func shouldNotThrow Exception::class }
assertFails {
invoking { throw Exception() } shouldNotThrow Exception::class
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,89 @@ import kotlin.test.assertFails
class ShouldThrowShould {
@Test
fun passWhenTestingAFunctionThatThrowsTheExpectedException() {
val func = { throw IndexOutOfBoundsException() }
func shouldThrow IndexOutOfBoundsException::class
invoking { throw IndexOutOfBoundsException() } shouldThrow IndexOutOfBoundsException::class
}

@Test
fun failWhenTestingAFunctionThatDoesNotThrowTheExpectedException() {
val func = { throw IndexOutOfBoundsException() }
assertFails { func shouldThrow IllegalArgumentException::class }
assertFails {
invoking { throw IndexOutOfBoundsException() } shouldThrow IllegalArgumentException::class
}
}

@Test
fun passWhenTestingAFunctionThatTriesToGetAnOutOfIndexedItem() {
val funcWithReturn = { listOf(0).get(-1) }
funcWithReturn shouldThrow IndexOutOfBoundsException::class
invoking { listOf(0)[-1] } shouldThrow IndexOutOfBoundsException::class
}

@Test
fun passWhenTestingAFunctionWhichThrowsASubtypeOfTheExpectedException() {
val func = { throw IllegalStateException() }
func shouldThrow RuntimeException::class
invoking { throw IllegalStateException() } shouldThrow RuntimeException::class
}

@Test
fun passWhenTestingAFunctionWhichThrowsAnExceptionWithTheExpectedMessage() {
val func = { throw Exception("Hello World!") }
func shouldThrow Exception::class withMessage "Hello World!"
invoking { throw Exception("Hello World!") } shouldThrow Exception::class withMessage "Hello World!"
}

@Test
fun failWhenTestingAFunctionWhichThrowsAnExceptionWithADifferentMessage() {
val func = { throw Exception("Hello World!") }
assertFails { func shouldThrow Exception::class withMessage "Hello" }
assertFails {
invoking { throw Exception("Hello World!") } shouldThrow Exception::class withMessage "Hello"
}
}

@Test
fun passWhenTestingAFunctionWhichThrowsAnExceptionWithTheExpectedCause() {
val func = { throw Exception(RuntimeException()) }
func shouldThrow Exception::class withCause RuntimeException::class
invoking { throw Exception(RuntimeException()) } shouldThrow Exception::class withCause RuntimeException::class
}

@Test
fun failWhenTestingAFunctionWhichThrowsAnExceptionWithAnUnexpectedCause() {
val func = { throw Exception(RuntimeException()) }
assertFails { func shouldThrow Exception::class withCause IOException::class }
assertFails {
invoking { throw Exception(RuntimeException()) } shouldThrow Exception::class withCause IOException::class
}
}

@Test
fun passWhenTestingAFunctionWhichThrowsWhenAnyExceptionIsExpected() {
val func = { throw Exception() }
func shouldThrow AnyException
invoking { throw Exception() } shouldThrow AnyException
}

@Test
fun failWhenTestingAFunctionWhichDoesNotThrowButAnyExceptionIsExpected() {
val func = { Unit }
assertFails { func shouldThrow AnyException }
assertFails { invoking { Unit } shouldThrow AnyException }
}

@Test
fun passWhenTestingAFunctionWhichThrowsAnExceptionWithMessageAndCause() {
val func = { throw IllegalArgumentException("hello", IOException()) }
func shouldThrow IllegalArgumentException::class withCause IOException::class withMessage "hello"
invoking { throw IllegalArgumentException("hello", IOException()) } shouldThrow
IllegalArgumentException::class withCause IOException::class withMessage "hello"
}

@Test
fun failWhenTestingAFunctionWhichThrowsAnExceptionWithMessageAndCauseExceptingADifferentMessage() {
val func = { throw IllegalArgumentException("not hello", IOException()) }
assertFails { func shouldThrow IllegalArgumentException::class withCause IOException::class withMessage "hello" }
assertFails {
invoking { throw IllegalArgumentException("not hello", IOException()) } shouldThrow
IllegalArgumentException::class withCause IOException::class withMessage "hello"
}
}

@Test
fun returnTheExceptionWhenPassing() {
val exception = invoking { throw CustomException(10) }.shouldThrow(CustomException::class).exception

val func = { throw CustomException(10) }

val exception = func.shouldThrow(CustomException::class).exception
exception.code.shouldEqual(10)
}

@Test
fun passWhenTestingForAnExactThrownException() {

val func = { throw CustomException(12345) }
func shouldThrow CustomException(12345)
invoking { throw CustomException(12345) } shouldThrow CustomException(12345)
}

@Test
fun failWhenTestingForAnExactThrownExceptionWhenTheExceptionDiffers() {
val func = { throw CustomException(12345) }
assertFails { func shouldThrow CustomException(54321) }
fun failWhenTestisngForAnExactThrownExceptionWhenTheExceptionDiffers() {
assertFails { invoking { throw CustomException(12345) } shouldThrow CustomException(54321) }
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ class StubTests : Spek({
it("should throw an exception") {
val mock = mock(Database::class)
When calling mock.getPerson() itReturns any()
val func = { mock.getPerson() }
func shouldThrow InvalidUseOfMatchersException::class
invoking { mock.getPerson() } shouldThrow InvalidUseOfMatchersException::class
}
}

Expand Down