Skip to content
Permalink
Browse files

Add infix modifier to more Arrow matchers (#921) (#931)

  • Loading branch information...
Jiehong authored and sksamuel committed Aug 5, 2019
1 parent b97703c commit 3943e3ec2303de40833f1f02e53031dcd707bb1b
@@ -20,14 +20,13 @@ fun <T> Either<*, T>.shouldBeRight() {
fun <T> Either<Any?, T>.shouldNotBeRight() = this shouldNot beRight()
fun <T> beRight() = beInstanceOf2<Either<Any?, T>, Either.Right<T>>()


inline fun <B> Either<*, B>.shouldBeRight(fn: (B) -> Unit) {
inline infix fun <B> Either<*, B>.shouldBeRight(fn: (B) -> Unit) {
this should beRight()
fn((this as Either.Right<B>).b)
}

fun <B> Either<Any?, B>.shouldBeRight(b: B) = this should beRight(b)
fun <B> Either<Any?, B>.shouldNotBeRight(b: B) = this shouldNot beRight(b)
infix fun <B> Either<Any?, B>.shouldBeRight(b: B) = this should beRight(b)
infix fun <B> Either<Any?, B>.shouldNotBeRight(b: B) = this shouldNot beRight(b)
fun <B> beRight(b: B) = object : Matcher<Either<Any?, B>> {
override fun test(value: Either<Any?, B>): MatcherResult {
return when (value) {
@@ -55,13 +54,14 @@ fun <T> Either<T, Any?>.shouldBeLeft() {
fun <T> Either<T, Any?>.shouldNotBeLeft() = this shouldNot beLeft()
fun <T> beLeft() = beInstanceOf2<Either<T, Any?>, Either.Left<T>>()

inline fun <A> Either<A, *>.shouldBeLeft(fn: (A) -> Unit) {
inline infix fun <A> Either<A, *>.shouldBeLeft(fn: (A) -> Unit) {
this should beLeft()
fn((this as Either.Left<A>).a)
}

fun <A> Either<A, Any?>.shouldBeLeft(a: A) = this should beLeft(a)
fun <A> Either<A, Any?>.shouldNotBeLeft(a: A) = this shouldNot beLeft(a)

infix fun <A> Either<A, Any?>.shouldBeLeft(a: A) = this should beLeft(a)
infix fun <A> Either<A, Any?>.shouldNotBeLeft(a: A) = this shouldNot beLeft(a)
fun <A> beLeft(a: A) = object : Matcher<Either<A, Any?>> {
override fun test(value: Either<A, Any?>): MatcherResult {
return when (value) {
@@ -50,8 +50,8 @@ fun <T> containNoNulls() = object : Matcher<NonEmptyList<T>> {
)
}

fun <T> NonEmptyList<T>.shouldContain(t: T) = this should contain(t)
fun <T> NonEmptyList<T>.shouldNotContain(t: T) = this shouldNot contain(t)
infix fun <T> NonEmptyList<T>.shouldContain(t: T) = this should contain(t)
infix fun <T> NonEmptyList<T>.shouldNotContain(t: T) = this shouldNot contain(t)
fun <T> contain(t: T) = object : Matcher<NonEmptyList<T>> {
override fun test(value: NonEmptyList<T>) = MatcherResult(
value.all.contains(t),
@@ -75,8 +75,8 @@ fun <T> haveDuplicates() = object : Matcher<NonEmptyList<T>> {

fun <T> NonEmptyList<T>.shouldContainAll(vararg ts: T) = this should containAll(*ts)
fun <T> NonEmptyList<T>.shouldNotContainAll(vararg ts: T) = this shouldNot containAll(*ts)
fun <T> NonEmptyList<T>.shouldContainAll(ts: List<T>) = this should containAll(ts)
fun <T> NonEmptyList<T>.shouldNotContainAll(ts: List<T>) = this shouldNot containAll(ts)
infix fun <T> NonEmptyList<T>.shouldContainAll(ts: List<T>) = this should containAll(ts)
infix fun <T> NonEmptyList<T>.shouldNotContainAll(ts: List<T>) = this shouldNot containAll(ts)
fun <T> containAll(vararg ts: T) = containAll(ts.asList())
fun <T> containAll(ts: List<T>): Matcher<NonEmptyList<T>> = object : Matcher<NonEmptyList<T>> {
override fun test(value: NonEmptyList<T>) = MatcherResult(
@@ -86,8 +86,8 @@ fun <T> containAll(ts: List<T>): Matcher<NonEmptyList<T>> = object : Matcher<Non
)
}

fun NonEmptyList<Any>.shouldHaveSize(size: Int) = this should haveSize(size)
fun NonEmptyList<Any>.shouldNotHaveSize(size: Int) = this shouldNot haveSize(size)
infix fun NonEmptyList<Any>.shouldHaveSize(size: Int) = this should haveSize(size)
infix fun NonEmptyList<Any>.shouldNotHaveSize(size: Int) = this shouldNot haveSize(size)
fun <T> haveSize(size: Int): Matcher<NonEmptyList<T>> = object : Matcher<NonEmptyList<T>> {
override fun test(value: NonEmptyList<T>) =
MatcherResult(
@@ -97,8 +97,8 @@ fun <T> haveSize(size: Int): Matcher<NonEmptyList<T>> = object : Matcher<NonEmpt
)
}

fun <T> NonEmptyList<T>.shouldBeSingleElement(t: T) = this should singleElement(t)
fun <T> NonEmptyList<T>.shouldNotBeSingleElement(t: T) = this shouldNot singleElement(t)
infix fun <T> NonEmptyList<T>.shouldBeSingleElement(t: T) = this should singleElement(t)
infix fun <T> NonEmptyList<T>.shouldNotBeSingleElement(t: T) = this shouldNot singleElement(t)
fun <T> singleElement(t: T): Matcher<NonEmptyList<T>> = object : Matcher<NonEmptyList<T>> {
override fun test(value: NonEmptyList<T>) = MatcherResult(
value.size == 1 && value.head == t,
@@ -23,8 +23,8 @@ fun beSome() = object : Matcher<Option<*>> {
MatcherResult(value is Some, "$value should be Some", "$value should not be Some")
}

fun <T> Option<T>.shouldBeSome(t: T) = this should beSome(t)
fun <T> Option<T>.shouldNotBeSome(t: T) = this shouldNot beSome(t)
infix fun <T> Option<T>.shouldBeSome(t: T) = this should beSome(t)
infix fun <T> Option<T>.shouldNotBeSome(t: T) = this shouldNot beSome(t)
fun <T> beSome(t: T) = object : Matcher<Option<T>> {
override fun test(value: Option<T>): MatcherResult {
return when (value) {
@@ -41,7 +41,7 @@ fun <T> beSome(t: T) = object : Matcher<Option<T>> {
}
}

fun <T> Option<T>.shouldBeSome(fn: (T) -> Unit) {
infix fun <T> Option<T>.shouldBeSome(fn: (T) -> Unit) {
this.shouldBeSome()
fn((this.t as T))
}
@@ -14,8 +14,8 @@ import io.kotlintest.should
* Int.order().assert {
* 0 shouldBeEqvTo 0
* 0 shouldNotBeEqvTo -1
* 0 shouldBeGreatherThan -1
* 0 shouldBeGreatherThanOrEqual 0
* 0 shouldBeGreaterThan -1
* 0 shouldBeGreaterThanOrEqual 0
* 0 shouldBeSmallerThan 1
* 0 shouldBeSmallerThanOrEqual 0
* }
@@ -27,23 +27,23 @@ interface OrderAssertions<A> : EqAssertions<A> {

override fun EQA(): Eq<A> = OA()

infix fun A.shouldBeGreatherThan(b: A): Unit =
this should beGreatherThan(b)
infix fun A.shouldBeGreaterThan(b: A): Unit =
this should beGreaterThan(b)

infix fun A.shouldBeGreatherThanOrEqual(b: A): Unit =
this should beGreatherThanOrEqual(b)
infix fun A.shouldBeGreaterThanOrEqual(b: A): Unit =
this should beGreaterThanOrEqual(b)

infix fun A.shouldBeSmallerThan(b: A): Unit =
this should beSmallerThan(b)

infix fun A.shouldBeSmallerThanOrEqual(b: A): Unit =
this should beSmallerThanOrEqual(b)

fun A.beGreatherThan(b: A): Matcher<A> =
OA().run { matcher(gt(b), "value ${this@beGreatherThan} not greather than $b") }
fun A.beGreaterThan(b: A): Matcher<A> =
OA().run { matcher(gt(b), "value ${this@beGreaterThan} not greater than $b") }
fun A.beGreatherThanOrEqual(b: A): Matcher<A> =
OA().run { matcher(gte(b), "value ${this@beGreatherThanOrEqual} not greather or equal than $b") }
fun A.beGreaterThanOrEqual(b: A): Matcher<A> =
OA().run { matcher(gte(b), "value ${this@beGreaterThanOrEqual} not greater or equal than $b") }
fun A.beSmallerThan(b: A): Matcher<A> =
OA().run { matcher(lt(b), "value ${this@beSmallerThan} not smaller than $b") }
@@ -54,4 +54,4 @@ interface IOAssertions : TaglessAssertions<ForIO> {
override fun AF(): Applicative<ForIO> = IO.applicative()

override fun <A> Kind<ForIO, A>.blockingValue(): A = fix().unsafeRunSync()
}
}
@@ -20,7 +20,7 @@ fun <T> beSuccess() = beInstanceOf2<Try<T>, Success<T>>()

infix fun <T> Try<T>.shouldBeSuccess(t: T) = this should beSuccess(t)

fun <T> Try<T>.shouldNotBeSuccess(t: T) = this shouldNot beSuccess(t)
infix fun <T> Try<T>.shouldNotBeSuccess(t: T) = this shouldNot beSuccess(t)
fun <A> beSuccess(a: A) = object : Matcher<Try<A>> {
override fun test(value: Try<A>): MatcherResult {
return when (value) {
@@ -20,10 +20,10 @@ fun Validated<*, *>.shouldBeValid() {

fun <T> Validated<*, T>.shouldNotBeValid() = this shouldNot beValid()

fun <T> Validated<*, T>.shouldBeValid(value: T) = this should beValid(value)
fun <T> Validated<*, T>.shouldNotBeValid(value: T) = this shouldNot beValid(value)
infix fun <T> Validated<*, T>.shouldBeValid(value: T) = this should beValid(value)
infix fun <T> Validated<*, T>.shouldNotBeValid(value: T) = this shouldNot beValid(value)

fun <T> Validated<*, T>.shouldBeValid(fn: (Valid<T>) -> Unit) {
infix fun <T> Validated<*, T>.shouldBeValid(fn: (Valid<T>) -> Unit) {
this.shouldBeValid()
fn(this as Valid<T>)
}
@@ -45,12 +45,14 @@ fun Validated<*, *>.shouldBeInvalid() {
}
this should beInvalid()
}
fun Validated<*, *>.shouldNotBeInvalid() = this shouldNot beInvalid()

fun <T> Validated<*, T>.shouldBeInvalid(value: T) = this should beInvalid(value)
@Deprecated("use shouldBeValid() instead")
fun Validated<*, *>.shouldNotBeInvalid() = this shouldNot beInvalid()
@Deprecated("use shouldBeValid() instead")
fun <T> Validated<*, T>.shouldNotBeInvalid(value: T) = this shouldNot beInvalid(value)
infix fun <T> Validated<*, T>.shouldBeInvalid(value: T) = this should beInvalid(value)

fun <T> Validated<T, *>.shouldBeInvalid(fn: (Invalid<T>) -> Unit) {
infix fun <T> Validated<T, *>.shouldBeInvalid(fn: (Invalid<T>) -> Unit) {
this.shouldBeInvalid()
fn(this as Invalid<T>)
}
@@ -57,8 +57,8 @@ class ArrowAssertionsTests : StringSpec({
OrderAssertions(Int.order()) {
0 shouldBeEqvTo 0
0 shouldNotBeEqvTo -1
0 shouldBeGreatherThan -1
0 shouldBeGreatherThanOrEqual 0
0 shouldBeGreaterThan -1
0 shouldBeGreaterThanOrEqual 0
0 shouldBeSmallerThan 1
0 shouldBeSmallerThanOrEqual 0
}
@@ -102,4 +102,4 @@ class ArrowAssertionsTests : StringSpec({
forAll(personGen) { it.name.startsWith(prefix) }
}

})
})
@@ -31,7 +31,7 @@ class EitherMatchersTest : WordSpec() {
"Either should beRight(fn)" should {
"test that the either is of type right" {
data class Person(val name: String, val location: String)
Either.right(Person("sam", "chicago")).shouldBeRight {
Either.right(Person("sam", "chicago")) shouldBeRight {
it.name shouldBe "sam"
it.location shouldBe "chicago"
}
@@ -49,11 +49,11 @@ class EitherMatchersTest : WordSpec() {
Either.right("foo") should beRight("boo")
}.message shouldBe "Either should be Right(boo) but was Right(foo)"

Either.right("foo").shouldNotBeRight("boo")
Either.left("foo").shouldNotBeRight("foo")
Either.right("foo") shouldNotBeRight "boo"
Either.left("foo") shouldNotBeRight "foo"

Either.right("boo") should beRight("boo")
Either.right("boo").shouldBeRight("boo")
Either.right("boo") shouldBeRight "boo"
}
}

@@ -72,7 +72,7 @@ class EitherMatchersTest : WordSpec() {
"Either should beLeft(fn)" should {
"test that the either is of type right" {
data class Person(val name: String, val location: String)
Either.left(Person("sam", "chicago")).shouldBeLeft {
Either.left(Person("sam", "chicago")) shouldBeLeft {
it.name shouldBe "sam"
it.location shouldBe "chicago"
}
@@ -91,12 +91,12 @@ class EitherMatchersTest : WordSpec() {
}.message shouldBe "Either should be Left(boo) but was Left(foo)"

shouldThrow<AssertionError> {
Either.left("foo").shouldNotBeLeft("foo")
Either.left("foo") shouldNotBeLeft "foo"
}.message shouldBe "Either should not be Left(foo)"

Either.left("boo") should beLeft("boo")
Either.left("boo").shouldBeLeft("boo")
Either.right("boo").shouldNotBeLeft("boo")
Either.left("boo") shouldBeLeft "boo"
Either.right("boo") shouldNotBeLeft "boo"
}
}

@@ -114,4 +114,4 @@ class EitherMatchersTest : WordSpec() {
}
}
}
}
}
@@ -8,19 +8,27 @@ import io.kotlintest.assertions.arrow.nel.containNoNulls
import io.kotlintest.assertions.arrow.nel.containNull
import io.kotlintest.assertions.arrow.nel.containOnlyNulls
import io.kotlintest.assertions.arrow.nel.haveDuplicates
import io.kotlintest.assertions.arrow.nel.haveElementAt
import io.kotlintest.assertions.arrow.nel.haveSize
import io.kotlintest.assertions.arrow.nel.shouldBeSingleElement
import io.kotlintest.assertions.arrow.nel.shouldBeSorted
import io.kotlintest.assertions.arrow.nel.shouldBeUnique
import io.kotlintest.assertions.arrow.nel.shouldContain
import io.kotlintest.assertions.arrow.nel.shouldContainAll
import io.kotlintest.assertions.arrow.nel.shouldContainElementAt
import io.kotlintest.assertions.arrow.nel.shouldContainNoNulls
import io.kotlintest.assertions.arrow.nel.shouldContainNull
import io.kotlintest.assertions.arrow.nel.shouldContainOnlyNulls
import io.kotlintest.assertions.arrow.nel.shouldHaveDuplicates
import io.kotlintest.assertions.arrow.nel.shouldHaveSize
import io.kotlintest.assertions.arrow.nel.shouldNotBeSingleElement
import io.kotlintest.assertions.arrow.nel.shouldNotBeSorted
import io.kotlintest.assertions.arrow.nel.shouldNotBeUnique
import io.kotlintest.assertions.arrow.nel.shouldNotContain
import io.kotlintest.assertions.arrow.nel.shouldNotContainAll
import io.kotlintest.assertions.arrow.nel.shouldNotContainElementAt
import io.kotlintest.assertions.arrow.nel.shouldNotContainNoNulls
import io.kotlintest.assertions.arrow.nel.shouldNotContainNull
import io.kotlintest.assertions.arrow.nel.shouldNotContainOnlyNulls
import io.kotlintest.assertions.arrow.nel.shouldNotHaveDuplicates
import io.kotlintest.assertions.arrow.nel.shouldNotHaveSize
@@ -42,6 +50,15 @@ class NelMatchersTest : WordSpec() {
NonEmptyList.of(1, 2) shouldNot containNull()

NonEmptyList.of(null).shouldContainNull()
NonEmptyList.of(1).shouldNotContainNull()
}
}

"haveElementAt()" should {
"test that a nel contains an element at the right position" {
NonEmptyList.of(1, 2, null) should haveElementAt<Int?>(1, 2)
NonEmptyList.of(1, 2, null).shouldContainElementAt(1, 2)
NonEmptyList.of(1, 2, null).shouldNotContainElementAt(0, 42)
}
}

@@ -53,6 +70,11 @@ class NelMatchersTest : WordSpec() {
NonEmptyList.of(2, 1) should beSorted<Int>()
}
}

"test that a collection is not sorted" {
NonEmptyList.of(3, 2, 1, 4) shouldNot beSorted<Int>()
NonEmptyList.of(5, 2, 3, 4).shouldNotBeSorted()
}
}

"haveDuplicates" should {
@@ -81,15 +103,19 @@ class NelMatchersTest : WordSpec() {
NonEmptyList.of(1, 2) shouldBe singleElement(2)
}

NonEmptyList.of(1).shouldBeSingleElement(1)
NonEmptyList.of(1) shouldBeSingleElement 1
}

"test that a collection does not contain a single element" {
NonEmptyList.of(1, 2) shouldNotBeSingleElement 1
}
}

"should contain element" should {
"test that a collection contains an element" {
NonEmptyList.of(1, 2, 3) should contain(2)
NonEmptyList.of(1, 2, 3).shouldContain(2)
NonEmptyList.of(1, 2, 3).shouldNotContain(4)
NonEmptyList.of(1, 2, 3) shouldContain 2
NonEmptyList.of(1, 2, 3) shouldNotContain 4
shouldThrow<AssertionError> {
NonEmptyList.of(1, 2, 3) should contain(4)
}
@@ -99,8 +125,8 @@ class NelMatchersTest : WordSpec() {
"haveSize" should {
"test that a collection has a certain size" {
NonEmptyList.of(1, 2, 3) should haveSize(3)
NonEmptyList.of(1, 2, 3).shouldHaveSize(3)
NonEmptyList.of(1, 2, 3).shouldNotHaveSize(2)
NonEmptyList.of(1, 2, 3) shouldHaveSize 3
NonEmptyList.of(1, 2, 3) shouldNotHaveSize 2
shouldThrow<AssertionError> {
NonEmptyList.of(1, 2, 3) should haveSize(2)
}
@@ -113,6 +139,7 @@ class NelMatchersTest : WordSpec() {
NonEmptyList.of(1, 2, 3).shouldContainNoNulls()
NonEmptyList.of(null, null, null) shouldNot containNoNulls()
NonEmptyList.of(1, null, null) shouldNot containNoNulls()
NonEmptyList.of(1, null, null).shouldNotContainNoNulls()
}
}

@@ -141,6 +168,7 @@ class NelMatchersTest : WordSpec() {
col.shouldContainAll(1, 2, 3, 4, 5)
col.shouldContainAll(3, 2, 1)
col.shouldContainAll(5, 4, 3, 2, 1)
col shouldContainAll listOf(1, 2, 3, 4)

shouldThrow<AssertionError> {
col should containAll(1, 2, 6)
@@ -158,6 +186,15 @@ class NelMatchersTest : WordSpec() {
col should containAll(3, 2, 0)
}
}

"test that a collection shouldNot containAll elements" {
val col = NonEmptyList.of(1, 2, 3, 4, 5)

col shouldNot containAll(99, 88, 77)

col.shouldNotContainAll(99,88,77)
col shouldNotContainAll listOf(99, 88, 77)
}
}
}
}
}

0 comments on commit 3943e3e

Please sign in to comment.
You can’t perform that action at this time.