Test expectation language (was Re: [squeak-dev] The Inbox: CollectionsTests-dtl.209.mcz)

Colin Putney colin at wiresong.com
Sat Jan 11 16:28:16 UTC 2014


On Sat, Jan 11, 2014 at 9:14 AM, Frank Shearar <frank.shearar at gmail.com>wrote:


> That, sadly, doesn't work, because here a is a message send to So.
> What you need is a message taking a (and b) as parameters, which
> requires two words before the word "a". I'm kind've leaning towards
> Assert: Assert that: a equals: b.
>
> In RSpec, changing the assertion language means not having to monkey
> patch. In our case, TestCase does a whole pile of stuff. One of those
> things is a DSL (domain specific vocab, as Colin put it - maybe even
> "domain specific jargon") in the form of #assert: and friends. Pulling
> that out into (say) Assert means factoring things better. JUnit also
> did this, AFAIK, when they pulled in the Hamcrest matchers. (Assert
> isn't a novel idea, btw: I've stolen it from NUnit:
> Assert.IsTrue(proposition), Assert.AreEqual(a, b).)
>

It's true; it would separate the machinery for running tests (TestCase and
friends) from the vocabulary for expressing expectations . Node.js does
this nicely, and there's a selection of both test runners and assertion
libraries that you can mix and match.

However, using a global like that makes customizing the vocabulary awkward.
You'd have to add class-side extension methods to Assert, possibly leading
to extension conflicts between different test packages.

One idea is to wrap the value being verified in an assertion proxy. It
would forward messages to the object being examined, and intercept the
answer. If the answer is false, it raises a TestFailure, other wise it
wraps the answer and returns it. That would enable things like:

it := Assertion for: [3 + 4].
it > 6.
it = 7.
it < 8.
it + 1 = 8.
it negated = -7.

Then you could make a custom wrapper if you wanted more abstract assertions:

it := EnvironmentAssertion for:
    [(Environment named: #test)
        exportSelf;
       bind: #Griffle to: Object new;
      yourself].

it exports: #Griffle.

That's kind of neat, but I suspect it breaks down when you have non-trivial
tests. It's tough to beat the simple #assert:.

Colin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20140111/77fde361/attachment.htm


More information about the Squeak-dev mailing list