Q:

Understanding inner classes in trait

I'm pretty new to Scala and now I'm trying to learn how to write tests in Scalatest. Here is an example they provided with the using custom matchers section:
import org.scalatest._
import matchers._

trait CustomMatchers {

    class FileEndsWithExtensionMatcher(expectedExtension: String) extends Matcher[java.io.File] {
        def apply(left: java.io.File) = {
            val name = left.getName
            MatchResult(
                name.endsWith(expectedExtension),
                s"""File $name did not end with extension "$expectedExtension"""",
                s"""File $name ended with extension "$expectedExtension"""""
            )
        }
    }

def endWithExtension(expecedExtension: String) = new FileEndsWithExtensionMatcher(expectedExtension)

}

// Make them easy to import with:
// import CustomMatchers._
object CustomMatchers extends CustomMatchers

I do not quite understand the reason they pu the FileEndsWithExtensionMatcher class into a trait? Why? Is it idiomatic scala way or some? Can't you explain it?

A:

It is just for your convienience.
That is one of many ways to modularize your code and than reuse it in desired suites. You can organize it in packages or objects and it will work either. With this approach it is just easy to mix traits into your tests. What's more it allows you to nicely organize connected matchers.
For example you can create trait with group of matchers for currencies and another trait matching physical units. Next you can mixin one or both traits into your Test.

