Self-describing matchers

26 views
Skip to first unread message

Dominic Fox

unread,
May 1, 2013, 8:13:13 AM5/1/13
to specs2...@googlegroups.com
A useful property of Hamcrest matchers is that they describe the properties they are looking for. This means that if I have a matcher that scans a collection for items matching other matchers, e.g.

[1, 2, 3] must containItemsLike(greaterThan(1), greaterThan(2), greaterThan(3))

I can return a match failure report like "no item in the collection was greater than 3".

Specs2 matchers appear not to be self-describing, which means that I'm limited to saying "no set of items in the collection uniquely matched the supplied matchers" - much less helpful.

Is there a canonical way to attach a self-description to a matcher, so that the match failure reports of compound matchers can be more informative?

Dominic

etorreborre

unread,
May 1, 2013, 9:00:50 AM5/1/13
to specs2...@googlegroups.com
The short answer is no.

I clearly need to spend more time on this subject but what's already there will return something like:

Seq(1, 2, 3) must haveAllElementsLike(e => e must beGreaterThan(3))

> in 'Seq(1, 2, 3)'
   some elements are not correct: 
   1 is not greater than 3
   2 is not greater than 3

I deliberately changed your example because the hamcrest method "hasItems" is ambiguous IMHO. It takes a list of matchers and apply an OR to them but it's so clear from the name, it could be AND as well.
Then I think that a full failure message is better than just a description of the matcher because you also get the actual value that has been checked, not only the expected one.

Anyway there is certainly some room to improve existing collection matchers (you're the second person to ask about that in the last 2 days :-)), I'll try to look into this and learn from Hamcrest.

E.

Dominic Fox

unread,
May 1, 2013, 12:45:43 PM5/1/13
to specs2...@googlegroups.com
The use case I have in mind is my custom hasItemsLike matcher, which tests that *some* items in an unordered collection match the supplied matchers (it requires that every matcher has at least one distinct item that matches it). In the case of this matcher, simply reporting mis-matches won't work because the overall failure to match means that there is no sub-collection of the items in the collection that can be paired one-to-one with the collection of matchers.

Code here, in case anyone's interested:


E, I'd be interested in your responses to the other matchers in this library - we aren't using the very latest version of specs2, and you may have things brewing that would render them obsolete...

Dominic
Reply all
Reply to author
Forward
0 new messages