Boost logo

Boost :

Subject: Re: [boost] [test] Interest in 'hamcrest'-style checks?
From: Jared Grubb (jared.grubb_at_[hidden])
Date: 2013-12-29 13:14:35


On Dec 29, 2013, at 1:04, Richard <legalize+jeeves_at_[hidden]> wrote:

> [Please do not mail me a copy of your followup]
>
> boost_at_[hidden] spake the secret code
> <A266F076-5C34-4A6C-8686-B8CE83318ECA_at_[hidden]> thusly:
>
>> I recently read "Modern C++ Programming with Test-Driven Development",
>> and I really enjoyed this book and highly recommend it.
>
> I didn't know about this book until you posted this thread. I bought
> it and read it and loved it!

Yes, it's a terrific book!

>> One of the
>> styles I saw for the first time was "hamcrest"-style assertions.
>> ("Hamcrest" is an anagram of "matchers"; also see
>> http://en.wikipedia.org/wiki/Hamcrest.)
>
> This is something that I don't think really buys us much in C++. The
> reason I say this is because in C++ we can take a macro argument and
> turn it into a string in order to produce a diagnostic message for the
> assert -- which Boost.Test does already.
>
> In Java, you have no preprocessor and no way to do this, so Hamcrest
> style matchers are the way that they get the extra detail into the
> assertion messages.
>
> Combine this ability of the preprocessor to take arbitrary program
> source text and turn it into a string and introduce a set of useful
> predicates for things like checking if a string starts with a prefix
> and you find the need for Hamcrest style matchers pretty much
> disappears.
>
> Without the ability to turn source text into a string, the case for
> Hamcrest style matchers would increase dramatically. One area where I
> see Hamcrest style matchers being useful is in configuring mock
> objects. Having used jMock for Java and Turtle for Boost.Test/C++ I
> think both of them read fairly similar.

I put together a version of the Hamcrest matchers, and the only ones that I've ended up using are:
 * SameSequence -- I find the "equal collections" macro to be awkward (mainly due to the use of iterators)
 * SameSequenceAnyOrder -- when the order of the result is not known (or relevant); the custom error strings are very helpful here
 * FutureReady/FutureNotReady -- shorthand for "BOOST_CHECK_EQUAL(future.wait_for(...), std::future_status::ready/timeout)"

But, it is very Java-esque, so I understand no one else was very excited about adding these. But, I figured I'd ask :)

Jared

> --
> "The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
> The Computer Graphics Museum <http://computergraphicsmuseum.org>
> The Terminals Wiki <http://terminals.classiccmp.org>
> Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk