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

Add support for allow_duplicates! #346

Merged
merged 1 commit into from Feb 15, 2023
Merged

Conversation

mitsuhiko
Copy link
Owner

@mitsuhiko mitsuhiko commented Feb 12, 2023

This adds a new macro called allow_duplicates!. When a block of code is wrapped with it, every snapshot assertion can be visited more than once. In that case all of the snapshot assertions in a loop etc. have to match with each other and if not, an assertion error is raised.

Motivational example:

#[test]
fn test_basic_duplicates_passes() {
    allow_duplicates! {
        for x in (0..10).step_by(2) {
            let is_even = x % 2 == 0;
            assert_debug_snapshot!(is_even, @"true");
        }
    }
}

Fixes #313

@ilyagr
Copy link

ilyagr commented Feb 13, 2023

What are the limitations of this, aside from the snapshot having to take the same value every time? Can I just wrap all of my tests in allow_duplicates!? Could I repeatedly call a function that has an assert_snapshot! in it.

I guess one problem with that is that rustfmt and rust-analyzer might have some trouble if all of the coffee is in a macro.

I'm hoping that understanding this better might clarify what names are good for the feature.

@mitsuhiko
Copy link
Owner Author

The limitations are that the results have to match per invocation. They uniqueness key for inline snapshots is the location in the file and for named snapshots is the name of the snapshot provided.

@ilyagr
Copy link

ilyagr commented Feb 13, 2023

Sorry, I edited the question above just as you answered. TLDR: Any limitations apart from that?

@mitsuhiko
Copy link
Owner Author

Not really.

@ilyagr
Copy link

ilyagr commented Feb 14, 2023

In the spirit of trying to clarify pesky details, which of these will work? (Some or all of these might be worth adding as tests):

1

fn assert_even(x:i64) {
        let is_even = x % 2 == 0;
        insta::assert_debug_snapshot!(is_even, @"true");
}

fn test(){
  insta::allow_duplicates! {
      assert_even(0);
      assert_even(2);
    }
}

2

fn assert_even(x:i64) {
        let is_even = x % 2 == 0;
        insta::assert_debug_snapshot!(is_even, @"true");
}

fn test(){
  insta::allow_duplicates! {
      assert_even(0);
  };
  insta::allow_duplicates! {
      assert_even(2);
  };
}

3

fn assert_even(x:i64) {
        let is_even = x % 2 == 0;
        insta::allow_duplicates! {
              insta::assert_debug_snapshot!(is_even, @"true");
        };
}

fn test(){
      assert_even(0);
      assert_even(2);
}

@mitsuhiko
Copy link
Owner Author

They will all "work" but only the first will do what you want on assertion failures.

@mitsuhiko
Copy link
Owner Author

Going to be sticking with allow_duplicates! as name.

@mitsuhiko mitsuhiko merged commit e5c5dfa into master Feb 15, 2023
@mitsuhiko mitsuhiko deleted the feature/allow-duplicates branch February 15, 2023 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better ways of dealing with snapshots in loops or helper functions
2 participants