Reporting errors in a ScalaTest-friendly way

40 views
Skip to first unread message

Paul Butcher

unread,
May 18, 2012, 8:18:39 PM5/18/12
to scalate...@googlegroups.com
I'm in the process of writing ScalaMock 3, which will be based on macros instead of a compiler plugin. One of the things I'd like to do better this time around is to report errors in a more ScalaTest-friendly way.

Right now, ScalaMock just does:

throw new ExpectationException(message)

What should I do instead?

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Bill Venners

unread,
May 18, 2012, 8:45:34 PM5/18/12
to scalate...@googlegroups.com
Hi Paul,

There's actually quite a bit you can do. If you mix in
org.scalatest.StackDepth, you can provide a stack depth where the
actual error occurred. This helps users navigate to the offending line
of test code more quickly:

http://www.artima.com/docs-scalatest-1.8.RC2/#org.scalatest.exceptions.StackDepth

(Exception classes and related traits are moving to
org.scalatest.exceptions in 1.8, so you may as well use the new
package name in ScalaMock 3. The old package names will still work but
be deprecated for a while (then later removed.)

StackDepthException is a superclass that does this, in case that helps:

http://www.artima.com/docs-scalatest-1.8.RC2/#org.scalatest.exceptions.StackDepthException

You have to provide the stack depth, which you can get by looking at
the call stack. This exception takes a function
(failedCodeStackDepthFun) that produces the stack depth, so that the
time spent filling in then inspecting the call stack need not happen
unless it is needed.

Another trait you might implement is ModifiableMessage:

http://www.artima.com/docs-scalatest-1.8.RC2/#org.scalatest.exceptions.ModifiableMessage

This lets clue strings be appended or prepended via the withClue
construct. You get a withClue by default in any Suite, inherited from
trait Assertions, that prepends the message with a clue string, a la:

withClue("it's bigger than a breadbox") {
args should have size 12
}

Trait AppendedClues provides an implicit that let's people put it after, as in:

eventually { query() should be ("happy") } withClue "it's bigger than
a breadbox"

Both of these use ModifiableMessage. Another trait you might implement
is ModifiablePayload:

http://www.artima.com/docs-scalatest-1.8.RC2/#org.scalatest.exceptions.ModifiablePayload

This lets payloads be inserted into the exception via the withPayload
construct (from trait Payloads):

http://www.artima.com/docs-scalatest-1.8.RC2/#org.scalatest.Payloads

ModifiablePayload's self type is PayloadField, so you'll have to mix
that in too:

http://www.artima.com/docs-scalatest-1.8.RC2/#org.scalatest.exceptions.PayloadField

Hope you're not sorry you asked. Having this stuff in your exceptions
makes it more ScalaTest-friendly, but will still allow it to be used
in other test frameworks just fine.

1.8 is ready for release by the way. It has completed its RC phase.
Next week Dick Wall and I are teaching a course in Atlanta, so I'm a
bit shy about releasing a major ScalaTest release right before that,
because I won't have as much time to fix any problems that might
arise. If I feel I have enough time this weekend I'll release it then,
else it will be released one week later, after the Atlanta course.

Thanks.

Bill
> --
> You received this message because you are subscribed to the Google
> Groups "scalatest-users" group.
> To post to this group, send email to scalate...@googlegroups.com
> To unsubscribe from this group, send email to
> scalatest-use...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/scalatest-users?hl=en
> ScalaTest itself, and documentation, is available here:
> http://www.artima.com/scalatest



--
Bill Venners
Artima, Inc.
http://www.artima.com

Paul Butcher

unread,
May 19, 2012, 8:43:15 AM5/19/12
to scalate...@googlegroups.com
Thanks Bill - just what I was after :-)

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Reply all
Reply to author
Forward
0 new messages