Skip to content

Commit 46dbc34

Browse files
committed
Verify only props matching the given TestSelectors
Previously, ScalaCheck ignored the selectors that it receives as input in the "root task". This prevented users from running only a subset of properties in a specification by passing their `TestSelector` to ScalaCheck in a `TaskDef`. This patch fixes this by having the root task program the execution of only the requested properties if the `TaskDef` contains only `TestSelector`s. ScalaCheck's behavior remains unchanged if the `TaskDef` contains any other kind of `Selector`.
1 parent ec41b4c commit 46dbc34

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

core/shared/src/main/scala/org/scalacheck/ScalaCheckFramework.scala

+9-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,14 @@ private abstract class ScalaCheckRunner extends Runner {
9292
}
9393

9494
def rootTask(td: TaskDef): BaseTask = new BaseTask(td) {
95-
def execute(handler: EventHandler, loggers: Array[Logger]): Array[Task] =
96-
props.map(_._1).toSet.toArray map { name =>
95+
def execute(handler: EventHandler, loggers: Array[Logger]): Array[Task] = {
96+
// If the task contains only `TestSelector`s, then run only these props instead of the whole suite.
97+
val propFilter: String => Boolean =
98+
if (td.selectors().forall(_.isInstanceOf[TestSelector]))
99+
td.selectors().collect { case ts: TestSelector => ts.testName() }.toSet
100+
else
101+
Function.const(true)
102+
props.map(_._1).toSet.filter(propFilter).toArray map { name =>
97103
checkPropTask(
98104
new TaskDef(
99105
td.fullyQualifiedName(),
@@ -102,6 +108,7 @@ private abstract class ScalaCheckRunner extends Runner {
102108
Array(new TestSelector(name))),
103109
single = true)
104110
}
111+
}
105112
}
106113

107114
def checkPropTask(taskDef: TaskDef, single: Boolean): BaseTask = new BaseTask(taskDef) { self =>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* ScalaCheck
3+
* Copyright (c) 2007-2021 Rickard Nilsson. All rights reserved.
4+
* http://www.scalacheck.org
5+
*
6+
* This software is released under the terms of the Revised BSD License.
7+
* There is NO WARRANTY. See the file LICENSE for the full text.
8+
*/
9+
10+
package org.scalacheck
11+
12+
import org.scalacheck.Prop.proved
13+
import sbt.testing.{Selector, SuiteSelector, TaskDef, TestSelector}
14+
15+
object ScalaCheckFrameworkSpecification extends Properties("ScalaCheckFramework") {
16+
17+
private val firstProp = "ScalaCheckFrameworkHelper.first prop"
18+
private val secondProp = "ScalaCheckFrameworkHelper.second prop"
19+
private val thirdProp = "ScalaCheckFrameworkHelper.third prop"
20+
21+
22+
property("all props with SuiteSelector") = {
23+
getPropNamesForSelectors(List(new SuiteSelector)) == List(firstProp, secondProp, thirdProp)
24+
getPropNamesForSelectors(List(new SuiteSelector, new TestSelector(firstProp))) == List(firstProp, secondProp, thirdProp)
25+
getPropNamesForSelectors(List(new SuiteSelector, new TestSelector("no matches"))) == List(firstProp, secondProp, thirdProp)
26+
}
27+
28+
property("only matching props with TestSelector") = {
29+
getPropNamesForSelectors(List(new TestSelector(firstProp))) == List(firstProp)
30+
getPropNamesForSelectors(List(new TestSelector(secondProp))) == List(secondProp)
31+
getPropNamesForSelectors(List(new TestSelector(firstProp), new TestSelector(thirdProp))) == List(firstProp, thirdProp)
32+
getPropNamesForSelectors(List(new TestSelector("no matches"))) == Nil
33+
}
34+
35+
private def getPropNamesForSelectors(selectors: List[Selector]): List[String] = {
36+
val framework = new ScalaCheckFramework()
37+
val runner = framework.runner(Array.empty, Array.empty, getClass.getClassLoader).asInstanceOf[ScalaCheckRunner]
38+
val taskDef = new TaskDef(classOf[ScalaCheckFrameworkSpecificationHelper].getName, framework.fingerprints()(0), true, selectors.toArray)
39+
val baseTask = runner.rootTask(taskDef)
40+
val newTasks = baseTask.execute(null, null)
41+
val propNames = for {
42+
task <- newTasks
43+
selector <- task.taskDef().selectors()
44+
} yield selector.asInstanceOf[TestSelector].testName()
45+
propNames.toList
46+
}
47+
}
48+
49+
class ScalaCheckFrameworkSpecificationHelper extends Properties("ScalaCheckFrameworkHelper") {
50+
property("first prop") = proved
51+
property("second prop") = proved
52+
property("third prop") = proved
53+
}

0 commit comments

Comments
 (0)