From c402b1861128b82edbe19ae1ce96914fbcaa3a0e Mon Sep 17 00:00:00 2001 From: Alphonse Bendt Date: Fri, 17 May 2019 00:00:23 +0200 Subject: [PATCH] get rid of isXYZWhere(predicate) functions. add extension properties to allow traversal of the unwrapped value --- .../main/kotlin/strikt/arrow/either/Either.kt | 52 ++------- .../main/kotlin/strikt/arrow/option/Option.kt | 63 +++++------ .../src/main/kotlin/strikt/arrow/try/Try.kt | 101 +++++------------- .../strikt/arrow/either/EitherAssertions.kt | 28 ++--- .../strikt/arrow/option/OptionAssertions.kt | 11 +- .../kotlin/strikt/arrow/try/TryAssertions.kt | 32 ++---- 6 files changed, 75 insertions(+), 212 deletions(-) diff --git a/strikt-arrow/src/main/kotlin/strikt/arrow/either/Either.kt b/strikt-arrow/src/main/kotlin/strikt/arrow/either/Either.kt index 6f8b0b4e..cd9f83f6 100644 --- a/strikt-arrow/src/main/kotlin/strikt/arrow/either/Either.kt +++ b/strikt-arrow/src/main/kotlin/strikt/arrow/either/Either.kt @@ -1,9 +1,7 @@ package strikt.arrow.either import arrow.core.Either -import arrow.core.Predicate import strikt.api.Assertion -import strikt.api.expectThat @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isRight() = @@ -28,28 +26,9 @@ fun Assertion.Builder>.isRight(value: R) = } } as Assertion.Builder> -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.isRightWhere(predicate: Predicate) = - assert("should be Right") { - when (it) { - is Either.Right -> if (predicate(it.b)) { - pass() - } else { - fail() - } - - else -> fail() - } - } as Assertion.Builder> - -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.expectRight(expect: Assertion.Builder.() -> Unit) = - assert("should be right") { - when (it) { - is Either.Left -> fail() - is Either.Right -> expectThat(it.b).expect() - } - } as Assertion.Builder> +val Assertion.Builder>.b: Assertion.Builder + @JvmName("eitherB") + get() = get("right value") { b } @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isLeft() = @@ -75,25 +54,6 @@ fun Assertion.Builder>.isLeft(value: L) = } } as Assertion.Builder> -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.isLeftWhere(predicate: Predicate) = - assert("should be Left") { - when (it) { - is Either.Left -> if (predicate(it.a)) { - pass() - } else { - fail() - } - - else -> fail() - } - } as Assertion.Builder> - -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.expectLeft(expect: Assertion.Builder.() -> Unit) = - assert("should be Left") { - when (it) { - is Either.Right -> fail() - is Either.Left -> expectThat(it.a).expect() - } - } as Assertion.Builder> +val Assertion.Builder>.a: Assertion.Builder + @JvmName("eitherA") + get() = get("left value") { a } diff --git a/strikt-arrow/src/main/kotlin/strikt/arrow/option/Option.kt b/strikt-arrow/src/main/kotlin/strikt/arrow/option/Option.kt index b211e639..71ac41af 100644 --- a/strikt-arrow/src/main/kotlin/strikt/arrow/option/Option.kt +++ b/strikt-arrow/src/main/kotlin/strikt/arrow/option/Option.kt @@ -2,54 +2,41 @@ package strikt.arrow.option import arrow.core.None import arrow.core.Option -import arrow.core.Predicate import arrow.core.Some import strikt.api.Assertion @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isNone() = - assert("should be None") { - when (it) { - is Some -> fail() - is None -> pass() - } - } as Assertion.Builder + assert("should be None") { + when (it) { + is Some -> fail() + is None -> pass() + } + } as Assertion.Builder @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isSome() = - assert("should be Some") { - when (it) { - is Some -> pass() - is None -> fail() - } - } as Assertion.Builder> + assert("should be Some") { + when (it) { + is Some -> pass() + is None -> fail() + } + } as Assertion.Builder> @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isSome(value: T) = - assert("should be Some($value)") { - when (it) { - is Some -> { - if (it.t == value) { - pass() - } else { - fail() - } - } - is None -> fail() + assert("should be Some($value)") { + when (it) { + is Some -> { + if (it.t == value) { + pass() + } else { + fail() } - } as Assertion.Builder> + } + is None -> fail() + } + } as Assertion.Builder> -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.isSomeWhere(predicate: Predicate) = - assert("should be Some") { - when (it) { - is Some -> { - if (predicate(it.t)) { - pass() - } else { - fail() - } - } - is None -> fail() - } - } as Assertion.Builder> +val Assertion.Builder>.t: Assertion.Builder + get() = get("some value") { t } diff --git a/strikt-arrow/src/main/kotlin/strikt/arrow/try/Try.kt b/strikt-arrow/src/main/kotlin/strikt/arrow/try/Try.kt index 63526a28..c335984d 100644 --- a/strikt-arrow/src/main/kotlin/strikt/arrow/try/Try.kt +++ b/strikt-arrow/src/main/kotlin/strikt/arrow/try/Try.kt @@ -1,92 +1,45 @@ package strikt.arrow.`try` import arrow.core.Failure -import arrow.core.Predicate import arrow.core.Success import arrow.core.Try import strikt.api.Assertion @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isSuccess() = - assert("should be Success") { - when (it) { - is Success -> pass() - else -> fail() - } - } as Assertion.Builder> + assert("should be Success") { + when (it) { + is Success -> pass() + else -> fail() + } + } as Assertion.Builder> @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isSuccess(value: T) = - assert("should be Success($value)") { - when (it) { - is Success -> { - if (it.value == value) { - pass() - } else { - fail() - } - } - else -> fail() + assert("should be Success($value)") { + when (it) { + is Success -> { + if (it.value == value) { + pass() + } else { + fail() } - } as Assertion.Builder> + } + else -> fail() + } + } as Assertion.Builder> -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.isSuccessWhere(check: Predicate) = - assert("should be Success") { - when (it) { - is Success -> { - if (check(it.value)) { - pass() - } else { - fail() - } - } - else -> fail() - } - } as Assertion.Builder> +val Assertion.Builder>.value: Assertion.Builder + get() = get("success value") { value } @Suppress("UNCHECKED_CAST") fun Assertion.Builder>.isFailure() = - assert("should be Failure") { - when (it) { - is Failure -> pass() - else -> fail() - } - } as Assertion.Builder - -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.isFailure(e: Exception) = - assert("should be Failure") { - when (it) { - is Failure -> { - if (it.exception == e) - pass() - else - fail(actual = it.exception) - } - else -> fail() - } - } as Assertion.Builder - -@Suppress("UNCHECKED_CAST") -fun Assertion.Builder>.isFailureWhere(check: Predicate) = - assert("should be Failure") { - when (it) { - is Failure -> { - if (check(it.exception)) { - pass() - } else { - fail() - } - } - else -> fail() - } - } as Assertion.Builder - -inline fun Assertion.Builder>.isFailureOfType() = - isFailureWhere { - when (it) { - is E -> true - else -> false - } + assert("should be Failure") { + when (it) { + is Failure -> pass() + else -> fail() } + } as Assertion.Builder + +val Assertion.Builder.exception: Assertion.Builder + get() = get("failure exception") { exception } diff --git a/strikt-arrow/src/test/kotlin/strikt/arrow/either/EitherAssertions.kt b/strikt-arrow/src/test/kotlin/strikt/arrow/either/EitherAssertions.kt index 7e8ed8e7..1dc980bc 100644 --- a/strikt-arrow/src/test/kotlin/strikt/arrow/either/EitherAssertions.kt +++ b/strikt-arrow/src/test/kotlin/strikt/arrow/either/EitherAssertions.kt @@ -27,18 +27,12 @@ object EitherAssertions { expectThat(anEither).isLeft("left") } - test("can assert on type with own predicate") { - expectThat(anEither).isLeftWhere { it == "left" } + test("can assert on type and traverse unwrapped value") { + expectThat(anEither).isLeft().a.isEqualTo("left") } - test("can chain assertion on narrowed type") { - expectThat(anEither).isLeft() - .get { a } - .isEqualTo("left") - } - - test("can have nested assertions on unwrapped type") { - expectThat(Either.left(MyTuple("myName", 1, "uuid"))).expectLeft { + test("can have nested assertions on unwrapped value") { + expectThat(Either.left(MyTuple("myName", 1, "uuid"))).isLeft().a.and { get { name }.isEqualTo("myName") get { id }.isNotNull().isGreaterThan(0L) get { uuid }.isNotNull().isNotBlank() @@ -59,18 +53,12 @@ object EitherAssertions { expectThat(anEither).isRight("right") } - test("can assert on type with own predicate") { - expectThat(anEither).isRightWhere { it == "right" } - } - - test("can chain assertion on narrowed type") { - expectThat(anEither).isRight() - .get { b } - .isEqualTo("right") + test("can assert on type and traverse unwrapped value") { + expectThat(anEither).isRight().b.isEqualTo("right") } - test("can have nested assertions on unwrapped type") { - expectThat(Either.right(MyTuple("myName", 1, "uuid"))).expectRight { + test("can have nested assertions on unwrapped value") { + expectThat(Either.right(MyTuple("myName", 1, "uuid"))).isRight().b.and { get { name }.isEqualTo("myName") get { id }.isNotNull().isGreaterThan(0L) get { uuid }.isNotNull().isNotBlank() diff --git a/strikt-arrow/src/test/kotlin/strikt/arrow/option/OptionAssertions.kt b/strikt-arrow/src/test/kotlin/strikt/arrow/option/OptionAssertions.kt index 85ce6078..98604367 100644 --- a/strikt-arrow/src/test/kotlin/strikt/arrow/option/OptionAssertions.kt +++ b/strikt-arrow/src/test/kotlin/strikt/arrow/option/OptionAssertions.kt @@ -27,15 +27,8 @@ object OptionAssertions { expectThat(option).isSome("aValue") } - test("can assert on type and with custom predicate") { - expectThat(option).isSomeWhere { it == "aValue" } - } - - test("can chain assertion on narrowed type") { - expectThat(option) - .isSome() - .get { t } - .isEqualTo("aValue") + test("can assert on type and traverse wrapped value") { + expectThat(option).isSome().t.isEqualTo("aValue") } }) diff --git a/strikt-arrow/src/test/kotlin/strikt/arrow/try/TryAssertions.kt b/strikt-arrow/src/test/kotlin/strikt/arrow/try/TryAssertions.kt index 96dfb60a..475d66af 100644 --- a/strikt-arrow/src/test/kotlin/strikt/arrow/try/TryAssertions.kt +++ b/strikt-arrow/src/test/kotlin/strikt/arrow/try/TryAssertions.kt @@ -6,7 +6,10 @@ import dev.minutest.rootContext import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.TestFactory import strikt.api.expectThat +import strikt.assertions.isA import strikt.assertions.isEqualTo +import strikt.assertions.isSameInstanceAs +import strikt.assertions.message @DisplayName("assertions on Try") object TryAssertions { @@ -27,15 +30,8 @@ object TryAssertions { expectThat(aTry).isSuccess("success") } - test("can assert on type and with custom predicate") { - expectThat(aTry).isSuccessWhere { it == "success" } - } - - test("can chain assertion on narrowed type") { - expectThat(aTry) - .isSuccess() - .get { value } - .isEqualTo("success") + test("can assert on type and traverse value") { + expectThat(aTry).isSuccess().value.isEqualTo("success") } }) @@ -51,22 +47,8 @@ object TryAssertions { expectThat(aTry).not().isSuccess() } - test("can assert on type and with custom predicate") { - expectThat(aTry).isFailureWhere { it.message == "testFailure" } + test("can assert on type and traverse exception") { + expectThat(aTry).isFailure().exception.message.isEqualTo("testFailure") } - - test("can chain assertion on narrowed type") { - expectThat(aTry) - .isFailure() - .get { exception.message } - .isEqualTo("testFailure") - } - - /* - it("can chain on narrowed type") { - expectThat(aTry) - .isFailureOfType() - } - */ }) }