Skip to content

Instantly share code, notes, and snippets.

@stefanobaghino
Created February 17, 2017 12:43
Show Gist options
  • Save stefanobaghino/4ed2a96bda1e1468858c58232e095ef0 to your computer and use it in GitHub Desktop.
Save stefanobaghino/4ed2a96bda1e1468858c58232e095ef0 to your computer and use it in GitHub Desktop.
Extend ScalaTest DSL using the Containing type class defined over Joda-Time types.
import org.joda.time._
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.enablers.Containing
import ExampleTimeSpec._
object ExampleTimeSpec {
implicit val containing: Containing[ReadableInterval] = new Containing[ReadableInterval] {
override def containsOneOf(container: ReadableInterval, elements: Seq[Any]): Boolean =
elements.exists(contains(container, _))
override def contains(container: ReadableInterval, element: Any): Boolean =
element match {
case i: ReadableInstant => container.contains(i)
case i: ReadableInterval => container.contains(i)
case _ => false
}
override def containsNoneOf(container: ReadableInterval, elements: Seq[Any]): Boolean =
!containsOneOf(container, elements)
}
}
class ExampleTimeSpec extends FlatSpec with Matchers {
behavior of "0 to 10"
val `0 to 10` = new Interval(0, 10)
it should "contain 5" in {
`0 to 10` should contain(new Instant(5))
}
it should "not contain -5" in {
`0 to 10` should not contain new Instant(-5)
}
it should "contain one of (-5, 5)" in {
`0 to 10` should contain oneOf (new Instant(-5), new Instant(5))
}
it should "not contain one of (-5, -4)" in {
`0 to 10` should not contain oneOf(new Instant(-5), new Instant(-4))
}
it should "contain none of (-5, -4)" in {
`0 to 10` should contain noneOf (new Instant(-5), new Instant(-4))
}
it should "not contain none of (-5, 5)" in {
`0 to 10` should not contain noneOf(new Instant(-5), new Instant(5))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment