3
import org.specs2.mutable.Specification
7
case class OptionInt(i: Option[Int])
9
case class EitherIntString(i: Either[Int, String])
11
case class EitherStringInt(i: Either[String, Int])
13
case class EitherListIntListString(i: Either[List[String], List[Int]])
15
case class EitherListStringListInt(i: Either[List[Int], List[String]])
17
case class EitherListListStringMapStringInt(i: Either[List[List[String]], List[Map[String, List[Int]]]])
21
object JacksonEitherTest extends EitherTest[JValue]("Native") with jackson.JsonMethods
23
abstract class EitherTest[T](mod: String) extends Specification with JsonMethods[T] {
27
implicit val formats: Formats = DefaultFormats + ShortTypeHints(List(classOf[Either[_, _]], classOf[List[_]]))
30
(mod + " EitherTest Specification") should {
31
"See that it works for Option[Int]" in {
32
val opt = OptionInt(Some(39))
33
Extraction.decompose(opt).extract[OptionInt].i.get must_== 39
36
"Work for Either[Int, String]" in {
37
val opt = EitherIntString(Left(39))
38
Extraction.decompose(opt).extract[EitherIntString].i.left.get must_== 39
40
val opt2 = EitherIntString(Right("hello"))
41
Extraction.decompose(opt2).extract[EitherIntString].i.right.get must_== "hello"
44
"Work for Either[List[Int], List[String]]" in {
45
val opt = EitherListStringListInt(Left(List(1, 2, 3)))
46
Extraction.decompose(opt).extract[EitherListStringListInt].i.left.get must_== List(1, 2, 3)
48
val opt2 = EitherListStringListInt(Right(List("hello", "world")))
49
Extraction.decompose(opt2).extract[EitherListStringListInt].i.right.get must_== List("hello", "world")
52
"Work for Either[List[List[String]], List[Map[String, List[Int]]]]" in {
53
val opt = EitherListListStringMapStringInt(Left(List(List("a", "b", "c"), List("d", "e", "f"))))
54
Extraction.decompose(opt).extract[EitherListListStringMapStringInt].i.left.get must_== List(List("a", "b", "c"), List("d", "e", "f"))
56
val opt2 = EitherListListStringMapStringInt(Right(
58
Map("hello" -> List(5, 4, 3, 2, 1), "world" -> List(10, 20, 30)),
59
Map("bye" -> List(10), "world" -> List(10, 20, 30))
61
Extraction.decompose(opt2).extract[EitherListListStringMapStringInt].i.right.get must_== List(
62
Map("hello" -> List(5, 4, 3, 2, 1), "world" -> List(10, 20, 30)),
63
Map("bye" -> List(10), "world" -> List(10, 20, 30))