Skip to content

Latest commit

 

History

History
115 lines (84 loc) · 3.41 KB

customising-assertions.md

File metadata and controls

115 lines (84 loc) · 3.41 KB

Customising Assertions

Customising Output

Sometimes we want to customise the failure messages of an Assertion. Given the following:

!List.empty[String].isEmpty | "empty List is empty"

we get the following when run:

   - empty List is empty [✗]
     => false != true

If we don't like this message we can easily change it completely or add some extra information to it via the >> operator and the differentMessage function. differentMessage takes a NonEmptySeq of Strings to represent the lines of output required. You can also control whether to Replace the old Assertion message or Append to it. The >> operator works on any Predicate:

!List.empty[String].isEmpty >> differentMessage(oneOrMore("empty List is not empty", "I expected empty!"), Replace) | "empty List is empty"

which results in:

   - empty List is empty [✗]
     => empty List is not empty
        I expected empty!

You can also add (Append) some additional messages to the original failed output:

!List.empty[String].isEmpty >> differentMessage(oneOrMore("empty List is not empty", "I expected empty!"), Append) | "empty List is empty"

which results in:

   - empty List is empty [✗]
     => false != true
        empty List is not empty
        I expected empty!

Customising all functions used on an Assertion

If you want to customise the equality or difference functions, location information or context used when generating an Assertion use the |? operator with params or paramsWithContext functions.

Let's start by defining a custom Difference instance that prints out a custom message when two instances are what they are expected to be:

import boon._
import boon.model._
import Equality.genEq

val diff = Difference.from[String]((v1, v2, et) => {
    val (equalityType, expectation) = et match {
      case IsEqual    => ("is not the same as", "I expect them to be the same")
      case IsNotEqual => ("is the same as", "I expect them to be different")
    }
    oneOrMore(s"$v1 $equalityType $v2", expectation)
  }
)

We can combine the above into an Assertion as follows:

"Hello" =?= "Yellow" || "greeting" |? params(diff, genEq, implicitly[SourceLocation])

genEq is the generic equality (==) that boon defines. We implicitly get the SourceLocation here, you can also use a custom value if you have access to one.

The above will result in:

   - greeting [✗]
     => Hello is not the same as Yellow
        I expect them to be the same
     at ...

Or when asserting a difference with =/= :

"Hello" =/= "Hello" || "greeting" |? params(diff, genEq, implicitly[SourceLocation])

will result in:

   - greeting [✗]
     => Hello is the same as Hello
        I expect them to be different
     at ...

You may have noticed that we have not supplied a context to any of the above modifications. Since this is not what you'd always want to do, the contextual parameter is available on the paramsWithContext function instead of params and can be used as:

"Hello" =?= "Yellow" || "greeting" |? paramsWithContext(diff, genEq, oneOrMore("actual" -> "Hello", "expected" -> "Yellow"), implicitly[SourceLocation])

which results in:

   - greeting [✗]
     => Hello is not the same as Yellow
        I expect them to be the same
     at ...
       #: actual -> Hello
          expected -> Yellow