【问题标题】:How write a property test for particular list content如何为特定列表内容编写属性测试
【发布时间】:2017-11-08 09:54:13
【问题描述】:

我有以下功能,我想用 ScalaCheck 测试它:

object Windows {

  val Directory = "^[a-zA-Z]:\\\\(((?![<>:\"/\\\\|?*]).)+((?<![ .])\\\\)?)*$".r


  def arePathsValid(paths: List[String]): Eval[List[String]] = {
    Foldable[List]
      .foldRight(paths, Eval.later(List.empty[String]))((a: String, b: Eval[List[String]]) => {
        Directory.findFirstIn(a) match {
          case Some(a) => b.map(a :: _)
          case None => b
        }
      })
  }
}

我试着从:

val propPaths = forAll { l: List[String] => ??? }

但无法编写该属性的实现。

应该在List中随机生成的String应该有一个Windows模式路径,例如:

C:\temp\foo

如何进行属性实现?

【问题讨论】:

    标签: scala scalacheck property-testing


    【解决方案1】:

    您可以像这样添加 Windows 路径前缀:

    val strGen = Gen.alphaStr // Or any other String generator
    val windowsPathGen = strGen.map("C:\temp\foo" + _)
    

    【讨论】:

    • 如何将创建的Generator传递给属性,例如forAll
    • 你可以像这样创建属性: property("check") = forAll(windowsPathGen) { item => item.startsWith("C:\temp\foo") }
    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 2019-10-29
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多