Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a specs matcher that will unbox Option and Either

Tags:

scala

bdd

specs

I've created a specs test, to validate some JSON parsing. Although the test works perfecly well, it feels rather noisy.

I wonder if there is existing code in Specifications to unbox Option and Either?

"twitter json to Scala class mapper" should {
    "parsing a tweet" in {
      TwitterJsonMapper.tweetP(tweetS) match {
        case Right(t: Tweet) => {
          implicit def unOption[T](t: Option[T]): T = t.get
          implicit def unEither[T](t: Either[T,Throwable]): T = t match {case Left(left) => left ;case Right(t) => throw t}
          "test id" in {
            true must_== (t.id.get == 228106060337135617l)
          }
          "test id_str" in {
            true must_== (t.id_str.get == "228106060337135617")
          }
          "test time" in {
            true must_== (t.created_at.getHours == 13 )
          }
        }
        case Left((pe: JsonParseException, reason: String)) => fail(reason + "\n" + pe)
      }
    }
  }

 //The Tweet is produced from JSON using Fasterxml's Jackson-Scala library. 
 //I want to use Option or Either monads over all child attributes - for the usual reasons.
case class Tweet(
  @BeanProperty contributors: Option[String],
  @BeanProperty coordinates: Option[String],

  @BeanProperty @JsonDeserialize (
      using = classOf[TwitterDateDeserializer]
  ) created_at: Either[Date,Throwable],
  @BeanProperty favorited: Boolean = false,
  //elided etc etc
  @BeanProperty id_str: Option[String]
}
like image 260
Bryan Hunt Avatar asked Jul 29 '12 11:07

Bryan Hunt


Video Answer


1 Answers

There are indeed some specific matchers for Option and Either:

t.id must beSome(228106060337135617l)
t.id_str must beSome("228106060337135617")
t.created_at.left.map(_.getHours) must beLeft(13)
like image 194
Eric Avatar answered Sep 18 '22 17:09

Eric