assertables-rust-crate icon indicating copy to clipboard operation
assertables-rust-crate copied to clipboard

More types of assertions?

Open drmorr0 opened this issue 2 years ago • 2 comments

I am wondering if adding more types of assertions would be useful/beneficial to this project? I would really like to have things like

assert_none!(option) // ensures the option type is empty
assert_some_eq!(option, val) // ensures the option is inhabited and contains val
assert_ok!(result) // ensures the result is OK
assert_err_is!(result, errtype) // ensures the result is the expected error
assert_len!(seq, n) // ensures the sequence or object has n elements

I'm happy to implement and submit a PR if this would be useful!

drmorr0 avatar Sep 26 '23 16:09 drmorr0

I am wondering if adding more types of assertions would be useful/beneficial to this project?

Yes please.

I would really like to have things like...

All sound good.

Yes please for merge requests.

Heads up that the existing implementation macros are significantly long. Each macro group is its own file, and comes with variations to use a message parameter or not, as well as typical cargo tests, and cargo doc comments that are tests, and corresponding links in the README. I speculate that there may be better ways to arrange things, or do a meta-macro, or some such-- any ideas of yours will be good to know.

joelparkerhenderson avatar Sep 27 '23 21:09 joelparkerhenderson

I will take a stab and see what I can come up with. I'll let you know if I run into issues!

drmorr0 avatar Sep 29 '23 20:09 drmorr0

Completed these:

  • assert_ok etc.
  • assert_err etc.
  • assert_some etc.
  • assert_none
  • assert_ready etc.
  • assert_pending

I have assert_len on the roadmap.

Thanks!

joelparkerhenderson avatar Oct 03 '24 14:10 joelparkerhenderson

inserting asset type is assert of number being within range, but less of other number:

assert_lt_by!(9,10,2); // success
assert_lt_by!(7,10,2); // panics

similar to check number withing some delta/epsilon, but directional.

usage of existing delta/epsilon requires manual calculation of midpoints which is a lot of noise.

dzmitry-lahoda avatar Nov 18 '24 01:11 dzmitry-lahoda

Are you saying something like this?

assert_lt_by!(a, b, c) => (b - a) < c

If so, what do you think of the word "delta" because it can make it more clear?

assert_delta_lt!(a, b, c) => (b - a) < c

joelparkerhenderson avatar Nov 18 '24 17:11 joelparkerhenderson

If b - a under flows like (1u64 - 2u64), what would be panic? Ideally should assert with description, rather than underflow (that why I cannot write it as is easy - need sugar).

I am fine with any naming, most important is easy to grasp explanation imho. I wrote such methods before, were very useful. Just lost them all time moving from project to project.

dzmitry-lahoda avatar Nov 18 '24 20:11 dzmitry-lahoda

Yes you're right. There will need to be a checked delta and it will need to returns a flow error.

I'll do it. E.t.a. 1-2 weeks.

joelparkerhenderson avatar Nov 19 '24 22:11 joelparkerhenderson

Update for you...

The new macros assert_diff are ready on the main branch if you want to see them:

https://github.com/SixArm/assertables-rust-crate/tree/main/src/assert_diff

The macros assert_abs_diff are now updated with overflow detection:

https://github.com/SixArm/assertables-rust-crate/tree/main/src/assert_abs_diff

I intend to test and release this weekend.

joelparkerhenderson avatar Dec 02 '24 19:12 joelparkerhenderson

Released. When convenient, can you upgrade to version 9.5.0, and try the new macros assert_diff_eq_x and assert_abs_diff_eq_x and let me know if they work the way you want?

joelparkerhenderson avatar Dec 07 '24 02:12 joelparkerhenderson