-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
assert should take an optional message #6190
Comments
There are alternatives to assert. assert should really just be used for language tests (Gilad, correct me if I'm wrong). For everything else there is a comprehensive set of matchers in the unit test library, which are soon going to be factored out into their own library. These matchers generate good messages automatically, and can be supplemented with user-supplied messages. |
I agree with the submitter that assert would be much more useful with an optional message parameter. I'm not sure I understand comment 1. I assume one rights code with asserts that is not in unit tests and does not have access to that library. Added Accepted label. |
This comment was originally written by @davidB but "expect" can not be ignored like assert via in non checked mode (VM or dart2js). other example : var nextState = states.get(next); |
This comment was originally written by [email protected] This post is old, but I'd like to +1 it if it hasn't been prioritized. assert seems like an awesome way to have extra dev/testing validation, but without a message the developer has to find that line number (which the dart IDE doesn't make clickable) and navigate manually to understand exactly what failed. |
Issue #17042 has been merged into this issue. |
This comment was originally written by [email protected] Is there any update on this? |
@lrhn what do you think about this request? (A customer asked to be able to add a message to assert.) |
It's a language change, not a library change, so if anything, it should be a DEP proposal. I think there's a reason nothing has been done on this in almost three years - it's not that important. The simple work-around is to use assert with a function: assert(() => test || throw "message"); |
Thanks for the feedback. If we don't plan to do this, let's close the issue. |
You could possibly make the argument that Dart is mature enough now to not need things like language-level assert, but that's probably a non-starter.
|
FWIW, we use asserts extensively in our library, to help users of our library understand when they are violating our library's contract. We would love to be able to direct users of our library to documentation when they hit an assert; currently the asserts aren't very actionable. Making them throw a message is certainly one option we will consider in the meantime. |
Wanted to ping this thread to see what the status is. I have been using asserts in a similar manner to Flutter and would like to pass some actionable messages to users when runtime assertions fail. |
@zoechi Thanks for the pointer, watching that issue now. |
We're doing this now. Tracking it here: #24213. |
Currently the assert statement cannot provide any runtime information about what went wrong. Adding an addition message argument to the assert statement could provide such runtime information. For instance
assert(foo.isBar(), '$foo is not Bar');
could provide runtime information about the particalur foo instance that failed and not only, as now, that some foo instance failed. Such additional information would be valuable in debugging failed assertions.
The text was updated successfully, but these errors were encountered: