Skip to content

Commit

Permalink
Rename Throwable extractor functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Cooper authored and grodin committed May 17, 2024
1 parent d529997 commit 8b075bd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
16 changes: 8 additions & 8 deletions assertk/src/commonMain/kotlin/assertk/assertions/throwable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,38 @@ import assertk.all
/**
* Returns an assert on the throwable's message.
*/
fun Assert<Throwable>.message() = having("message", Throwable::message)
fun Assert<Throwable>.havingMessage() = having("message", Throwable::message)

/**
* Returns an assert on the throwable's cause.
*/
fun Assert<Throwable>.cause() = having("cause", Throwable::cause)
fun Assert<Throwable>.havingCause() = having("cause", Throwable::cause)

/**
* Returns an assert on the throwable's root cause.
*/
fun Assert<Throwable>.rootCause() = having("rootCause", Throwable::rootCause)
fun Assert<Throwable>.havingRootCause() = having("rootCause", Throwable::rootCause)

/**
* Asserts the throwable has the expected message.
*/
fun Assert<Throwable>.hasMessage(message: String?) {
message().isEqualTo(message)
havingMessage().isEqualTo(message)
}

/**
* Asserts the throwable contains the expected text
*/
fun Assert<Throwable>.messageContains(text: String) {
message().isNotNull().contains(text)
havingMessage().isNotNull().contains(text)
}

/**
* Asserts the throwable is similar to the expected cause, checking the type and message.
* @see [hasNoCause]
*/
fun Assert<Throwable>.hasCause(cause: Throwable) {
cause().isNotNull().all {
havingCause().isNotNull().all {
kClass().isEqualTo(cause::class)
hasMessage(cause.message)
}
Expand All @@ -48,14 +48,14 @@ fun Assert<Throwable>.hasCause(cause: Throwable) {
* @see [hasCause]
*/
fun Assert<Throwable>.hasNoCause() {
cause().isNull()
havingCause().isNull()
}

/**
* Asserts the throwable is similar to the expected root cause, checking the type and message.
*/
fun Assert<Throwable>.hasRootCause(cause: Throwable) {
rootCause().all {
havingRootCause().all {
kClass().isEqualTo(cause::class)
hasMessage(cause.message)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package test.assertk
import assertk.assertFailure
import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
import assertk.assertions.message
import assertk.assertions.havingMessage
import com.willowtreeapps.opentest4k.AssertionFailedError
import test.assertk.assertions.valueOrFail
import kotlin.coroutines.resume
Expand All @@ -28,7 +28,7 @@ class AssertFailureTest {
fun failure_originating_subject_not_wrapped_in_result() {
val t = assertFailsWith<AssertionFailedError> {
assertFailure { throw RuntimeException("foo") }
.message()
.havingMessage()
.isEqualTo("bar")
}
assertTrue("RuntimeException" in t.message!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class ThrowableTest {

@Test
fun extracts_message() {
assertEquals(subject.message, assertThat(subject).message().valueOrFail)
assertEquals(subject.message, assertThat(subject).havingMessage().valueOrFail)
}

@Test
fun extracts_cause() {
assertEquals(cause, assertThat(subject).cause().valueOrFail)
assertEquals(cause, assertThat(subject).havingCause().valueOrFail)
}

@Test
fun extracts_root_cause() {
assertEquals(rootCause, assertThat(subject).rootCause().valueOrFail)
assertEquals(rootCause, assertThat(subject).havingRootCause().valueOrFail)
}

//region hasMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package test.assertk.assertions.support

import assertk.assertThat
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isFailure
import assertk.assertions.isSuccess
import assertk.assertions.message
import assertk.assertions.havingMessage
import assertk.assertions.support.expected
import assertk.assertions.support.fail
import assertk.assertions.support.show
Expand Down Expand Up @@ -236,7 +234,7 @@ class SupportTest {
@Test
fun expected_originating_throwable_included_as_cause() {
val subject = RuntimeException()
val assert = assertThat(subject).message()
val assert = assertThat(subject).havingMessage()
val error = assertFailsWith<AssertionFailedError> {
assert.expected("message")
}
Expand All @@ -246,7 +244,7 @@ class SupportTest {
@Test
fun expected_originating_failure_result_included_as_cause() {
val subject = RuntimeException()
val assert = assertThat(Result.failure<String>(subject)).isFailure().message()
val assert = assertThat(Result.failure<String>(subject)).isFailure().havingMessage()
val error = assertFailsWith<AssertionFailedError> {
assert.expected("message")
}
Expand Down

0 comments on commit 8b075bd

Please sign in to comment.