0

我正在尝试 ScalaCheck,但无法弄清楚如何在 ScalaTest 用户手册上构建第一个示例。我很确定以下内容之前需要一些导入并包装在从某个库类扩展的类中。用户手册中的页面说要“混入” PropertyChecks,但它没有给出适当类的示例以将其混入:

forAll { (n: Int, d: Int) =>

  whenever (d != 0 && d != Integer.MIN_VALUE
      && n != Integer.MIN_VALUE) {

    val f = new Fraction(n, d)

    if (n < 0 && d < 0 || n > 0 && d > 0)
      f.numer should be > 0
    else if (n != 0)
      f.numer should be < 0
    else
      f.numer should be === 0

    f.denom should be > 0
  }
}

我一直在尝试各种组合,但到目前为止我得到的最好结果是这样的编译错误:

[info] Compiling 1 Scala source to .../target/scala-2.11/test-classes...
[error] .../src/test/scala/TestFraction.scala:14: value should is not a member of Int
[error]         f.numer should be > 0
[error]                 ^

什么是测试、导入和所有的完整源文件?

4

2 回答 2

1

如果你在这里查看 scala test 的 github 存储库,你可以在这里找到一些额外的代码

github 源码 scalatest

对于您的来源,这是导入和结构

import org.scalatest.{Matchers, FlatSpec}
import org.scalatest.prop.PropertyChecks

class Fraction(n: Int, d: Int) {

  require(d != 0)
  require(d != Integer.MIN_VALUE)
  require(n != Integer.MIN_VALUE)

  val numer = if (d < 0) -1 * n else n
  val denom = d.abs

  override def toString = numer + " / " + denom
}

class PropertySpec extends FlatSpec with PropertyChecks with Matchers {
  forAll { (n: Int, d: Int) =>

    whenever(d != 0 && d != Integer.MIN_VALUE
      && n != Integer.MIN_VALUE) {

      val f = new Fraction(n, d)

      if (n < 0 && d < 0 || n > 0 && d > 0)
        f.numer should be > 0
      else if (n != 0)
        f.numer should be < 0
      else
        f.numer should be === 0

      f.denom should be > 0
    }
  }

  val invalidCombos =
    Table(
      ("n", "d"),
      (Integer.MIN_VALUE, Integer.MIN_VALUE),
      (1, Integer.MIN_VALUE),
      (Integer.MIN_VALUE, 1),
      (Integer.MIN_VALUE, 0),
      (1, 0)
    )

  forAll(invalidCombos) { (n: Int, d: Int) =>
    evaluating {
      new Fraction(n, d)
    } should produce[IllegalArgumentException]
  }
}
于 2015-05-10T14:09:45.263 回答
1

scalatest 的最新版本未实现evaluating并标记===为已过时。因此,我将示例重写为:

import org.scalatest._
import org.scalatest.prop.PropertyChecks

protected class Fraction(n: Int, d: Int) {
  require(d != 0 && d != Integer.MIN_VALUE && n != Integer.MIN_VALUE)
  val numer = if (d < 0) -1 * n else n
  val denom = d.abs
  override def toString = numer + " / " + denom
}

class SscceProps extends FlatSpec with PropertyChecks with Matchers {
  it should "valid combos" in { forAll { (n: Int, d: Int) =>
      whenever(d != 0 && d != Integer.MIN_VALUE && n != Integer.MIN_VALUE) {
        val f = new Fraction(n, d)
        if (n < 0 && d < 0 || n > 0 && d > 0) f.numer should be > 0
        else if (n != 0) f.numer should be < 0
        else f.numer shouldBe 0
        f.denom should be > 0
      }
  }}
  it should "invalid combos" in {
    forAll(Table(
      ("n", "d"),
      (Integer.MIN_VALUE, Integer.MIN_VALUE),
      (1, Integer.MIN_VALUE),
      (Integer.MIN_VALUE, 1),
      (Integer.MIN_VALUE, 0),
      (1, 0)
    )) { (n: Int, d: Int) =>
      an[IllegalArgumentException] should be thrownBy {new Fraction(n, d)}}
  }
}

于 2020-01-17T13:31:15.253 回答