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
Matchers: ||, &&, and ! instead of anyOf, allOf, isNot #3727
Comments
Nor are || and &&. For those the reason is simple: They are not really operators, but rather a shorthand for more complex control flow (just as the conditional operator). I'm guessing that "!", being the fourth boolean "operator" was made not-user-configurable for consistency with the other three - boolean operators can just not be overridden. |
I would suggest a top-level "not", and member functions "and" and "or". expect(v, new isInstanceOf<num>.and(isPositive)); |
This comment was originally written by @seaneagan @1: makes sense, if || and && were overridable, then the RHS would get evaluated conditionally based on whether the operators were overloaded, which would be unpredictable. @2: +1, as that reads the exact same way as the operators would, "not" clearly reads betters than "isNot": not(equals(5)) ... and "and" and "or" read more fluently than "allOf" and "anyOf": expect(v, isNull.or(isEmpty)) |
Added Area-Library, Triaged labels. |
cc @gramster. |
I don't think this will happen before M1, and it comes with some cost. I assume that the expectation is that the base Matcher class will support additional methods for composition: Match and(Matcher other); This means we need to add extra non-final properties to the Matchers for these, and we can't have const matchers for common cases like we do now (e.g. isTrue). Also, one thing I don't like about this is you can do weird compositions like: A.and(B).or(C).and(D).or(E) |
This comment was originally written by @seaneagan I think someone suggested | and & on the mailing list, looks like it didn't make it to this bug. Along with "not", seems pretty nice: expect(x, not(isNull | isEmpty)); |
Removed Type-Defect, Area-Library labels. |
Added Pkg-Unittest label. |
Removed Area-UnitTest label. |
Removed Pkg-Unittest label. |
This issue has been moved to dart-lang/matcher#11. |
This issue was originally filed by @seaneagan
consider:
expect(v, !(isNull || isEmpty));
// vs.
expect(v, isNot(anyOf(isNull, isEmpty)));
expect(v, new isInstanceOf<num> && isPositive);
// vs.
expect(v, allOf(new isInstanceOf<num>, isPositive));
The operators are clearly shorter, but more importantly to me, they read more naturally, and I would think this would be especially true for non-native English speakers.
One small problem... the ! operator is not currently user-definable. Is there a reason for this ?
Could also potentially remove all the composite negation matchers:
isFalse // somewhat misleading since it matches more than just "false".
isNotNull
isNonZero
isNonNegative
isNonPositive
and just have the ! operator of the Matcher they are negating, lazily initialize them:
! isTrue
! isNull
! isZero
! isNegative
! isPositive
It would be unusual to want to override || or && operators, but for the ! operator, it would be useful to customize the negated message, for the description for:
! hasChildren
could be customized to:
"to not have any children"
instead of:
"not <to have children>"
which might be generated by:
isNot(hasChildren)
The text was updated successfully, but these errors were encountered: