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

Kotest intellij diff #53

Merged
merged 2 commits into from
Oct 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.forkhandles.result4k.kotest
import dev.forkhandles.result4k.Failure
import dev.forkhandles.result4k.Result
import dev.forkhandles.result4k.Success
import io.kotest.matchers.ComparableMatcherResult
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
Expand Down Expand Up @@ -59,15 +60,29 @@ infix fun <E> Result<*, E>.shouldBeFailure(expected: E) =
this should beFailure(expected)

private fun <T, E> Result<T, E>.matchesValue(value: Result<T, E>): MatcherResult =
MatcherResult(
value == this,
{ "$value should be $this" },
{ "$value should not be $this" },
matcherResultWithIntelliJDiff(
passed = value == this,
actual = toString(),
expected = value.toString()
)

private fun <T, E, C : Result<T, E>> Result<T, E>.matchesType(resultClass: KClass<C>): MatcherResult =
MatcherResult(
resultClass.isInstance(this),
{ "$this should be ${resultClass.simpleName}" },
{ "$this should not be ${resultClass.simpleName}" },
matcherResultWithIntelliJDiff(
passed = resultClass.isInstance(this),
actual = this.toString(),
expected = resultClass.simpleName!!
)

/**
* Return ComparableMatcherResult so that Kotest throws AssertFailedError
* with the failure message formatted by io.kotest.assertions.intellijFormatError()
* which makes IntelliJ show the link to the diff window.
*/
private fun matcherResultWithIntelliJDiff(passed: Boolean, actual: String, expected: String) =
ComparableMatcherResult(
passed = passed,
failureMessageFn = { "" },
negatedFailureMessageFn = { "not " },
actual = actual,
expected = expected
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,66 @@ import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows

class MatchersTest {

@Test
fun `should correctly assert when Success`() {
val actualValue = "Successful"
val actualResult = Success(actualValue)
val value = "Successful"
val success = Success(value)

assertDoesNotThrow { actualResult.shouldBeSuccess() }
assertDoesNotThrow { actualResult.shouldBeSuccess() shouldBe actualValue }
assertDoesNotThrow { actualResult.shouldBeSuccess { assertEquals(actualValue, it) } }
assertDoesNotThrow { actualResult.shouldBeSuccess(actualValue) }
assertDoesNotThrow { success.shouldBeSuccess() }
assertDoesNotThrow { success.shouldBeSuccess() shouldBe value }
assertDoesNotThrow { success.shouldBeSuccess { assertEquals(value, it) } }
assertDoesNotThrow { success.shouldBeSuccess(value) }
}

@Test
fun `should correctly assert when Failure`() {
val actualValue = "Failed"
val actualResult = Failure(actualValue)
val reason = "Failed"
val failure = Failure(reason)

assertDoesNotThrow { actualResult.shouldBeFailure() }
assertDoesNotThrow { actualResult.shouldBeFailure() shouldBe actualValue }
assertDoesNotThrow { actualResult.shouldBeFailure { assertEquals(actualValue, it) } }
assertDoesNotThrow { actualResult.shouldBeFailure(actualValue) }
assertDoesNotThrow { failure.shouldBeFailure() }
assertDoesNotThrow { failure.shouldBeFailure() shouldBe reason }
assertDoesNotThrow { failure.shouldBeFailure { assertEquals(reason, it) } }
assertDoesNotThrow { failure.shouldBeFailure(reason) }
}

@Test
fun `should correctly assert when Success but expecting Failure`() {
val actualValue = "Test successful"
val actualResult = Success(actualValue)
val value = "Test successful"
val success = Success(value)

throwsAssertionError("Success(value=Test successful) should be Failure") {
actualResult.shouldBeFailure()
throwsAssertionError("expected:<Failure> but was:<Success(value=Test successful)>") {
success.shouldBeFailure()
}

throwsAssertionError("Success(value=Test successful) should be Failure") {
actualResult.shouldBeFailure { }
throwsAssertionError("expected:<Failure> but was:<Success(value=Test successful)>") {
success.shouldBeFailure { }
}

throwsAssertionError("Success(value=Test successful) should be Failure(reason=Test successful)") {
actualResult.shouldBeFailure(actualValue)
throwsAssertionError("expected:<Success(value=Test successful)> but was:<Failure(reason=Test successful)>") {
success.shouldBeFailure(value)
}
}

@Test
fun `should correctly assert when Failure but expecting Success`() {
val actualValue = "Test failed"
val actualResult = Failure(actualValue)
val reason = "Test failed"
val failure = Failure(reason)

throwsAssertionError("Failure(reason=Test failed) should be Success") {
actualResult.shouldBeSuccess()
throwsAssertionError("expected:<Success> but was:<Failure(reason=Test failed)>") {
failure.shouldBeSuccess()
}

throwsAssertionError("Failure(reason=Test failed) should be Success") {
actualResult.shouldBeSuccess { }
throwsAssertionError("expected:<Success> but was:<Failure(reason=Test failed)>") {
failure.shouldBeSuccess { }
}

throwsAssertionError("Failure(reason=Test failed) should be Success(value=Test failed)") {
actualResult.shouldBeSuccess(actualValue)
throwsAssertionError("expected:<Failure(reason=Test failed)> but was:<Success(value=Test failed)>") {
failure.shouldBeSuccess(reason)
}
}

@Test
fun `should correctly assert value`() {
throwsAssertionError("Failure(reason=Actual value) should be Failure(reason=Expected value)") {
throwsAssertionError("expected:<Failure(reason=Actual value)> but was:<Failure(reason=Expected value)>") {
Failure("Actual value").shouldBeFailure("Expected value")
}

throwsAssertionError("Success(value=Actual value) should be Success(value=Expected value)") {
throwsAssertionError("expected:<Success(value=Actual value)> but was:<Success(value=Expected value)>") {
Success("Actual value").shouldBeSuccess("Expected value")
}
}
Expand All @@ -86,5 +80,4 @@ class MatchersTest {
assertThrows<AssertionError> { block() }.also {
assertThat(it.message, present(equalTo(message)))
}

}
Loading