Repository navigation
New matcher HasSubsequence - #79
bhamiltoncx wants to merge 3 commits into
Conversation
When implementing a library to test compiler command-line arguments in the form: -x foo -y -z bar I found I needed a Hamcrest matcher which would look for a consecutive sequence of items anywhere inside a collection of items. I was surprised that Hamcrest had matchers which: 1) Match if a collection includes a single item anywhere (`hasItem`) 2) Match if a collection includes multiple items anywhere (`hasItems`) 3) Match if a collection includes multiple items in order anywhere, consecutive or not (`containsInRelativeOrder`) 4) Match if a collection exactly matches another collection in order (`contains`) 5) Match if a collection exactly matches another collection in any order (`containsInAnyOrder`) but not for a consecutive sequence of items anywhere inside the collection. This implements such a matcher and adds tests. I borrowed the tests from `containsInRelativeOrder`, so let me know if there are better tests I should include.
|
You might have to remove it, I don't see it being used in the rest of the code. |
|
@bgertzfield can we not name it something starting with |
|
How about containsSubsequence? It can be defined for List and OrderedTree On Friday, December 5, 2014, Narendra Pathai notifications@github.com
|
|
Re symmetry: it's already consistent isn't it? TBH in always get confused by hamcrest's |
|
You're right, but by the time we'd figured that out it was too late. Like the make tab/space problem. S On 5 Dec 2014, at 09:31, bacar notifications@github.com wrote:
|
|
Have de considered/tried:
I am getting a bit off topic so I can start a new issue for this if that makes sense. |
|
With the current semantics that I think it's only I'm happy to rename this to |
|
@npryce @bgertzfield |
|
Namewise, either hasConsecutiveItems or hasSubsequence is fine. I think subsequence is a single word, not hyphenated, so hasSubSequence would not be right. |
|
OK, I renamed it to |
|
Hi Ben, I think with the other suggestion - hasConsecutiveItems - it's more obvious that it's most similar to hasItems. |
|
@bacar: In fact it was |
There was a problem hiding this comment.
Not sure about it being a Matcher<Collection<? extends T>>.
- Collections are not necessarily ordered, and so may not have stable subsequences. Should we make it only apply to ordered collections (List and SortedSet)?
- If not, then I think it would be better to define it as Matcher<Iterable<? extends T>>
There was a problem hiding this comment.
@npryce Yes I agree that with first point. IMHO it would not make sense to verify subsequence for unordered collections.
There was a problem hiding this comment.
Hi Nat,
Collections are not necessarily ordered, but they often are, and often this information is often embedded only in the documentation. Consider LinkedHashMap and TreeMap's keySet() and values() methods; to prevent using this matcher against these could be very tiresome - it forces the user into an cast (to an undocumented type which may change) or to a copy which requires the user to know as much as they did before.
I think there is already an onus on the Java programmer to understand the iteration semantics of the containers they use, since the JDK regularly documents additional semantics on top of that guaranteed by the type.
To me it seems that a Collection does have an order. That it may not be the same order every time are part of the semantics that the Java programmer deals with already.
There was a problem hiding this comment.
Iterable is not necessarily ordered either, of course. (Collection is simply Iterable plus size(), in my opinion, which is why I used it.)
I agree with @bacar that it's up to the engineer to provide Iterable / Collection objects which are ordered.
There was a problem hiding this comment.
Fair enough on not enforcing ordering in the type system. It is impractical.
@bgertzfield - Collection is more than Iterable plus size(). It also defines the mutation methods add, addAll, remove and clear. In the code I see, it's rare for a method to accept or return a Collection but common to accept or return an Iterable, especially code written in "modern Java" style that has more of a functional style in which streams of immutable objects (represented as Iterables) are transformed and filtered.
How hard would it be to make this matcher work in terms of Iterables rather than Collections?
|
Merged onto the v7.0 branch and generalised to be a matcher of Iterable |
|
Just back from the holidays now. Thanks for the merge! I agree that It's useful, then, to simply treat |
When implementing a library to test compiler command-line arguments in the form:
-x foo -y -z barI found I needed a Hamcrest matcher which would look for a consecutive
sequence of items anywhere inside a collection of items.
I was surprised that Hamcrest had matchers which:
hasItem)hasItems)containsInRelativeOrder)contains)containsInAnyOrder)but not for a consecutive sequence of items anywhere inside the collection.
This implements such a matcher (
hasSubsequence) and adds tests. I borrowed the tests fromcontainsInRelativeOrder,so let me know if there are better tests I should include. I wasn't sure about the
WithValuestuffso I just copy-pasted it from that test.