【发布时间】:2012-07-27 09:03:43
【问题描述】:
我创建了一个规范测试,以验证一些 JSON 解析。虽然测试效果非常好,但感觉相当嘈杂。
我想知道 Specifications 中是否有现有代码可以将 Option 和 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]
}
【问题讨论】:
-
是的,请参阅匹配器指南:etorreborre.github.com/specs2/guide/…
-
啊,好的。我忘了说,我还在 Specs 1 上。只有这么多时间花在升级我的库上。该功能不在 Specs 1 中吗?
-
很难获得可靠的 scala / maven / eclipse / specs - 工具链 - 启动并运行。我获得了使用 Eclipse 的规范,并冻结了我的依赖项的那部分。
-
您在升级过程中遇到过什么问题吗?
-
我是从 specs2 开始的,所以我无法对迁移发表评论。由于社区变化的步伐,我虔诚地升级了我的 Scala 项目。我发现 Gradle 优于 SBT/Maven,已经广泛使用。对于 specs1 匹配器,请参阅code.google.com/p/specs/wiki/MatchersGuide