Skip to content

Commit

Permalink
Even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luka Jacobowitz committed Aug 16, 2018
1 parent 6a669b8 commit e5b414c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
38 changes: 38 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyChain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal {

/**
* Tests if some element is contained in this chain.
* {{{
* scala> import cats.data.NonEmptyChain
* scala> import cats.implicits._
* scala> val nec = NonEmptyChain(4, 5, 6)
* scala> nec.contains(5)
* res0: Boolean = true
* }}}
*/
final def contains(a: A)(implicit A: Eq[A]): Boolean = toChain.contains(a)

Expand All @@ -227,6 +234,13 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal {

/**
* Returns a new `Chain` containing all elements where the result of `pf` is final defined.
* {{{
* scala> import cats.data.NonEmptyChain
* scala> import cats.implicits._
* scala> val nec = NonEmptyChain(4, 5, 6).map(n => if (n % 2 == 0) Some(n) else None)
* scala> nec.collect { case Some(n) => n }
* res0: cats.data.Chain[Int] = Chain(4, 6)
* }}}
*/
final def collect[B](pf: PartialFunction[A, B]): Chain[B] = toChain.collect(pf)

Expand Down Expand Up @@ -255,6 +269,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal {

/**
* Left-associative reduce using f.
* {{{
* scala> import cats.data.NonEmptyChain
* scala> val nec = NonEmptyChain(4, 5, 6)
* scala> nec.reduceLeft(_ + _)
* res0: Int = 15
* }}}
*/
final def reduceLeft(f: (A, A) => A): A = {
val iter = toChain.iterator
Expand All @@ -266,6 +286,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal {
/**
* Apply `f` to the "initial element" of this chain and lazily combine it
* with every other value using the given function `g`.
* {{{
* scala> import cats.data.NonEmptyChain
* scala> val nec = NonEmptyChain(4, 5, 6)
* scala> nec.reduceLeftTo(_.toString)((acc, cur) => acc + cur.toString)
* res0: String = 456
* }}}
*/
final def reduceLeftTo[B](f: A => B)(g: (B, A) => B): B = {
val iter = toChain.iterator
Expand All @@ -276,6 +302,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal {

/**
* Right-associative reduce using f.
* {{{
* scala> import cats.data.NonEmptyChain
* scala> val nec = NonEmptyChain(4, 5, 6)
* scala> nec.reduceRight(_ + _)
* res0: Int = 15
* }}}
*/
final def reduceRight(f: (A, A) => A): A = {
val iter = toChain.reverseIterator
Expand All @@ -287,6 +319,12 @@ class NonEmptyChainOps[A](val value: NonEmptyChain[A]) extends AnyVal {
/**
* Apply `f` to the "initial element" of this chain and lazily combine it
* with every other value using the given function `g`.
* {{{
* scala> import cats.data.NonEmptyChain
* scala> val nec = NonEmptyChain(4, 5, 6)
* scala> nec.reduceLeftTo(_.toString)((cur, acc) => acc + cur.toString)
* res0: String = 654
* }}}
*/
final def reduceRightTo[B](f: A => B)(g: (A, B) => B): B = {
val iter = toChain.reverseIterator
Expand Down
18 changes: 17 additions & 1 deletion tests/src/test/scala/cats/tests/ChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package tests

import cats.data.Chain
import cats.kernel.laws.discipline.{MonoidTests, OrderTests}
import cats.kernel.laws.discipline.{EqTests, MonoidTests, OrderTests, PartialOrderTests}
import cats.laws.discipline.{AlternativeTests, CoflatMapTests, MonadTests, SerializableTests, TraverseTests}
import cats.laws.discipline.arbitrary._

Expand All @@ -25,6 +25,22 @@ class ChainSuite extends CatsSuite {
checkAll("Chain[Int]", OrderTests[Chain[Int]].order)
checkAll("Order[Chain]", SerializableTests.serializable(Order[Chain[Int]]))

{
implicit val partialOrder = ListWrapper.partialOrder[Int]
checkAll("Chain[ListWrapper[Int]]",
PartialOrderTests[Chain[ListWrapper[Int]]].partialOrder)
checkAll("PartialOrder[Chain[ListWrapper[Int]]",
SerializableTests.serializable(PartialOrder[Chain[ListWrapper[Int]]]))
}

{
implicit val eqv = ListWrapper.eqv[Int]
checkAll("Chain[ListWrapper[Int]]",
EqTests[Chain[ListWrapper[Int]]].eqv)
checkAll("Eq[Chain[ListWrapper[Int]]",
SerializableTests.serializable(Eq[Chain[ListWrapper[Int]]]))
}

test("show"){
Show[Chain[Int]].show(Chain(1, 2, 3)) should === ("Chain(1, 2, 3)")
Chain.empty[Int].show should === ("Chain()")
Expand Down
18 changes: 17 additions & 1 deletion tests/src/test/scala/cats/tests/NonEmptyChainSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cats
package tests

import cats.data.{Chain, NonEmptyChain}
import cats.kernel.laws.discipline.{OrderTests, SemigroupTests}
import cats.kernel.laws.discipline.{EqTests, OrderTests, PartialOrderTests, SemigroupTests}
import cats.laws.discipline.{BimonadTests, NonEmptyTraverseTests, SemigroupKTests, SerializableTests}
import cats.laws.discipline.arbitrary._

Expand All @@ -22,6 +22,22 @@ class NonEmptyChainSuite extends CatsSuite {
checkAll("NonEmptyChain[Int]", OrderTests[NonEmptyChain[Int]].order)
checkAll("Order[NonEmptyChain[Int]", SerializableTests.serializable(Order[NonEmptyChain[Int]]))

{
implicit val partialOrder = ListWrapper.partialOrder[Int]
checkAll("NonEmptyChain[ListWrapper[Int]]",
PartialOrderTests[NonEmptyChain[ListWrapper[Int]]].partialOrder)
checkAll("PartialOrder[NonEmptyChain[ListWrapper[Int]]",
SerializableTests.serializable(PartialOrder[NonEmptyChain[ListWrapper[Int]]]))
}

{
implicit val eqv = ListWrapper.eqv[Int]
checkAll("NonEmptyChain[ListWrapper[Int]]",
EqTests[NonEmptyChain[ListWrapper[Int]]].eqv)
checkAll("Eq[NonEmptyChain[ListWrapper[Int]]",
SerializableTests.serializable(Eq[NonEmptyChain[ListWrapper[Int]]]))
}

test("show"){
Show[NonEmptyChain[Int]].show(NonEmptyChain(1, 2, 3)) should === ("NonEmptyChain(1, 2, 3)")
}
Expand Down

0 comments on commit e5b414c

Please sign in to comment.