4.10.2015

Scala: Property-Based Testing with DateTime

Scala: DateTime型のプロパティテスト

Specs2 + ScalaCheck の例。

 

依存ライブラリ

libraryDependencies ++= Seq(
  "com.github.nscala-time" %% "nscala-time" % "1.8.0",
  "org.specs2" %% "specs2-core" % "3.4" % "test",
  "org.scalacheck" %% "scalacheck" % "1.12.2" % "test"
)

 

実装例

Arbitrary ベース
import com.github.nscala_time.time.Imports.DateTime
import org.scalacheck.{Gen, Arbitrary}
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification

class DateTimeSpec extends Specification with ScalaCheck {

  implicit val arbDateTime: Arbitrary[DateTime] =
    Arbitrary(Gen.choose(0L, Long.MaxValue).map(new DateTime(_)))

  "DateTime" should {
    "do something" in prop { t: DateTime =>
      t must ???  // write tests here
    }
  }
}

 

Gen ベース
import com.github.nscala_time.time.Imports.DateTime
import org.scalacheck.{Gen, Prop}
import org.specs2.ScalaCheck
import org.specs2.mutable.Specification

class DateTimeSpec extends Specification with ScalaCheck {

  private val genDateTime = Gen.choose(0L, Long.MaxValue).map(new DateTime(_))

  "DateTime" should {
    "do something" in Prop.forAll(genDateTime) { t: DateTime =>
      t must ???  // write tests here
    }
  }
}

 

Related Posts

0 件のコメント:

コメントを投稿