|
| 1 | +package org.specs2 |
| 2 | +package runner |
| 3 | + |
| 4 | +import sbt.testing.{Event, EventHandler, Logger, Selector, Status, SuiteSelector, TaskDef, TestSelector} |
| 5 | + |
| 6 | +import scala.collection.mutable.ArrayBuffer |
| 7 | + |
| 8 | +class SbtSelectorSpec extends Specification: |
| 9 | + def is = s2""" |
| 10 | + |
| 11 | + An sbt runner executes the examples passed in the `TestSelector`s |
| 12 | + when there is a single `TestSelector` $singleTestSelector |
| 13 | + when there are 2 `TestSelector`s $twoTestSelectors |
| 14 | + run everything if there are other selectors $otherSelectors |
| 15 | + run nothing if there are no matches $noMatches |
| 16 | + regexes in test selectors are escaped $regexesAreEscaped |
| 17 | +""" |
| 18 | + |
| 19 | + private def singleTestSelector = |
| 20 | + val wholeSuiteEvents = runWithSelectors(new SuiteSelector :: Nil) |
| 21 | + wholeSuiteEvents.length === 3 |
| 22 | + val failEvents = wholeSuiteEvents.filter(_.status == Status.Failure) |
| 23 | + val singleExampleEvents = runWithSelectors(failEvents.map(_.selector())) |
| 24 | + |
| 25 | + (failEvents must haveSize(1)) and |
| 26 | + (testName(failEvents.head) === "has a failing test") and |
| 27 | + (singleExampleEvents must haveSize(1)) and |
| 28 | + (singleExampleEvents.head.status() === Status.Failure) |
| 29 | + |
| 30 | + private def twoTestSelectors = |
| 31 | + val events = runWithSelectors( |
| 32 | + List(new TestSelector("has a successful test"), new TestSelector("has a failing test")) |
| 33 | + ) |
| 34 | + (events must haveSize(2)) and |
| 35 | + (events.map(testName) must contain("has a successful test", "has a failing test")) |
| 36 | + |
| 37 | + private def otherSelectors = |
| 38 | + val events = runWithSelectors(List(new SuiteSelector, new TestSelector("hello"))) |
| 39 | + (events must haveSize(3)) and |
| 40 | + (events.map(testName) must contain("has a successful test", "has a failing test", ".*")) |
| 41 | + |
| 42 | + private def noMatches = |
| 43 | + val events = runWithSelectors(List(new TestSelector("won't match anything"))) |
| 44 | + events must beEmpty |
| 45 | + |
| 46 | + private def regexesAreEscaped = |
| 47 | + val events = runWithSelectors(new TestSelector(".*") :: Nil) |
| 48 | + (events must haveSize(1)) and |
| 49 | + (events.head.status() === Status.Success) and |
| 50 | + (testName(events.head) === ".*") |
| 51 | + |
| 52 | + private def runWithSelectors(selectors: List[Selector]): List[Event] = |
| 53 | + val loggers = Array(NoLogger: Logger) |
| 54 | + val events = ArrayBuffer.empty[Event] |
| 55 | + val handler: EventHandler = (e: Event) => events.append(e) |
| 56 | + val framework = new Specs2Framework() |
| 57 | + val runner = framework.runner(Array.empty, Array.empty, getClass.getClassLoader) |
| 58 | + val fqcn = classOf[HelperSpec].getName |
| 59 | + |
| 60 | + val taskDef = new TaskDef(fqcn, Fingerprints.fp1m, true, selectors.toArray) |
| 61 | + val tasks = runner.tasks(Array(taskDef)) |
| 62 | + tasks.foreach(_.execute(handler, loggers)) |
| 63 | + |
| 64 | + events.toList |
| 65 | + |
| 66 | + private def testName(event: Event): String = |
| 67 | + event.selector() match |
| 68 | + case ts: TestSelector => ts.testName() |
| 69 | + |
| 70 | +private class HelperSpec extends Specification: |
| 71 | + def is = s2""" |
| 72 | + The helper spec |
| 73 | + has a successful test $ok |
| 74 | + has a failing test $ko |
| 75 | + .* $ok |
| 76 | +""" |
| 77 | + |
| 78 | +private object NoLogger extends Logger: |
| 79 | + override def ansiCodesSupported(): Boolean = false |
| 80 | + override def error(msg: String): Unit = () |
| 81 | + override def warn(msg: String): Unit = () |
| 82 | + override def info(msg: String): Unit = () |
| 83 | + override def debug(msg: String): Unit = () |
| 84 | + override def trace(t: Throwable): Unit = () |
0 commit comments