import org.specs2._
import runner._
import control._
import specification.core._
class MySuite extends Specification { def is = s2"""
My specifications
${ SpecificationsFinder.findSpecifications(glob = "**/*Spec.scala").runOption.toList.flatten.foldLeft(Fragments.empty) {
(res, s) => res ^ br ^ link(s)
}
}
"""
}
import org.specs2._
import runner._
import control._
import specification.core._
class MySuperSuite extends Specification {
val someResult = <some code here>
Try {
val mySpecStructure =
s2"""
My specifications
${
SpecificationsFinder.findSpecifications(glob = "**/*Spec.scala").runOption.toList.flatten.foldLeft(Fragments.empty) {
(res, s) => res ^ br ^ link(s)
}
}
"""
<code that runs 'mySpecStructure' and returns some result, even if it's only boolean>
} match {
case Success(testResults) => <run more of my code using 'someResult' and 'testResults'>
case Failure(e) => <run some other code>
}
}
Is there an explicit way of running the specification structure manually from my code?
I seem to have a gap in knowledge in using the created specification structure and cannot find any good source (almost nothing is written about this) of examples.
Thanks!
abstract class ScalaTestSimulationSuite extends Suite with SimulationSuite {
lazy val suits = {
val reflections = new Reflections(package2Scan, new TypeAnnotationsScanner())
val tests: Set[Class[_]] = reflections.getTypesAnnotatedWith(testAnnotation).toSet
tests.map { testClass =>val ctor = testClass.getDeclaredConstructor()}
ctor.newInstance().asInstanceOf[Suite]
} toIndexedSeq
override def nestedSuites: IndexedSeq[Suite] = suits
override def run(testName: Option[String], args: Args): Status = {
val beforeVersion = getVersion
val status = Try {
super.run(testName, args)
}
val afterVersion = getVersion
if (beforeVersion != afterVersion || beforeVersion.isEmpty) {
handleVersionsMissMatch(beforeVersion, afterVersion)
} else {
// Write some logs and do something else.
}
status.get
}
}
import org.specs2._
import runner._
import control._
import specification.core._
import specification.process._
import scalaz._, Scalaz._
class MySuite(env: Env) extends Specification { def is = s2"""
Run all the specifications
${run(specifications)}
"""
def run(specs: List[SpecificationStructure]): Fragments = {
specs.foldLeft(Fragments.empty.right[Fragments]) {
case (\/-(res), spec) =>
val previousVersion = getVersion
val stats = ClassRunner.report(env)(spec).runOption.getOrElse(Stats.empty)
val newVersion = getVersion
if (previousVersion != newVersion)
\/.left(res ^ br ^ handleVersionsMissMatch(previousVersion, newVersion))
else
\/.right(res ^ br ^ logStuff(stats))
case (res, _) => res
}.fold(identity _, identity _)
}
def specifications: List[SpecificationStructure] =
SpecificationsFinder.findSpecifications(glob = "**/*Spec.scala").runOption.toList.flatten
def handleVersionsMissMatch(previousVersion: Version, newVersion: Version): Fragments =
s2"""
There was a new version, stopping everything !!!! $previousVersion != $newVersion
"""
def logStuff(stats: Stats): Fragments =
s2"""
some logging here
The stats are ${stats.toString}
"""
type Version = String
def getVersion: Version =
"current version"
}