Skip to content

Commit

Permalink
Merge pull request #216 from piotrb5e3/add-suspend-backticks-exceptio…
Browse files Browse the repository at this point in the history
…n-assertions

Added `should throw` and `should not throw` assertions that operate on suspending functions
  • Loading branch information
MarkusAmshove committed Oct 14, 2022
2 parents 96b5314 + e46ca10 commit 385570a
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jvm/src/main/kotlin/org/amshove/kluent/ExceptionsBacktick.kt
Expand Up @@ -5,9 +5,13 @@ import kotlin.reflect.KClass
infix fun <T : Throwable> (() -> Any?).`should throw`(expectedException: KClass<T>) =
this.shouldThrow(expectedException)

suspend infix fun <T : Throwable> (suspend () -> Any?).`should throw`(expectedException: KClass<T>) = this.shouldThrow(expectedException)

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

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

@Deprecated("Use `should throw` instead", ReplaceWith("this `should throw` expectedException"))
infix fun <T : Throwable> (() -> Any).`should throw the Exception`(expectedException: KClass<T>) =
this.shouldThrow(expectedException)
Expand All @@ -26,3 +30,5 @@ infix fun <T : Throwable> ExceptionResult<T>.`with cause`(expectedCause: KClass<
infix fun NotThrowExceptionResult.`with cause`(expectedCause: KClass<out Throwable>) = this.withCause(expectedCause)

infix fun <T : Throwable> (() -> Any?).`should throw`(expectedException: T) = this.shouldThrow(expectedException)

suspend infix fun <T : Throwable> (suspend () -> Any?).`should throw`(expectedException: T) = this.shouldThrow(expectedException)
@@ -0,0 +1,24 @@
package org.amshove.kluent.tests.assertions.exceptions

import org.amshove.kluent.AnyException
import org.amshove.kluent.`should not throw`
import org.amshove.kluent.invoking
import org.junit.Test
import kotlin.test.assertFails

class ShouldNotThrowBackticksStyle {
@Test
fun shouldNotThrowSucceedsWhenNotThrown() {
invoking { } `should not throw` AnyException
}

@Test
fun shouldNotThrowFailsWhenThrown() {
assertFails { invoking { throw CustomException(12345) } `should not throw` AnyException }
}

@Test
fun shouldNotThrowSucceedsWhenDifferentExceptionClassThrown() {
invoking { throw IllegalArgumentException() } `should not throw` CustomException::class
}
}
@@ -0,0 +1,35 @@
package org.amshove.kluent.tests.assertions.exceptions

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.test.runBlockingTest
import org.amshove.kluent.AnyException
import org.amshove.kluent.`should not throw`
import org.amshove.kluent.coInvoking
import org.amshove.kluent.internal.assertFails
import org.junit.Test

class ShouldNotThrowSuspendBackticksStyle {
@Test
@ExperimentalCoroutinesApi
fun shouldNotThrowSucceedsWhenNotThrown() = runBlockingTest {
suspend fun func() = coroutineScope {}
coInvoking { func() } `should not throw` AnyException
}

@Test
@ExperimentalCoroutinesApi
fun shouldNotThrowFailsWhenThrown() = runBlockingTest {
suspend fun func(): Nothing = coroutineScope { throw CustomException(12345) }

assertFails { coInvoking { func() } `should not throw` AnyException }
}

@Test
@ExperimentalCoroutinesApi
fun shouldNotThrowSucceedsWhenDifferentExceptionClassThrown() = runBlockingTest {
suspend fun func(): Nothing = coroutineScope { throw IllegalArgumentException() }

coInvoking { func() } `should not throw` CustomException::class
}
}
@@ -0,0 +1,34 @@
package org.amshove.kluent.tests.assertions.exceptions


import org.amshove.kluent.`should throw`
import org.amshove.kluent.invoking
import org.junit.Test
import kotlin.test.assertFails

class ShouldThrowBackticksStyle {
@Test
fun shouldThrowSucceedsWhenExpectedInstanceThrown() {
invoking { throw CustomException(12345) } `should throw` CustomException(12345)
}

@Test
fun shouldThrowSucceedsWhenExpectedClassThrown() {
invoking { throw CustomException(12345) } `should throw` CustomException::class
}

@Test
fun shouldThrowFailsWhenNoExceptionThrown() {
assertFails { invoking { } `should throw` CustomException(12345) }
}

@Test
fun shouldThrowFailsWhenDifferentExceptionThrown() {
assertFails { invoking { throw CustomException(54321) } `should throw` CustomException(12345) }
}

@Test
fun shouldThrowFailsWhenDifferentExceptionClassThrown() {
assertFails { invoking { throw IllegalArgumentException() } `should throw` CustomException::class }
}
}
@@ -0,0 +1,51 @@
package org.amshove.kluent.tests.assertions.exceptions

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.test.runBlockingTest
import org.amshove.kluent.`should throw`
import org.amshove.kluent.coInvoking
import org.junit.Test
import kotlin.test.assertFails

class ShouldThrowSuspendBackticksStyle {
@Test
@ExperimentalCoroutinesApi
fun shouldThrowSucceedsWhenExpectedInstanceThrown() = runBlockingTest {
suspend fun func(): Nothing = coroutineScope { throw CustomException(12345) }

coInvoking { func() } `should throw` CustomException(12345)
}

@Test
@ExperimentalCoroutinesApi
fun shouldThrowSucceedsWhenExpectedClassThrown() = runBlockingTest {
suspend fun func(): Nothing = coroutineScope { throw CustomException(12345) }

coInvoking { func() } `should throw` CustomException::class
}

@Test
@ExperimentalCoroutinesApi
fun shouldThrowFailsWhenNoExceptionThrown() = runBlockingTest {
suspend fun func() = coroutineScope { }

assertFails { coInvoking { func() } `should throw` CustomException(12345) }
}

@Test
@ExperimentalCoroutinesApi
fun shouldThrowFailsWhenDifferentExceptionThrown() = runBlockingTest {
suspend fun func(): Nothing = coroutineScope { throw CustomException(54321) }

assertFails { coInvoking { func() } `should throw` CustomException(12345) }
}

@Test
@ExperimentalCoroutinesApi
fun shouldThrowFailsWhenDifferentExceptionClassThrown() = runBlockingTest {
suspend fun func(): Nothing = coroutineScope { throw java.lang.IllegalArgumentException() }

assertFails { coInvoking { func() } `should throw` CustomException::class }
}
}

0 comments on commit 385570a

Please sign in to comment.