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

#[should_panic] ignores function-like macros #88430

Open
dylni opened this issue Aug 28, 2021 · 2 comments
Open

#[should_panic] ignores function-like macros #88430

dylni opened this issue Aug 28, 2021 · 2 comments
Labels
A-libtest Area: #[test] related C-bug Category: This is a bug.

Comments

@dylni
Copy link
Contributor

dylni commented Aug 28, 2021

I tried this code:

#[cfg(test)]
mod tests {
    #[test]
    #[should_panic = concat!("a")]
    fn test() {
        panic!("b");
    }
}

fn main() {}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1268154aa9d34acf6833b9affa7e6b81

I expected to see this happen:

running 1 test
test tests::test - should panic ... FAILED

failures:

---- tests::test stdout ----
thread 'tests::test' panicked at 'b', src/lib.rs:6:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
note: panic did not contain expected string
      panic message: `"b"`,
 expected substring: `"a"`

failures:
    tests::test

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Instead, this happened:

running 1 test
test tests::test - should panic ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s


running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

Changing concat!("a") to "a" shows the expected error:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e0aa41c95b4cd19e82332b6f030987f5

@dylni dylni added the C-bug Category: This is a bug. label Aug 28, 2021
@dylni
Copy link
Contributor Author

dylni commented Aug 29, 2021

@rustbot modify labels +A-libtest

@rustbot rustbot added the A-libtest Area: #[test] related label Aug 29, 2021
@edmorley
Copy link
Contributor

edmorley commented Jul 6, 2022

I encountered this too.

My use-case is wanting to test a panic error message that contains an absolute path (which must be absolute due to the nature of what the error is trying to convey) which will vary based on what machine ran the test.

Since should_panic does not support globs/regex, I'm not able to test the whole error message, only the part up to the absolute path (or the part after it, but not both).

If function-like macros were supported here, I could use concat! + env!("CARGO_MANIFEST_DIR") etc to construct the absolute path in should_match's expected value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-libtest Area: #[test] related C-bug Category: This is a bug.
Projects
Status: No status
Development

No branches or pull requests

3 participants