File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
common/shared/src/main/scala/org/specs2/execute
core/jvm/src/test/scala/org/specs2/mutable Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ package org .specs2 .execute
2+
3+ /** This trait can be used in mutable specifications to provide setup values. For example:
4+ * ```scala
5+ * class MySpec extends mutable.Specification:
6+ * "e1" in new MyScope:
7+ * someValue === 1
8+ *
9+ * trait MyScope extends Scope: val someValue: Int = 1
10+ * ```
11+ */
12+ trait Scope
13+
14+ object Scope :
15+ /** This Given transforms a Scope to a Result */
16+ given scopeAsResult [S <: Scope ]: AsResult [S ] = new AsResult [S ]:
17+ def asResult (t : => S ): Result = AsResult .safely { Result .resultOrSuccess(t) }
Original file line number Diff line number Diff line change 1+ package org .specs2 .mutable
2+
3+ import org .specs2 .specification .core .Env
4+
5+ class ScopeSpec (env : Env ) extends org.specs2.Specification :
6+ def is = s2 """
7+
8+ A mutable specification can use the Scope trait to initialize values for each example $useScopeTrait
9+ """
10+
11+ def useScopeTrait =
12+ // make sure that the arguments show all examples
13+ val arguments = org.specs2.main.Arguments (" " )
14+ val result = org.specs2.runner.TextRunner .run(new ScopeSpecification )(env.setArguments(arguments)).output
15+ (result must contain(" + This is an example which succeeds" )) and
16+ (result must contain(" x This is an example which fails" ))
17+
18+ class ScopeSpecification extends org.specs2.mutable.Specification {
19+ " This is an example which succeeds" in new SpecScope :
20+ value === 1
21+
22+ " This is an example which fails" in new SpecScope :
23+ value === 2
24+ }
25+
26+ trait SpecScope extends org.specs2.execute.Scope :
27+ val value : Int = 1
You can’t perform that action at this time.
0 commit comments