~ubuntu-branches/debian/experimental/json4s/experimental

« back to all changes in this revision

Viewing changes to tests/src/test/scala/org/json4s/Either.scala

  • Committer: Package Import Robot
  • Author(s): Frédéric Bonnard
  • Date: 2017-05-24 16:23:52 UTC
  • Revision ID: package-import@ubuntu.com-20170524162352-k7k8cj8u88wupw4b
Tags: upstream-3.5.0
Import upstream version 3.5.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.json4s
 
2
 
 
3
import org.specs2.mutable.Specification
 
4
 
 
5
object EitherTest {
 
6
 
 
7
  case class OptionInt(i: Option[Int])
 
8
 
 
9
  case class EitherIntString(i: Either[Int, String])
 
10
 
 
11
  case class EitherStringInt(i: Either[String, Int])
 
12
 
 
13
  case class EitherListIntListString(i: Either[List[String], List[Int]])
 
14
 
 
15
  case class EitherListStringListInt(i: Either[List[Int], List[String]])
 
16
 
 
17
  case class EitherListListStringMapStringInt(i: Either[List[List[String]], List[Map[String, List[Int]]]])
 
18
 
 
19
}
 
20
 
 
21
object JacksonEitherTest extends EitherTest[JValue]("Native") with jackson.JsonMethods
 
22
 
 
23
abstract class EitherTest[T](mod: String) extends Specification with JsonMethods[T] {
 
24
 
 
25
  import EitherTest._
 
26
 
 
27
  implicit val formats: Formats = DefaultFormats + ShortTypeHints(List(classOf[Either[_, _]], classOf[List[_]]))
 
28
 
 
29
 
 
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
 
34
    }
 
35
 
 
36
    "Work for Either[Int, String]" in {
 
37
      val opt = EitherIntString(Left(39))
 
38
      Extraction.decompose(opt).extract[EitherIntString].i.left.get must_== 39
 
39
 
 
40
      val opt2 = EitherIntString(Right("hello"))
 
41
      Extraction.decompose(opt2).extract[EitherIntString].i.right.get must_== "hello"
 
42
    }
 
43
 
 
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)
 
47
 
 
48
      val opt2 = EitherListStringListInt(Right(List("hello", "world")))
 
49
      Extraction.decompose(opt2).extract[EitherListStringListInt].i.right.get must_== List("hello", "world")
 
50
    }
 
51
 
 
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"))
 
55
 
 
56
      val opt2 = EitherListListStringMapStringInt(Right(
 
57
        List(
 
58
          Map("hello" -> List(5, 4, 3, 2, 1), "world" -> List(10, 20, 30)),
 
59
          Map("bye" -> List(10), "world" -> List(10, 20, 30))
 
60
        )))
 
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))
 
64
      )
 
65
    }
 
66
  }
 
67
}