scala - scalatest "A stack" should "do something" -- wtf? How is should a method of string? -
i'm starting out scala here , i'm finding of syntax confusing. example scalatest main page
class examplespec extends flatspec matchers { "a stack" should "pop values in last-in-first-out order" in {...} }
as read means "should" method of string "a stack"? if right, how happen? doesn't seem work scala prompt
scala> class examplespec { | "a stack" should "pop values" | } <console>:9: error: value should not member of string "a stack" should "pop values"
if "should" not method of string "a stack" in above snippet how read snippet correctly? "should" , how relate string? clues?
this commonly known pimp library enrich library pattern can extend other libraries (in case scala strings) our own methods using implicit conversions.
the way works flatspec
mixes in trait called shouldverb
has following implicit conversion defined:
implicit def converttostringshouldwrapper(o: string): stringshouldwrapperforverb = new stringshouldwrapperforverb { val leftsidestring = o.trim }
and stringshouldwrapperforverb
has should
method defined:
def should(right: string) ....
the reason not work in repl you don't have flatspec
, via shouldverb
trait mixed in.
you can read more in documentation under implicit classes.
Comments
Post a Comment