Skip to content

New matcher HasSubsequence - #79

Closed
bhamiltoncx wants to merge 3 commits into
hamcrest:masterfrom
bhamiltoncx:has-consecutive-items
Closed

bhamiltoncx wants to merge 3 commits into
hamcrest:masterfrom
bhamiltoncx:has-consecutive-items

Conversation

@bhamiltoncx

Copy link
Copy Markdown

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 (hasSubsequence) and adds tests. I borrowed the tests from containsInRelativeOrder,
so let me know if there are better tests I should include. I wasn't sure about the WithValue stuff
so I just copy-pasted it from that test.

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.
@bhamiltoncx bhamiltoncx mentioned this pull request Dec 4, 2014
@Coneko

Coneko commented Dec 4, 2014

Copy link
Copy Markdown

ant javadoc fails on openjdk6 because of @SafeVarargs.

You might have to remove it, I don't see it being used in the rest of the code.

@npathai

npathai commented Dec 5, 2014

Copy link
Copy Markdown
Contributor

@bgertzfield can we not name it something starting with containsXXX as it will keep the api symmetric? Only if we can find a better name for it.

@npryce

npryce commented Dec 5, 2014

Copy link
Copy Markdown
Contributor

How about containsSubsequence?

It can be defined for List and OrderedTree

On Friday, December 5, 2014, Narendra Pathai notifications@github.com
wrote:

@bgertzfield https://github.com/bgertzfield can we not name it
something starting with containsXXX as it will keep the api symmetric?
Only if we can find a better name for it.

—
Reply to this email directly or view it on GitHub
#79 (comment).

@bacar

bacar commented Dec 5, 2014

Copy link
Copy Markdown

Re symmetry: it's already consistent isn't it?
Isn't this more like hasItems (matches a subset) than contains (matches the whole collection)?

TBH in always get confused by hamcrest's contains; I expect it to match a subset, like java.lang.String.contains or java.util.Collection.contains :-/

@sf105

sf105 commented Dec 5, 2014

Copy link
Copy Markdown
Member

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:

Re symmetry: it's already consistent isn't it?
Isn't this more like hasItems (matches a subset) than contains (matches the whole collection)?

TBH in always get confused by hamcrest's contains; I expect it to match a subset, like java.lang.String.contains or java.util.Collection.contains :-/

—
Reply to this email directly or view it on GitHub.

@bacar

bacar commented Dec 5, 2014

Copy link
Copy Markdown

Have de considered/tried:

  • coming up with a new name for it
  • marking the old name as @deprecated
    ?

I am getting a bit off topic so I can start a new issue for this if that makes sense.

@bhamiltoncx

Copy link
Copy Markdown
Author

With the current semantics that contains in Hamcrest is an anchored match which requires a match on all items, I think this is more like hasItems than contains.

I think it's only containsInRelativeOrder that is inconsistent, and I think we should probably rename that to hasItemsInRelativeOrder or something similar.

I'm happy to rename this to hasSubSequence if that seems clearer than hasConsecutiveItems. I just wanted to make it clear that this is very similar to hasItems except that hasItems does not have to be consecutive.

@npathai

npathai commented Dec 10, 2014

Copy link
Copy Markdown
Contributor

@npryce @bgertzfield hasSubSequence seems good. It clearly expresses the intention.

@npryce

npryce commented Dec 10, 2014

Copy link
Copy Markdown
Contributor

Namewise, either hasConsecutiveItems or hasSubsequence is fine. I think subsequence is a single word, not hyphenated, so hasSubSequence would not be right.

@bhamiltoncx

Copy link
Copy Markdown
Author

OK, I renamed it to hasSubsequence. Does this look good to land?

@bhamiltoncx bhamiltoncx changed the title New matcher HasConsecutiveItems New matcher HasSubsequence Dec 12, 2014
@bacar

bacar commented Dec 12, 2014

Copy link
Copy Markdown

Hi Ben,

I think with the other suggestion - hasConsecutiveItems - it's more obvious that it's most similar to hasItems.

@bhamiltoncx

Copy link
Copy Markdown
Author

@bacar: In fact it was hasConsecutiveItems, and @NarendraPathai asked me to rename it to hasSubsequence. Which does the team prefer?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about it being a Matcher<Collection<? extends T>>.

  1. Collections are not necessarily ordered, and so may not have stable subsequences. Should we make it only apply to ordered collections (List and SortedSet)?
  2. If not, then I think it would be better to define it as Matcher<Iterable<? extends T>>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@npryce Yes I agree that with first point. IMHO it would not make sense to verify subsequence for unordered collections.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@npryce npryce added this to the 7.0 milestone Dec 23, 2014
npryce added a commit that referenced this pull request Dec 23, 2014
@npryce

npryce commented Dec 23, 2014

Copy link
Copy Markdown
Contributor

Merged onto the v7.0 branch and generalised to be a matcher of Iterable

@npryce npryce closed this Dec 23, 2014
@npryce npryce self-assigned this Dec 23, 2014
@bhamiltoncx
bhamiltoncx deleted the has-consecutive-items branch January 6, 2015 16:38
@bhamiltoncx

Copy link
Copy Markdown
Author

Just back from the holidays now. Thanks for the merge!

I agree that Collection can optionally support add() and other mutation methods, but since those are optional methods, you're totally right that using it in an API basically means you can't rely on any optional methods.

It's useful, then, to simply treat Collection as Iterable plus size(). In this case, the extra copy to an ArrayList isn't going to really kill performance, so it's fine to hide the Collection interface if we don't want it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants