From c5bd3ebecae82271d00d64e46df8ce4adc19dbcb Mon Sep 17 00:00:00 2001 From: Dmitry Kandalov Date: Fri, 6 Oct 2023 16:56:25 +0100 Subject: [PATCH 1/2] tidy up, formatting --- .../forkhandles/result4k/kotest/matchers.kt | 12 ++--- .../result4k/kotest/MatchersTest.kt | 51 ++++++++----------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt b/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt index d8c7d8f..713d5e8 100644 --- a/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt +++ b/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt @@ -60,14 +60,14 @@ infix fun Result<*, E>.shouldBeFailure(expected: E) = private fun Result.matchesValue(value: Result): MatcherResult = MatcherResult( - value == this, - { "$value should be $this" }, - { "$value should not be $this" }, + passed = value == this, + failureMessageFn = { "$value should be $this" }, + negatedFailureMessageFn = { "$value should not be $this" }, ) private fun > Result.matchesType(resultClass: KClass): MatcherResult = MatcherResult( - resultClass.isInstance(this), - { "$this should be ${resultClass.simpleName}" }, - { "$this should not be ${resultClass.simpleName}" }, + passed = resultClass.isInstance(this), + failureMessageFn = { "$this should be ${resultClass.simpleName}" }, + negatedFailureMessageFn = { "$this should not be ${resultClass.simpleName}" }, ) diff --git a/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt b/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt index d5ed08e..820edea 100644 --- a/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt +++ b/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt @@ -12,62 +12,57 @@ 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() + success.shouldBeFailure() } - throwsAssertionError("Success(value=Test successful) should be Failure") { - actualResult.shouldBeFailure { } + success.shouldBeFailure { } } - throwsAssertionError("Success(value=Test successful) should be Failure(reason=Test successful)") { - actualResult.shouldBeFailure(actualValue) + 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() + failure.shouldBeSuccess() } - throwsAssertionError("Failure(reason=Test failed) should be Success") { - actualResult.shouldBeSuccess { } + failure.shouldBeSuccess { } } - throwsAssertionError("Failure(reason=Test failed) should be Success(value=Test failed)") { - actualResult.shouldBeSuccess(actualValue) + failure.shouldBeSuccess(reason) } } @@ -76,7 +71,6 @@ class MatchersTest { throwsAssertionError("Failure(reason=Actual value) should be Failure(reason=Expected value)") { Failure("Actual value").shouldBeFailure("Expected value") } - throwsAssertionError("Success(value=Actual value) should be Success(value=Expected value)") { Success("Actual value").shouldBeSuccess("Expected value") } @@ -86,5 +80,4 @@ class MatchersTest { assertThrows { block() }.also { assertThat(it.message, present(equalTo(message))) } - } From 94140b05aebaf102e6a4fd06fd09942b289cef7c Mon Sep 17 00:00:00 2001 From: Dmitry Kandalov Date: Sat, 7 Oct 2023 13:00:40 +0100 Subject: [PATCH 2/2] use ComparableMatcherResult so that IntelliJ shows link for the diff window --- .../forkhandles/result4k/kotest/matchers.kt | 27 ++++++++++++++----- .../result4k/kotest/MatchersTest.kt | 16 +++++------ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt b/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt index 713d5e8..62525b4 100644 --- a/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt +++ b/result4k/kotest/src/main/kotlin/dev/forkhandles/result4k/kotest/matchers.kt @@ -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 @@ -59,15 +60,29 @@ infix fun Result<*, E>.shouldBeFailure(expected: E) = this should beFailure(expected) private fun Result.matchesValue(value: Result): MatcherResult = - MatcherResult( + matcherResultWithIntelliJDiff( passed = value == this, - failureMessageFn = { "$value should be $this" }, - negatedFailureMessageFn = { "$value should not be $this" }, + actual = toString(), + expected = value.toString() ) private fun > Result.matchesType(resultClass: KClass): MatcherResult = - MatcherResult( + matcherResultWithIntelliJDiff( passed = resultClass.isInstance(this), - failureMessageFn = { "$this should be ${resultClass.simpleName}" }, - negatedFailureMessageFn = { "$this should not be ${resultClass.simpleName}" }, + 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 ) diff --git a/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt b/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt index 820edea..4c7cd58 100644 --- a/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt +++ b/result4k/kotest/src/test/kotlin/dev/forkhandles/result4k/kotest/MatchersTest.kt @@ -39,13 +39,13 @@ class MatchersTest { val value = "Test successful" val success = Success(value) - throwsAssertionError("Success(value=Test successful) should be Failure") { + throwsAssertionError("expected: but was:") { success.shouldBeFailure() } - throwsAssertionError("Success(value=Test successful) should be Failure") { + throwsAssertionError("expected: but was:") { success.shouldBeFailure { } } - throwsAssertionError("Success(value=Test successful) should be Failure(reason=Test successful)") { + throwsAssertionError("expected: but was:") { success.shouldBeFailure(value) } } @@ -55,23 +55,23 @@ class MatchersTest { val reason = "Test failed" val failure = Failure(reason) - throwsAssertionError("Failure(reason=Test failed) should be Success") { + throwsAssertionError("expected: but was:") { failure.shouldBeSuccess() } - throwsAssertionError("Failure(reason=Test failed) should be Success") { + throwsAssertionError("expected: but was:") { failure.shouldBeSuccess { } } - throwsAssertionError("Failure(reason=Test failed) should be Success(value=Test failed)") { + throwsAssertionError("expected: but was:") { failure.shouldBeSuccess(reason) } } @Test fun `should correctly assert value`() { - throwsAssertionError("Failure(reason=Actual value) should be Failure(reason=Expected value)") { + throwsAssertionError("expected: but was:") { Failure("Actual value").shouldBeFailure("Expected value") } - throwsAssertionError("Success(value=Actual value) should be Success(value=Expected value)") { + throwsAssertionError("expected: but was:") { Success("Actual value").shouldBeSuccess("Expected value") } }