Skip to content

Commit

Permalink
Merge pull request #51 from tkrs/add-ccons-test
Browse files Browse the repository at this point in the history
Add ccons test
  • Loading branch information
tkrs committed Oct 25, 2018
2 parents a18c200 + 9330792 commit 4bdf922
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Expand Up @@ -45,19 +45,6 @@ private[mess] trait LowPriorityDerivedDecoder {
Left(TypeMismatchError("CNil", m))
}

implicit final def decodeCCons[L, R <: Coproduct](implicit
L: Decoder[L],
R: DerivedDecoder[R]): DerivedDecoder[L :+: R] = {
new DerivedDecoder[L :+: R] {
override def apply(m: MsgPack): Decoder.Result[L :+: R] = {
L.map(Inl(_)).apply(m) match {
case r @ Right(_) => r
case Left(_) => R.map(Inr(_)).apply(m)
}
}
}
}

implicit final def decodeLabelledCCons[K <: Symbol, L, R <: Coproduct](
implicit
K: Witness.Aux[K],
Expand Down
14 changes: 13 additions & 1 deletion modules/core/src/test/scala/mess/DecoderSpec.scala
Expand Up @@ -8,9 +8,12 @@ class DecoderSpec extends FunSuite with MsgpackHelper {

case class Bar(double: Double)
case class Foo(int: Int, str: String, bar: Bar)

case class Qux(byte: Option[Int])

sealed trait Z
case class Z0(a: Int) extends Z
case class Z1(b: Int) extends Z

def decode[A](msg: MsgPack)(implicit A: Decoder[A]): Either[Throwable, A] = A(msg)

def check[A: Decoder](tc: Seq[(A, MsgPack)]): Unit = {
Expand Down Expand Up @@ -235,6 +238,15 @@ class DecoderSpec extends FunSuite with MsgpackHelper {
}
}

test("Decoder[Z]") {
check {
Seq[(Z, MsgPack)](
(Z0(1), MsgPack.mMap(MsgPack.mStr("Z0") -> MsgPack.mMap(MsgPack.mStr("a") -> MsgPack.mByte(1.toByte)))),
(Z1(2), MsgPack.mMap(MsgPack.mStr("Z1") -> MsgPack.mMap(MsgPack.mStr("b") -> MsgPack.mByte(2.toByte))))
)
}
}

test("Decoder[Qux] should return TypeMismatchError when its type conversion is failed") {
assert(decode[Qux](MsgPack.mStr(" ")) == Left(TypeMismatchError("FieldType[K, H] :: T", MsgPack.MString(" "))))
}
Expand Down

0 comments on commit 4bdf922

Please sign in to comment.