Skip to content

Commit

Permalink
Merge pull request #539 from cb372/cats-NonEmptyList-syntax
Browse files Browse the repository at this point in the history
Add syntax for Cats NonEmptyList
  • Loading branch information
fthomas authored Jul 7, 2018
2 parents 190bfdb + 1ab5983 commit 4f0b315
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package eu.timepit.refined.cats

import cats.data.ValidatedNel
import cats.data.{NonEmptyList, ValidatedNel}
import cats.syntax.either._
import eu.timepit.refined.api.RefinedTypeOps
import eu.timepit.refined.types.numeric.PosInt

object syntax extends CatsRefinedTypeOpsSyntax
object syntax extends CatsRefinedTypeOpsSyntax with CatsNonEmptyListSyntax

trait CatsRefinedTypeOpsSyntax {
implicit class CatsRefinedTypeOps[FTP, T](rtOps: RefinedTypeOps[FTP, T]) {
def validate(t: T): ValidatedNel[String, FTP] =
rtOps.from(t).toValidatedNel
}
}

trait CatsNonEmptyListSyntax {
implicit class CatsNonEmptyListRefinedOps[A](nel: NonEmptyList[A]) {
def refinedSize: PosInt = PosInt.unsafeFrom(nel.size)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package eu.timepit.refined.cats

import _root_.cats.data.Validated
import _root_.cats.data.{NonEmptyList, Validated}
import eu.timepit.refined.W
import eu.timepit.refined.api.{Refined, RefinedTypeOps}
import eu.timepit.refined.numeric.Interval
import eu.timepit.refined.numeric.{Interval, Positive}
import eu.timepit.refined.refineMV
import eu.timepit.refined.types.numeric.PosInt
import org.scalacheck.Prop._
import org.scalacheck.Properties
Expand All @@ -25,4 +26,15 @@ class SyntaxSpec extends Properties("syntax") {
object OneToTen extends RefinedTypeOps[OneToTen, Int] with CatsRefinedTypeOpsSyntax
OneToTen.validate(5) ?= Validated.valid(OneToTen.unsafeFrom(5))
}

property("NonEmptyList refinedSize (1)") = secure {
import syntax._
NonEmptyList.of("one").refinedSize ?= refineMV[Positive](1)
}

property("NonEmptyList refinedSize (> 1)") = secure {
import syntax._
NonEmptyList.of("one", "two", "three").refinedSize ?= refineMV[Positive](3)
}

}

0 comments on commit 4f0b315

Please sign in to comment.