summaryrefslogtreecommitdiff
path: root/src/scalacheck/org/scalacheck/Properties.scala
blob: 7fceb4bd35436ccb66729e1125840ffb1f547454 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*-------------------------------------------------------------------------*\
**  ScalaCheck                                                             **
**  Copyright (c) 2007-2010 Rickard Nilsson. All rights reserved.          **
**  http://www.scalacheck.org                                              **
**                                                                         **
**  This software is released under the terms of the Revised BSD License.  **
**  There is NO WARRANTY. See the file LICENSE for the full text.          **
\*-------------------------------------------------------------------------*/

package org.scalacheck

/** Represents a collection of properties, with convenient methods
 *  for checking all properties at once. This class is itself a property, which
 *  holds if and only if all of the contained properties hold.
 *  <p>Properties are added in the following way:</p>
 *
 *  <p>
 *  <code>
 *  object MyProps extends Properties("MyProps") {
 *    property("myProp1") = forAll { (n:Int, m:Int) =&gt;
 *      n+m == m+n
 *    }
 *
 *    property("myProp2") = ((0/1) throws classOf[ArithmeticException])
 *  }
 */
class Properties(val name: String) extends Prop {

  import Test.cmdLineParser.{Success, NoSuccess}

  private val props = new scala.collection.mutable.ListBuffer[(String,Prop)]

  /** Returns one property which holds if and only if all of the
   *  properties in this property collection hold */
  private def oneProperty: Prop = Prop.all((properties map (_._2)):_*)

  /** Returns all properties of this collection in a list of name/property
   *  pairs.  */
  def properties: Seq[(String,Prop)] = props

  def apply(p: Prop.Params) = oneProperty(p)

  /** Convenience method that checks the properties with the given parameters
   *  and reports the result on the console. If you need to get the results
   *  from the test use the <code>check</code> methods in <code>Test</code>
   *  instead. */
  override def check(prms: Test.Params): Unit = Test.checkProperties(
    prms copy (testCallback = ConsoleReporter(1) chain prms.testCallback), this
  )

  /** Convenience method that checks the properties and reports the
   *  result on the console. If you need to get the results from the test use
   *  the <code>check</code> methods in <code>Test</code> instead. */
  override def check(): Unit = check(Test.Params())

  /** Convenience method that makes it possible to use a this instance
   *  as an application that checks itself on execution */
  override def main(args: Array[String]): Unit =
    Test.cmdLineParser.parseParams(args) match {
      case Success(params, _) => Test.checkProperties(params, this)
      case e: NoSuccess =>
        println("Incorrect options:"+"\n"+e+"\n")
        Test.cmdLineParser.printHelp
    }

  /** Adds all properties from another property collection to this one. */
  def include(ps: Properties) = for((n,p) <- ps.properties) property(n) = p

  /** Used for specifying properties. Usage:
   *  <code>property("myProp") = ...</code> */
  class PropertySpecifier() {
    def update(propName: String, p: Prop) = props += ((name+"."+propName, p))
  }

  lazy val property = new PropertySpecifier()
}