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

Set test case function Ident spans to corresponding test_case spans #118

Closed
rpwachowski opened this issue Mar 26, 2023 · 2 comments · Fixed by #119
Closed

Set test case function Ident spans to corresponding test_case spans #118

rpwachowski opened this issue Mar 26, 2023 · 2 comments · Fixed by #119

Comments

@rpwachowski
Copy link
Contributor

Hi,

Right now the Span of a test case Ident is derived from the macro's call site. This means the observable source location for every test is grouped on the first invocation of test_case, which leads to side effects in tools that utilize those locations (e.g. the rust-analyzer Visual Studio Code extension and some new additions to listing tests in a binary.) I was able to throw together a change which uses Ident::set_span to match the emitted test function to its source attribute, but I'm not very savvy on proc macros/hygiene and wasn't sure if doing so is bad practice. If there aren't any issues with it, I could open a PR to introduce the change. I've included below examples of rust-analyzer's test decorations on the readme's sample code and cargo test -- --list -Zunstable-options --format=json run on nightly both before and after applying the change to demonstrate.


Before

Before

$ cargo test -- --list -Zunstable-options --format=json
...
{ "type": "test", "event": "discovered", "name": "tests::multiplication_tests::when_both_operands_are_negative", "ignore": false, "ignore_message": "", "source_path": "tests/example.rs", "start_line": 15, "start_col": 5, "end_line": 15, "end_col": 61 }
{ "type": "test", "event": "discovered", "name": "tests::multiplication_tests::when_both_operands_are_positive", "ignore": false, "ignore_message": "", "source_path": "tests/example.rs", "start_line": 15, "start_col": 5, "end_line": 15, "end_col": 61 }
{ "type": "test", "event": "discovered", "name": "tests::multiplication_tests::when_operands_are_swapped", "ignore": false, "ignore_message": "", "source_path": "tests/example.rs", "start_line": 15, "start_col": 5, "end_line": 15, "end_col": 61 }

After

After

$ cargo test -- --list -Zunstable-options --format=json
...
{ "type": "test", "event": "discovered", "name": "tests::multiplication_tests::when_both_operands_are_negative", "ignore": false, "ignore_message": "", "source_path": "tests/example.rs", "start_line": 15, "start_col": 5, "end_line": 15, "end_col": 61 }
{ "type": "test", "event": "discovered", "name": "tests::multiplication_tests::when_both_operands_are_positive", "ignore": false, "ignore_message": "", "source_path": "tests/example.rs", "start_line": 16, "start_col": 5, "end_line": 16, "end_col": 61 }
{ "type": "test", "event": "discovered", "name": "tests::multiplication_tests::when_operands_are_swapped", "ignore": false, "ignore_message": "", "source_path": "tests/example.rs", "start_line": 17, "start_col": 5, "end_line": 17, "end_col": 55 }
@luke-biel
Copy link
Collaborator

These changes look very useful, it always was a pain point for me that test cases are grouped. I don't mind using Ident::set_span here. If you can put together a PR which changes this behavior we'll be more than happy to accept it :)

@luke-biel
Copy link
Collaborator

Merged, will release tmrw

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 a pull request may close this issue.

2 participants