org.scalatest.prop.PropertyChecks.scala Maven / Gradle / Ivy
Show all versions of scalatest_2.11.0-M3 Show documentation
/*
* Copyright 2001-2011 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalatest
package prop
/**
* Trait that facilitates property checks on data supplied by tables and generators.
*
*
* This trait extends both TableDrivenPropertyChecks
and
* GeneratorDrivenPropertyChecks
. Thus by mixing in
* this trait you can perform property checks on data supplied either by tables or generators. For the details of
* table- and generator-driven property checks, see the documentation for each by following the links above.
*
*
*
* For a quick example of using both table and generator-driven property checks in the same suite of tests, however,
* imagine you want to test this Fraction,
class:
*
*
*
* 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
* }
*
*
*
* If you mix in PropertyChecks
, you could use a generator-driven property check to test that the passed values for numerator and
* denominator are properly normalized, like this:
*
*
*
* 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
* }
* }
*
*
*
* And you could use a table-driven property check to test that all combinations of invalid values passed to the Fraction
constructor
* produce the expected IllegalArgumentException
, like this:
*
*
*
* 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]
* }
*
*
* @author Bill Venners
*/
trait PropertyChecks extends TableDrivenPropertyChecks with GeneratorDrivenPropertyChecks
/**
* Companion object that facilitates the importing of PropertyChecks
members as
* an alternative to mixing it in. One use case is to import PropertyChecks
members so you can use
* them in the Scala interpreter.
*
* @author Bill Venners
*/
object PropertyChecks extends PropertyChecks