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

WIP attr macro #316

Merged
merged 10 commits into from
May 20, 2024
Merged

WIP attr macro #316

merged 10 commits into from
May 20, 2024

Conversation

cameron1024
Copy link
Member

WIP attribute macro for proptest

Roughly speaking:

#[property_test]
fn foo(x: i32) {
  assert_eq!(x, x);
}

is equivalent to:

proptest! {
  #[test]
  fn foo(x in any::<i32>()) {
    assert_eq!(x, x);
  }
}

Config can be set with #[property_test(config = ...)]

Known issues:

  • trybuild tests
  • what to do about the trybuild fork
  • better test coverage
  • docs

Opening this now to get some initial feedback

},
],
},
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there an equivalent of a snapshot test of the printed output? this tree i find tough to interpret directly -- though this will for sure be helpful as things change, to have a basis to compare to

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the Display snapshot probably makes a lot more sense

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decided to run it through a formatter first - much more readable now

Copy link
Collaborator

@rex-remind101 rex-remind101 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good to me, looking forward to non-wip status. besides the code and behavior i tried to make sure what i saw was hygienic based on these rules: https://gist.github.com/Kestrer/8c05ebd4e0e9347eb05f265dfb7252e1

sharing in case this is helpful to you or other reviewers. I'm not sure if this is common knowledge, it was helpful in building my understanding of proc macros though.

Copy link
Member

@matthew-russo matthew-russo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. changes are fairly straight forward. i'll try to pull this locally and give it a whirl as well

proptest-macro/src/property_test/codegen/mod.rs Outdated Show resolved Hide resolved
@rklaehn
Copy link

rklaehn commented Sep 13, 2023

I think this would be nice. Compile errors within a big proptest! block are always a bit hard to track down.

There is also the test-strategy crate that seems to do something very similar.

@cameron1024
Copy link
Member Author

I've picked this back up again after a long hiatus. I'll respond to feedback, and push those changes today.

The main issue I'd like to address at some point is the lack of compile-fail tests via trybuild (it doesn't build #[cfg(test)] code, which means the tests are pretty much only testing the parsing of the attribute, which is better than nothing I guess). I'll migrate them to use compiletest_rs in a future PR, unless people feel strongly that this should be included in this PR.

The only other thing I'm not sure about is how strongly we feel like we should mark it as "experimental" - I'm feeling pretty confident about the API. It leaves room to add more features, and it doesn't really lock us into much. The only exception is the name of the macro itself. A library can't simultaneously export both a proc macro and a macro_rules macro with the same name. Some alternatives are:

  • ptest - IMO looks too much like the regular test
  • property- not clear its a test
  • we could also just re-export the proptest-macro crate, so you could use proptest::proptest_macro::proptest, though I'm not sure if this would be more confusing. Perhaps if we rename the crate, we could get something like proptest::proptest_attr::proptest, but it's still not ideal

Personally, I feel like property_test is the way to go, but keen to hear other people's thoughts

@cameron1024
Copy link
Member Author

This looks really good to me, looking forward to non-wip status. besides the code and behavior i tried to make sure what i saw was hygienic based on these rules: gist.github.com/Kestrer/8c05ebd4e0e9347eb05f265dfb7252e1

sharing in case this is helpful to you or other reviewers. I'm not sure if this is common knowledge, it was helpful in building my understanding of proc macros though.

This looks really good - I'll have a read through and see if I can break anything

@cameron1024 cameron1024 marked this pull request as ready for review March 11, 2024 11:11
..Default::default()
}
)]
fn trailing_comma(x: i32) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test says trailing_comma but I don't see where the trailing comma is being tested? is that meant to be on Default::default()?


let tokens = quote! ( {

#(#errors)*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to have a snapshot test with errors?

Copy link
Contributor

@rexmas rexmas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to give this a full review again before approving, thanks for your patience :)

There's 1 test I am a little confused about that I commented on. If we can clear that up we're good to go. also I added a suggestion for another test but i won't block on that.

Personally, I feel like property_test is the way to go, but keen to hear other people's thoughts

I like that name 👍

I'll migrate them to use compiletest_rs in a future PR, unless people feel strongly that this should be included in this PR.

totally reasonable

The only other thing I'm not sure about is how strongly we feel like we should mark it as "experimental" - I'm feeling pretty confident about the API.

I feel fairly similar about the api, though I usually lean on the side of letting things bake. If we merge i can play around with it a bit more and maybe make a decision then?

Copy link
Member

@matthew-russo matthew-russo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me -- I didn't have any specific comments about the code itself. I think the main area that could use some work in a follow up would be documentation and examples -- both in our crates and in the book. What does the shape of the macro look like? what attributes does it support? what attributes doesn't it support?

Doesn't need to fall on your plate.

Also how are we thinking about how this sits w/ the decl macro? When do we suggest one or the other, what does one support that the other doesn't?

Personally, I feel like property_test is the way to go, but keen to hear other people's thoughts

agreed

I'll migrate them to use compiletest_rs in a future PR, unless people feel strongly that this should be included in this PR.

sounds good

The only other thing I'm not sure about is how strongly we feel like we should mark it as "experimental" - I'm feeling pretty confident about the API.

i like that its behind a feature flag for now. I think there are a number of things we'll want to change going forward (async support, explicit test cases, ability to customize strategies inline, etc) and having some flexibility to iterate will be good.

Copy link
Contributor

@rexmas rexmas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@matthew-russo matthew-russo merged commit c117b76 into master May 20, 2024
9 checks passed
@matthew-russo matthew-russo deleted the attribute branch May 20, 2024 02:22
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.

None yet

5 participants