Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming of method Matchers#contains is ambiguous #140

Open
hakanai opened this issue Mar 11, 2016 · 3 comments
Open

Naming of method Matchers#contains is ambiguous #140

hakanai opened this issue Mar 11, 2016 · 3 comments

Comments

@hakanai
Copy link

hakanai commented Mar 11, 2016

In code reviews here, people often misread this:

assertThat(collection, contains("x"));

As meaning this: "Assert that the collection contains 'x'".

But what it actually means is this: "Assert that the collection contains only 'x'".

This causes a lot of unnecessary questions to be raised on our code reviews where code was updated to use Hamcrest, just because it reads differently to what it's actually doing.

At the same time, people looking to write new code with Hamcrest are using autocompletion to find appropriate-looking methods, and will often try to use contains before discovering that they really wanted hasItem.

I would suggest containsOnly as a new name, but containsInAnyOrder sort of has the same issue and I am finding it hard to jam the additional word in there.

@alechenninger
Copy link

What if containsOnly had no ordering requirement, and additionally there was a containsOnlyInOrder?

To me this is intuitive to what the matchers are saying: if I didn't say anything about order, don't worry about it.

@erosb
Copy link

erosb commented Mar 13, 2016

there was a containsOnlyInOrder

Few months ago I've created a PR with a similar matcher (though different naming), see #128
But don't expect any response soon (due to #131 )

@sf105
Copy link
Member

sf105 commented Apr 7, 2016

Agree with your points, but this would be quite a breaking change. I'll look at deprecating.

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

No branches or pull requests

4 participants