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

Unify first-party helper functions #536

Closed
Closed
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
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