-
-
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.
#685 refactored the BlockDsl trait to allow the easy definition
of other keywords than "can", "should",...
- Loading branch information
1 parent
c9815ec
commit 2fdbc76
Showing
4 changed files
with
82 additions
and
12 deletions.
There are no files selected for viewing
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
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
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,70 @@ | ||
package org.specs2.guide | ||
|
||
object AddKeywords extends UserGuidePage { def is = s2""" | ||
|
||
Mutable specifications offer a predefined "vocabulary" to define examples: ${snippet{ | ||
import org.specs2._ | ||
|
||
class MySpecification extends mutable.Specification { | ||
|
||
"the and function" should { | ||
"return true when passed true, true" >> { | ||
true && true ==== true | ||
} | ||
"return false when passed true, false" >> { | ||
true && false ==== false | ||
} | ||
} | ||
|
||
} | ||
}} | ||
|
||
This will print: | ||
``` | ||
the and function should | ||
+ return true when passed true, true | ||
+ return false when passed true, false | ||
``` | ||
And you can see that the word "should" has been added to the first description. | ||
|
||
However one size does not fit all and you might want to add your own predefined words. Here is how to do it: ${snippet{ | ||
import org.specs2._ | ||
import org.specs2.specification.core.{Fragment, Fragments} | ||
import org.specs2.specification.dsl.mutable._ | ||
import org.specs2.control.ImplicitParameters | ||
|
||
trait ToKeyword extends BlockDsl { | ||
implicit class DescribeTo(description: String) { | ||
def to(f: =>Fragment): Fragment = | ||
addFragmentBlockWithText(description + " to", f) | ||
|
||
// this implementation of `to` uses an implicit parameter. This is used to overload | ||
// the method for different arguments: Fragment and Fragments | ||
def to(fs: =>Fragments)(implicit p1: ImplicitParameters.ImplicitParam1): Fragments = | ||
addFragmentsBlockWithText(description + " to", fs) | ||
} | ||
} | ||
|
||
class MySpecification extends mutable.Specification with ToKeyword { | ||
|
||
"the and function is used" to { | ||
"return true when passed true, true" >> { | ||
true && true ==== true | ||
} | ||
"return false when passed true, false" >> { | ||
true && false ==== false | ||
} | ||
} | ||
} | ||
}} | ||
|
||
Now this will print | ||
``` | ||
the and is use to | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
etorreborre
Author
Owner
|
||
+ return true when passed true, true | ||
+ return false when passed true, false | ||
``` | ||
|
||
""" | ||
|
||
} |
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
typo: should be the and function is used to