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
Provide better mismatch descriptions for any_of
and only_contains
#55
Provide better mismatch descriptions for any_of
and only_contains
#55
Conversation
existing tests are adjusted to expose the problem/have regression
see problems with this commit in the merge request notes
|
I would be happy to address to issues marked as problems/improvements, but I would like to get feedback whether the suggestions outlined are OK. Thanks! |
@zsoldosp Sorry about the long delay on this. Yeah, matchers are intended to be stateless in the current design. That's not to say we couldn't revisit that, but certainly right now adding persistent state is not ideal. While state is useful in test cases, matchers are not a purely test-oriented concept and retaining stateless matching is useful in a lot of cases. So in order to merge this, it must support streaming iterables as well as non-streaming. |
@zsoldosp Peter, what is What do you think? |
@svetlyak40wt that sounds good to me. However, how do you "cast" something to bool?
|
Backwards compatibility is important in the 1.x stream; I'm willing to revisit it for the 2.x To make something boolean-ish, an object has to implement The barrier here, though, is pretty high. You're proposing revamping the interface of all matchers, including those written by people who have used Hamcrest to implement custom ones. I don't know that the value gained by such a change is enough to justify the amount of incompatibility woes involved. |
@offbyone quick note re: backwards compatibility - while not-so-pythonic, the hamcrest core can be changed in such a way to support both return types ( |
I agree with @zsoldosp we just need to play a little with such soltuion, rewrite one core matcher and see if there will be so problematic to use it in mix with old style matchers. |
You're not going to be able to see the full scope of the uses you'll get. The issue is that there's nothing out there that wraps the matcher. You can have assert_that do something nice, sure, but other code will have to be either built to support this "DescribedBoo" or not, and in one direction there will be a lot of repeated testing code to check it. Remember that the hamcrest core doesn't actually consume the output of the match function, it only emits it. The assert_that helper function isn't part of the core, nor is it the only way to use matchers (I use them in code that has nothing to do with testing, for example). |
after digging into the code more, I'm confused
|
This has sat long enough I think it probably needs a revisit post-2.0 |
What problem does this merge request solve?
running
in
master
reportsafter the merge request, the error message becomes
However, this merge request is not perfect.
Known (Guessed) Problems
sequence
is a stream generator that can only be iterated once, thendescribe_mismatch
will fail trying to iterate it again.I believe it would be safe to encapsulate
list(sequence)
as an instance variable (or hide behind a memoized property) where it could be used from both methods, but the tutorial/docs are pushing in the direction of stateless matcher preferences. Thus not being familiar enough with the architecture and usages ofhamcrest
, I didn't want to do that initiallyPotentatial Improvements
prefix
/postfix
could be handled as TestCase class variables (like a "Template Method")any_of
had the same problem (8f0bcce) asonly_contains
(5a6c990) - not having a proper delegation to the actual matchers to provide the mismatch description. I suspect there can be other (composite)matchers that have the same issue.
issequence_onlycontaining_test
is coupled to thehas_length
implementation. Probably there is (should be) a way to have a matcher with test-controllabledescribe_foo
values, so there is no need for coupling. Then the initial assertions fromtestDescribeMismatchDisplayUsedMatchersDescriptionForFirstFailingItem
could be removeddescribe_mismatch
and_matches
. However, to resolve that by introducing aget_first_mismatch
method would need to be an object (can't useNone
due to the need to distinguish between no mismatches andTypeError
return type cases), and I didn't want to invest in that until I knew that change was welcome