Skip to content

Commit

Permalink
get rid of isXYZWhere(predicate) functions.
Browse files Browse the repository at this point in the history
add extension properties to allow traversal of the unwrapped value
  • Loading branch information
abendt authored and robfletcher committed Sep 15, 2019
1 parent 6bfb38a commit c402b18
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 212 deletions.
52 changes: 6 additions & 46 deletions strikt-arrow/src/main/kotlin/strikt/arrow/either/Either.kt
Original file line number Diff line number Diff line change
@@ -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 <L, R> Assertion.Builder<Either<L, R>>.isRight() =
Expand All @@ -28,28 +26,9 @@ fun <L, R> Assertion.Builder<Either<L, R>>.isRight(value: R) =
}
} as Assertion.Builder<Either.Right<R>>

@Suppress("UNCHECKED_CAST")
fun <L, R> Assertion.Builder<Either<L, R>>.isRightWhere(predicate: Predicate<R>) =
assert("should be Right") {
when (it) {
is Either.Right -> if (predicate(it.b)) {
pass()
} else {
fail()
}

else -> fail()
}
} as Assertion.Builder<Either.Right<R>>

@Suppress("UNCHECKED_CAST")
fun <L, R> Assertion.Builder<Either<L, R>>.expectRight(expect: Assertion.Builder<R>.() -> Unit) =
assert("should be right") {
when (it) {
is Either.Left -> fail()
is Either.Right -> expectThat(it.b).expect()
}
} as Assertion.Builder<Either.Right<R>>
val <R> Assertion.Builder<Either.Right<R>>.b: Assertion.Builder<R>
@JvmName("eitherB")
get() = get("right value") { b }

@Suppress("UNCHECKED_CAST")
fun <L, R> Assertion.Builder<Either<L, R>>.isLeft() =
Expand All @@ -75,25 +54,6 @@ fun <L, R> Assertion.Builder<Either<L, R>>.isLeft(value: L) =
}
} as Assertion.Builder<Either.Left<L>>

@Suppress("UNCHECKED_CAST")
fun <L, R> Assertion.Builder<Either<L, R>>.isLeftWhere(predicate: Predicate<L>) =
assert("should be Left") {
when (it) {
is Either.Left -> if (predicate(it.a)) {
pass()
} else {
fail()
}

else -> fail()
}
} as Assertion.Builder<Either.Left<L>>

@Suppress("UNCHECKED_CAST")
fun <L, R> Assertion.Builder<Either<L, R>>.expectLeft(expect: Assertion.Builder<L>.() -> Unit) =
assert("should be Left") {
when (it) {
is Either.Right -> fail()
is Either.Left -> expectThat(it.a).expect()
}
} as Assertion.Builder<Either.Left<L>>
val <L> Assertion.Builder<Either.Left<L>>.a: Assertion.Builder<L>
@JvmName("eitherA")
get() = get("left value") { a }
63 changes: 25 additions & 38 deletions strikt-arrow/src/main/kotlin/strikt/arrow/option/Option.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> Assertion.Builder<Option<T>>.isNone() =
assert("should be None") {
when (it) {
is Some -> fail()
is None -> pass()
}
} as Assertion.Builder<None>
assert("should be None") {
when (it) {
is Some -> fail()
is None -> pass()
}
} as Assertion.Builder<None>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Option<T>>.isSome() =
assert("should be Some") {
when (it) {
is Some -> pass()
is None -> fail()
}
} as Assertion.Builder<Some<T>>
assert("should be Some") {
when (it) {
is Some -> pass()
is None -> fail()
}
} as Assertion.Builder<Some<T>>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Option<T>>.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<Some<T>>
}
is None -> fail()
}
} as Assertion.Builder<Some<T>>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Option<T>>.isSomeWhere(predicate: Predicate<T>) =
assert("should be Some") {
when (it) {
is Some -> {
if (predicate(it.t)) {
pass()
} else {
fail()
}
}
is None -> fail()
}
} as Assertion.Builder<Some<T>>
val <T> Assertion.Builder<Some<T>>.t: Assertion.Builder<T>
get() = get("some value") { t }
101 changes: 27 additions & 74 deletions strikt-arrow/src/main/kotlin/strikt/arrow/try/Try.kt
Original file line number Diff line number Diff line change
@@ -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 <T> Assertion.Builder<Try<T>>.isSuccess() =
assert("should be Success") {
when (it) {
is Success -> pass()
else -> fail()
}
} as Assertion.Builder<Success<T>>
assert("should be Success") {
when (it) {
is Success -> pass()
else -> fail()
}
} as Assertion.Builder<Success<T>>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Try<T>>.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<Success<T>>
}
else -> fail()
}
} as Assertion.Builder<Success<T>>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Try<T>>.isSuccessWhere(check: Predicate<T>) =
assert("should be Success") {
when (it) {
is Success -> {
if (check(it.value)) {
pass()
} else {
fail()
}
}
else -> fail()
}
} as Assertion.Builder<Success<T>>
val <T> Assertion.Builder<Success<T>>.value: Assertion.Builder<T>
get() = get("success value") { value }

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Try<T>>.isFailure() =
assert("should be Failure") {
when (it) {
is Failure -> pass()
else -> fail()
}
} as Assertion.Builder<Failure>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Try<T>>.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<Failure>

@Suppress("UNCHECKED_CAST")
fun <T> Assertion.Builder<Try<T>>.isFailureWhere(check: Predicate<Throwable>) =
assert("should be Failure") {
when (it) {
is Failure -> {
if (check(it.exception)) {
pass()
} else {
fail()
}
}
else -> fail()
}
} as Assertion.Builder<Failure>

inline fun <reified E : Throwable> Assertion.Builder<Try<*>>.isFailureOfType() =
isFailureWhere {
when (it) {
is E -> true
else -> false
}
assert("should be Failure") {
when (it) {
is Failure -> pass()
else -> fail()
}
} as Assertion.Builder<Failure>

val Assertion.Builder<Failure>.exception: Assertion.Builder<Throwable>
get() = get("failure exception") { exception }
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})

Expand Down
32 changes: 7 additions & 25 deletions strikt-arrow/src/test/kotlin/strikt/arrow/try/TryAssertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
})

Expand All @@ -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<IllegalArgumentException>()
}
*/
})
}

0 comments on commit c402b18

Please sign in to comment.