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

assert should take an optional message #6190

Closed
johnniwinther opened this issue Oct 23, 2012 · 16 comments
Closed

assert should take an optional message #6190

johnniwinther opened this issue Oct 23, 2012 · 16 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report customer-flutter P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@johnniwinther
Copy link
Member

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.

@gramster
Copy link

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.

@gbracha
Copy link
Contributor

gbracha commented Oct 25, 2012

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.

@DartBot
Copy link

DartBot commented Apr 12, 2013

This comment was originally written by @davidB


but "expect" can not be ignored like assert via in non checked mode (VM or dart2js).
assertion can usefull to detect an throw error in dev/staging and simply do nothing in production live.

other example :

var nextState = states.get(next);
assert(nextState != null, "state '${next}' is not defined"); // For test/dev runtime only
if (nextState == null) {
  // nothing to change.
}

@DartBot
Copy link

DartBot commented Nov 2, 2013

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.

@gbracha
Copy link
Contributor

gbracha commented Aug 27, 2014

Issue #17042 has been merged into this issue.

@DartBot
Copy link

DartBot commented Apr 21, 2015

This comment was originally written by [email protected]


Is there any update on this?

@johnniwinther johnniwinther added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Apr 21, 2015
@sethladd
Copy link
Contributor

sethladd commented Jul 6, 2015

@lrhn what do you think about this request?

(A customer asked to be able to add a message to assert.)

@lrhn
Copy link
Member

lrhn commented Jul 7, 2015

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.
I'm personally split on whether it's even a good idea.

The simple work-around is to use assert with a function:

assert(() => test || throw "message");

@sethladd
Copy link
Contributor

sethladd commented Jul 7, 2015

Thanks for the feedback. If we don't plan to do this, let's close the issue.

@gramster
Copy link

gramster commented Jul 7, 2015

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.

On Jul 7, 2015, at 9:08 AM, Seth Ladd [email protected] wrote:

Thanks for the feedback. If we don't plan to do this, let's close the issue.


Reply to this email directly or view it on GitHub.

@Hixie
Copy link
Contributor

Hixie commented Aug 24, 2015

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.

@sethladd
Copy link
Contributor

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed accepted labels Feb 29, 2016
@jxson
Copy link

jxson commented Aug 29, 2016

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
Copy link
Contributor

zoechi commented Aug 30, 2016

@jxson see #27154

@jxson
Copy link

jxson commented Aug 30, 2016

@zoechi Thanks for the pointer, watching that issue now.

@munificent
Copy link
Member

We're doing this now. Tracking it here: #24213.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-duplicate Closed in favor of an existing report customer-flutter P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests