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 9f0954f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
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/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 9f0954f

Please sign in to comment.