Skip to content

Relative order - #74

Merged
npryce merged 2 commits into
hamcrest:masterfrom
kay:relative_order
Nov 25, 2014
Merged

npryce merged 2 commits into
hamcrest:masterfrom
kay:relative_order

Conversation

@kay

@kay kay commented Nov 16, 2014

Copy link
Copy Markdown
Contributor

This PR is a feature I've wanted for some time that Kevin Pauli from the Hamcrest Developers mailing list suggested in April along with a patch. I've applied his patch to the codebase and ensured it all looks sensible. My only contribution to this is a couple test cases I felt were useful and inclusion in matchers.xml.

The matcher is similar to the IsIterableContainingInOrder matcher except it accepts a partial order ("relative order") between elements. Allowing you to match an actual collection of 1, 2, 3, 4, 5, 6, 7 with a specification of containsInRelativeOrder(2, 4, 6).

Kevin Pauli's initial post https://groups.google.com/forum/?fromgroups#!msg/hamcrest-dev/FsLlyRcGPzs/X0sWHuv8LkkJ

order) between items in an iterable. The iterable may contain gaps
inbetween two matching items. Gaps may appear at the beginning, end or
in between. A user may specify may gaps in the sequence.

e.g. Given an iterable 1, 2, 3, 4, 5, 6, 7 if the user were to specify
containsInRelativeOrder(2, 4, 6) this would match successfully.

This commit is the direct incorporation of a patch provided by Kevin Pauli.

See also https://groups.google.com/forum/?fromgroups#!topic/hamcrest-dev/FsLlyRcGPzs

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is this indentation deliberate?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No. Fixed.

it'll appear on the "Matchers" factory utility which was initially
missing from Kevin Pauli's patch.
npryce added a commit that referenced this pull request Nov 25, 2014
@npryce
npryce merged commit fb8cf22 into hamcrest:master Nov 25, 2014
@bhamiltoncx

Copy link
Copy Markdown

If I'm understanding this correctly, containsInRelativeOrder(2, 3, 4) does a non-anchored match, so it will match the list [1, 2, 3, 4, 5].

However, the similarly-named contains(2, 3, 4) does not match the list [1, 2, 3, 4, 5]—it does an anchored match, so it only matches the list [2, 3, 4].

Is this correct? Should we rename this so it's not confused with contains()?

@bhamiltoncx

Copy link
Copy Markdown

Here's a pull request that implements a non-anchored contains. I called it hasConsecutiveItems.

#79

@bacar

bacar commented Dec 5, 2014

Copy link
Copy Markdown

Hi Ben,

It does seem to me that it should be called hasItemsInRelativeOrder, similar to your recent PR.

sdwilsh pushed a commit to meta/buck that referenced this pull request Jan 10, 2015
Summary:
Hamcrest includes `Matcher` implementations which:

1) Check if a collection includes a single item anywhere (`hasItem`)
2) Check if a collection includes multiple items anywhere (`hasItems`)
3) Check if a collection exactly matches another collection in order (`contains`)
4) Check if a collection exactly matches another collection in any order (`containsInAnyOrder`)

Bizarrely, Hamcrest even recently added a `Matcher` which searches for
the items of a collection in relative order anywhere inside the target
collection (`containsInRelativeOrder`). (Note that this searches anywhere
inside the container, unlike the other `contains` `Matcher`s!)

hamcrest/JavaHamcrest#74

We need a `Matcher` which finds a sub-collection inside a larger
collection in order (i.e., finding `[2, 3]` inside `[1, 2, 3]` but not
inside `[1, 2, 42, 3]`).

This diff adds that matcher, `hasConsecutiveItems()`, along with tests.

Test Plan: Unit tests includes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants