|
| 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 sbt.testing.{ |
| 13 | + Event, |
| 14 | + EventHandler, |
| 15 | + Framework, |
| 16 | + Selector, |
| 17 | + SuiteSelector, |
| 18 | + Task, |
| 19 | + TaskDef, |
| 20 | + TestSelector, |
| 21 | + TestWildcardSelector |
| 22 | +} |
| 23 | + |
| 24 | +object SbtFixture extends Properties("SbtFixture") { |
| 25 | + property("success") = Prop.passed |
| 26 | +} |
| 27 | + |
| 28 | +object SbtNestingFixture extends Properties("SbtNestingFixture") { |
| 29 | + include(SbtFixture) |
| 30 | +} |
| 31 | + |
| 32 | +object SbtSpecification extends Properties("Sbt") { |
| 33 | + property("suite") = run(Array(new SuiteSelector)) == List("SbtFixture.success") |
| 34 | + |
| 35 | + property("exact") = run(Array(new TestSelector("success"))) == List("SbtFixture.success") |
| 36 | + property("exactFull") = run(Array(new TestSelector("SbtFixture.success"))) == List("SbtFixture.success") |
| 37 | + property("exactMissing") = run(Array(new TestSelector("nonexistent"))) == List.empty |
| 38 | + |
| 39 | + property("wildcard") = run(Array(new TestWildcardSelector("succ"))) == List("SbtFixture.success") |
| 40 | + property("wildcardFull") = run(Array(new TestWildcardSelector("xture.succ"))) == List("SbtFixture.success") |
| 41 | + property("wildcardMissing") = run(Array(new TestWildcardSelector("prev"))) == List.empty |
| 42 | + |
| 43 | + property("nestedFull") = run( |
| 44 | + Array(new TestSelector("SbtNestingFixture.SbtFixture.success")), |
| 45 | + "org.scalacheck.SbtNestingFixture") == List("SbtNestingFixture.SbtFixture.success") |
| 46 | + |
| 47 | + property("nestedMedium") = run( |
| 48 | + Array(new TestSelector("SbtFixture.success")), |
| 49 | + "org.scalacheck.SbtNestingFixture") == List("SbtNestingFixture.SbtFixture.success") |
| 50 | + |
| 51 | + property("nestedShort") = run( |
| 52 | + Array(new TestSelector("success")), |
| 53 | + "org.scalacheck.SbtNestingFixture") == List("SbtNestingFixture.SbtFixture.success") |
| 54 | + |
| 55 | + // Since ScalaCheck does not keep track what class/object a property belongs to, |
| 56 | + // the following two issues concerning nested properties can not be fixed: |
| 57 | + // |
| 58 | + // When `explicitlySpecified = true`, properties from objects other than the one being run |
| 59 | + // should *not* run (and the outcome should be `List.empty`) - but they do. |
| 60 | + // |
| 61 | + // When a property from an object other than the one being run *does* run, |
| 62 | + // its status should be reported with a `NestedTestSelector` |
| 63 | + // where `suiteId` names the object that the property belongs to - |
| 64 | + // but it is reported with a `TestSelector`. |
| 65 | + |
| 66 | + property("nestedShouldRunAndDoes") = run( |
| 67 | + Array(new SuiteSelector), |
| 68 | + fullyQualifiedName = "org.scalacheck.SbtNestingFixture", |
| 69 | + explicitlySpecified = false |
| 70 | + ) == List("SbtNestingFixture.SbtFixture.success") |
| 71 | + |
| 72 | + property("nestedShouldNotRunButDoes") = run( |
| 73 | + Array(new SuiteSelector), |
| 74 | + fullyQualifiedName = "org.scalacheck.SbtNestingFixture", |
| 75 | + explicitlySpecified = true |
| 76 | + ) == List("SbtNestingFixture.SbtFixture.success") // should be List.empty |
| 77 | + |
| 78 | + // run using SBT Test Interface |
| 79 | + def run( |
| 80 | + selectors: Array[Selector], |
| 81 | + fullyQualifiedName: String = "org.scalacheck.SbtFixture", |
| 82 | + explicitlySpecified: Boolean = false |
| 83 | + ): List[String] = { |
| 84 | + val framework: Framework = new ScalaCheckFramework |
| 85 | + var ran: List[String] = List.empty |
| 86 | + val eventHandler: EventHandler = |
| 87 | + (event: Event) => ran = ran :+ event.selector().asInstanceOf[TestSelector].testName() |
| 88 | + def execute(tasks: Array[Task]): Unit = tasks.foreach(task => execute(task.execute(eventHandler, Array.empty))) |
| 89 | + execute(framework.runner(Array.empty, Array.empty, Platform.getClassLoader).tasks(Array(new TaskDef( |
| 90 | + fullyQualifiedName, |
| 91 | + framework.fingerprints()(2), // object ... extends org.scalacheck.Properties |
| 92 | + explicitlySpecified, |
| 93 | + selectors |
| 94 | + )))) |
| 95 | + ran |
| 96 | + } |
| 97 | +} |
0 commit comments