Skip to content

Commit 38567f8

Browse files
committed
feature: add a Scope trait to initialize variables in mutable specifications
1 parent 75d1865 commit 38567f8

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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) }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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

0 commit comments

Comments
 (0)