Skip to content

Commit

Permalink
fix(NonEmptyList DecodeJson): made error message a little bit better (#1
Browse files Browse the repository at this point in the history
)

* feat(ArgonautHelpers): added encodeDecodeLaw, so it can be used in
  other projects

  * refactor(scalatest): removed and replaced with scalacheck
  • Loading branch information
AusCoder authored and dbousamra committed May 28, 2017
1 parent 9d143ce commit 290978f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import argonaut._
import argonaut.Argonaut._
import java.util.UUID
import java.util.concurrent.TimeUnit

import org.scalacheck.{Arbitrary, Prop}
import scala.concurrent.duration.Duration
import scalaz._
import Scalaz._
Expand Down Expand Up @@ -51,13 +51,18 @@ object ArgonautHelpers {
implicit def NonEmptyListDecodeJson[A: DecodeJson]: DecodeJson[NonEmptyList[A]] = {
implicitly[DecodeJson[List[A]]].flatMap(l =>
DecodeJson[NonEmptyList[A]](c => std.list.toNel(l) match {
case None => DecodeResult.fail("[A]NonEmptyList[A]", c.history)
case None => DecodeResult.fail("Could not decode non empty list", c.history)
case Some(n) => DecodeResult.ok(n)
})
) setName "[A]NonEmptyList[A]"
)
}

implicit def NonEmptyListEncodeJson[A: EncodeJson]: EncodeJson[NonEmptyList[A]] =
fromFoldable[NonEmptyList, A]

def encodeDecodeLaw[A: DecodeJson : EncodeJson : Arbitrary]: Prop = {
Prop.forAll { a: A =>
CodecJson.codecLaw(CodecJson.derived[A])(a)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,24 @@ import java.time.Instant
import java.util.UUID

import argonaut._
import org.scalacheck._
import ArgonautHelpers._
import com.imageintelligence.galahad.core.Generators._
import org.scalatest.PropSpec
import org.scalatest.prop.Checkers
import org.scalacheck._

import scala.concurrent.duration.Duration
import scalaz.NonEmptyList

class ArgonautHelpersSpec extends Properties("ArgonautHelpers") {

class ArgonautHelpersSpec extends PropSpec with Checkers {
property("URL encode/decode") = encodeDecodeLaw[URL]

def encodeDecodeLaw[A: DecodeJson : EncodeJson : Arbitrary]: Prop = {
org.scalacheck.Prop.forAll { a: A =>
implicitly[DecodeJson[A]].apply(implicitly[EncodeJson[A]].apply(a).hcursor).value exists (_ === a)
}
}
property("UUID encode/decode") = encodeDecodeLaw[UUID]

property("URL encode/decode") {
check(encodeDecodeLaw[URL], MinSuccessful(100))
}
property("Duration encode/decode") = encodeDecodeLaw[Duration]

property("UUID encode/decode") {
check(encodeDecodeLaw[UUID], MinSuccessful(100))
}
property("Instant encode/decode") = encodeDecodeLaw[Instant]

property("Duration encode/decode") {
check(encodeDecodeLaw[Duration], MinSuccessful(100))
}
property("NonEmptyList[Int] encode/decode") = encodeDecodeLaw[NonEmptyList[Int]]

property("Instant encode/decode") {
check(encodeDecodeLaw[Instant], MinSuccessful(100))
}
property("NonEmptyList[String] encode/decode") = encodeDecodeLaw[NonEmptyList[String]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import java.util.concurrent.TimeUnit
import org.scalacheck._

import scala.concurrent.duration.Duration
import scalaz._
import Scalaz._


object Generators {
Expand All @@ -25,6 +27,10 @@ object Generators {
path <- Gen.listOfN(length, genPathElement)
} yield new URL(protocol, host, port, path.mkString("/", "/", ""))

def genNel[A](g: Gen[A]): Gen[NonEmptyList[A]] = {
Gen.nonEmptyListOf(g).map(l => NonEmptyList.nel(l.head, l.tail.toIList))
}

val genDuration: Gen[Duration] =
Arbitrary.arbInt.arbitrary.map(i => Duration.create(i.toLong, TimeUnit.MILLISECONDS))

Expand All @@ -35,4 +41,6 @@ object Generators {
implicit val arbDuration: Arbitrary[Duration] = Arbitrary(genDuration)
implicit val arbUUID: Arbitrary[UUID] = Arbitrary(Gen.uuid)
implicit val arbInstant: Arbitrary[Instant] = Arbitrary(genInstant)
implicit val arbNelInt: Arbitrary[NonEmptyList[Int]] = Arbitrary(genNel(Gen.choose(Int.MinValue, Int.MaxValue)))
implicit val arbNelString: Arbitrary[NonEmptyList[String]] = Arbitrary(genNel(Gen.alphaStr))
}
6 changes: 3 additions & 3 deletions project/Depend.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ object Depend {
lazy val argonaut = Seq("io.argonaut" %% "argonaut" % argonautVersion)

lazy val scalaTestCheck = Seq(
"org.scalatest" %% "scalatest" % "2.2.4",
"org.scalacheck" %% "scalacheck" % "1.12.1"
).map(_.withSources).map(_ % "test")
"org.scalacheck" %% "scalacheck" % "1.12.5",
"org.scalacheck" %% "scalacheck" % "1.12.5" % "test"
).map(_.withSources)

lazy val depResolvers = Seq(
"Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases",
Expand Down

0 comments on commit 290978f

Please sign in to comment.