-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add a Scope trait to initialize variables in mutable specifi…
…cations
- Loading branch information
1 parent
75d1865
commit 38567f8
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
common/shared/src/main/scala/org/specs2/execute/Scope.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.specs2.execute | ||
|
||
/** This trait can be used in mutable specifications to provide setup values. For example: | ||
* ```scala | ||
* class MySpec extends mutable.Specification: | ||
* "e1" in new MyScope: | ||
* someValue === 1 | ||
* | ||
* trait MyScope extends Scope: val someValue: Int = 1 | ||
* ``` | ||
*/ | ||
trait Scope | ||
|
||
object Scope: | ||
/** This Given transforms a Scope to a Result */ | ||
given scopeAsResult[S <: Scope]: AsResult[S] = new AsResult[S]: | ||
def asResult(t: =>S): Result = AsResult.safely { Result.resultOrSuccess(t) } |
27 changes: 27 additions & 0 deletions
27
core/jvm/src/test/scala/org/specs2/mutable/ScopeSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.specs2.mutable | ||
|
||
import org.specs2.specification.core.Env | ||
|
||
class ScopeSpec(env: Env) extends org.specs2.Specification: | ||
def is = s2""" | ||
|
||
A mutable specification can use the Scope trait to initialize values for each example $useScopeTrait | ||
""" | ||
|
||
def useScopeTrait = | ||
// make sure that the arguments show all examples | ||
val arguments = org.specs2.main.Arguments("") | ||
val result = org.specs2.runner.TextRunner.run(new ScopeSpecification)(env.setArguments(arguments)).output | ||
(result must contain("+ This is an example which succeeds")) and | ||
(result must contain("x This is an example which fails")) | ||
|
||
class ScopeSpecification extends org.specs2.mutable.Specification { | ||
"This is an example which succeeds" in new SpecScope: | ||
value === 1 | ||
|
||
"This is an example which fails" in new SpecScope: | ||
value === 2 | ||
} | ||
|
||
trait SpecScope extends org.specs2.execute.Scope: | ||
val value: Int = 1 |