rust-pretty-assertions icon indicating copy to clipboard operation
rust-pretty-assertions copied to clipboard

✨"now configurable!"; customizable auto-labels and diff style

Open rivy opened this issue 5 years ago • 7 comments

Fully implements customizable labelling and diff style for assert_eq!(...) and assert_ne!(...), but easily extended).

Fixes: #35 Fixes: #37 Alternative to: #33, #39

rivy avatar Jan 01 '20 19:01 rivy

It needs some condensing and refactoring before release, but this now allows much more flexible configuration. Customized labels and git-style diff prefixes are built-in as features ('labels' and 'diffstyle-git', respectively).

I'll polish it and get it ready for release as a real PR over the next week or so...

rivy avatar Feb 05 '20 02:02 rivy

Code functionality and most of the code polish is done.

  • Adding automatic labelling of assert arguments is as simple as adding the "labels" feature when including the crate dependency (drop-in equivalent to current usage, no code changes necessary).
  • Displaying git-style-ish diffs is also simply feature gated with the "diffstyle_git" feature (again, drop-in equivalent to current usage, no code changes necessary).
  • More complex configurations can be easily accomplished with very simple macro boilerplate (see the pretty_assertion_custom example).
// from 'pretty_assertion_custom.rs'
use pretty_assertions;

macro_rules! assert_eq { ($($arg:tt)+) => ({
    pretty_assertions::with_config_assert_eq!( pretty_assertions::Config { 
        auto_label: true, 
        style: pretty_assertions::Color::Yellow.normal(),
        prefix: "<=>",
        prefix_left: "<<=", prefix_right: "=>>", 
        ..Default::default() 
        },
        $($arg)+
    )
})}

/// ... assert_eq!( actual, expect ); ...

Commit squashing into more logical chunks is the next WIP. And then some documentation work.

rivy avatar Feb 14 '20 05:02 rivy

Commits are more polished and logical. Docs are still WIP.

This covers the most common requested changes (labels and "git-esque" diffs) plus additional styling flexibility.

I'd like to be able to configure the other text in the assert. However, I haven't found a clean method as of yet. (Any suggestions?)

The added configuration flexibility is opt-in. Basic use of the "labels" or "diffstyle_git" features is straightforward by adding the feature in the dependency ... and the resulting use is still a drop-in for the usual assert_eq!(...) and assert_ne!(...).

Thoughts? (cc: @colin-kiegel, @cormacrelf, @aquarhead, @TimoFreiberg, @spenserblack, @martinlindhe)

rivy avatar Feb 25 '20 01:02 rivy

Although the boilerplate customization is fairly simple, I'd like to present a simpler generator/helper macro to more automatically generate a replacement assert_eq!(), but I'm not sure if it's possible.

When I create a generator macro, the generated assert_eq!() macro "shadows" the original at a deeper scope level producing a compiler error. So, I'm stymied, unable to produce an error-free version. Is there a possible workaround?

But, in the end, since the boilerplate is relatively simple, I'm ok with the current implementation.

Thoughts?

rivy avatar Feb 25 '20 02:02 rivy

Is there any progress on this?

wycats avatar Nov 03 '20 16:11 wycats

Ping @tommilligan ... are you maintaining this now? If so, could you review?

rivy avatar Apr 17 '21 14:04 rivy

@rivy thanks for the ping. I have looked over the basic design and like what you've done, but I need to invest some more time in benchmarking the changes and thinking about extensibility/the right public API to expose.

You might be interested in the recent discussion on this issue: https://github.com/colin-kiegel/rust-pretty-assertions/issues/24#issuecomment-797762148

tommilligan avatar Apr 19 '21 17:04 tommilligan

⚰️

rivy avatar Oct 13 '22 03:10 rivy