How ScalaTest DSL works (simplified version) - grant-guo/Ideas GitHub Wiki

With ScalaTest, we usually write the test cases like this

"subject" should "verb" in {
  ... ...
}

How this DSL is executed is the topic of this wiki page

we can identify should and in are the key words, which are represented by Scala functions, classes or structures.

At first, let's see how "subject" should work together. By searching the source code, I found the following implicit function in Matchers.scala

implicit def convertToStringShouldWrapper(o: String)(implicit pos: source.Position, prettifier: Prettifier): StringShouldWrapper = new StringShouldWrapper(o, pos, prettifier)

This function implicitly converts an String to a StringShouldWrapper object. object Position defines an implicit function called here, and object Prettifier defines an implicit object called default

The class StringShouldWrapper mixin the trait ShouldVerb, which has the following function

def should(right: String)(implicit svsi: StringVerbStringInvocation): ResultOfStringPassedToVerb

FlatSpecLike defines an implicit StringVerbStringInvocation object called shorthandTestRegistrationFunction

Function in is defined in protected final class InAndIgnoreMethods(resultOfStringPassedToVerb: ResultOfStringPassedToVerb) within the trait FlatSpecLike

Also in FlatSpecLike, there is an implicit function as the following

protected implicit def convertToInAndIgnoreMethods(resultOfStringPassedToVerb: ResultOfStringPassedToVerb): InAndIgnoreMethods =
    new InAndIgnoreMethods(resultOfStringPassedToVerb)