Skip to content

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

@rpwachowski

Description

@rpwachowski

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 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions