Skip to content

Commit

Permalink
add tests for property filters
Browse files Browse the repository at this point in the history
  • Loading branch information
ssanj committed Sep 28, 2016
1 parent 3d5d030 commit af16e6c
Showing 1 changed file with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*-------------------------------------------------------------------------*\
** ScalaCheck **
** Copyright (c) 2007-2016 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

import Prop.BooleanOperators

object PropertyFilterSampleSpecification extends Properties("PropertyFilterSample") {

property("positive numbers") = Prop.forAll(Gen.posNum[Int]) { n => n > 0 }

property("negative numbers") = Prop.forAll(Gen.negNum[Int]) { n => n < 0 }

property("lowercase alpha characters") = Prop.forAll(Gen.alphaLowerChar) { c =>
c.toInt >= 97 && c.toInt <= 122
}
}

object PropertyFilterSpecification extends Properties("PropertyFilter") {

val nl = System.lineSeparator

private def diff(filter: Option[String], actual: Seq[String],
expected: Seq[String]): String = {
s"filter: ${filter.getOrElse("not supplied")}" +
s"${nl}expected values:$nl" +
s"\t${expected.mkString(s"$nl\t")}" +
s"${nl}actual values:$nl" +
s"\t${actual.mkString(s"$nl\t")}"
}

private def prop(filter: Option[String], actualNames: Seq[String],
expectedNames: Seq[String]): Prop = {
def lengthProp = actualNames.length == expectedNames.length

def props = actualNames.forall(expectedNames.contains)

(lengthProp && props) :| diff(filter, actualNames, expectedNames)
}

property("filter properties by predicate") =
Prop.forAllNoShrink(
Gen.option(
Gen.oneOf(
"PropertyFilterSample.*numbers",
"PropertyFilterSample.*alpha"))) { pf =>

val testParams =
Test.Parameters.default.
withPropFilter(pf)

val props =
Test.checkProperties(
testParams,
PropertyFilterSampleSpecification
)

val propNames = props.map(_._1)

if (pf.exists(_.contains("*numbers"))) {
val expected =
Seq(
"PropertyFilterSample.positive numbers",
"PropertyFilterSample.negative numbers"
)

prop(pf, propNames, expected)
} else if (pf.exists(_.contains("*alpha"))) {
val expected = Seq("PropertyFilterSample.lowercase alpha characters")

prop(pf, propNames, expected)
} else { //no filter
val expected = Seq(
"PropertyFilterSample.positive numbers",
"PropertyFilterSample.negative numbers",
"PropertyFilterSample.lowercase alpha characters"
)

prop(pf, propNames, expected)
}
}
}

0 comments on commit af16e6c

Please sign in to comment.