crate::ix!();
pub trait IsShouldPanicAttr {
fn is_should_panic_attr(&self) -> bool;
}
impl IsShouldPanicAttr for syn::Attribute {
fn is_should_panic_attr(&self) -> bool {
if self.path().is_ident("should_panic") {
let _ = self.parse_nested_meta(|meta| {
if meta.path.is_ident("expected") {
let _ = meta.value()?.parse::<syn::LitStr>()?;
}
Ok(())
});
return true;
}
false
}
}
#[cfg(test)]
mod should_panic_attr_tests {
use super::*;
use syn::{Attribute, parse_quote};
#[test]
fn test_is_should_panic_attr_simple() {
let attr: Attribute = parse_quote!(#[should_panic]);
assert!(attr.is_should_panic_attr(), "Expected the attribute to be recognized as `should_panic`.");
}
#[test]
fn test_is_should_panic_attr_with_expected() {
let attr: Attribute = parse_quote!(#[should_panic(expected = "error")]);
assert!(attr.is_should_panic_attr(), "Expected the attribute to be recognized as `should_panic`.");
}
#[test]
fn test_is_should_panic_attr_invalid_format() {
let attr: Attribute = parse_quote!(#[should_panic(invalid = "some_value")]);
assert!(attr.is_should_panic_attr(), "Even with an invalid argument, should be recognized as `should_panic`.");
}
#[test]
fn test_is_not_should_panic_attr() {
let attr: Attribute = parse_quote!(#[not_should_panic]);
assert!(!attr.is_should_panic_attr(), "Expected the attribute to not be recognized as `should_panic`.");
}
#[test]
fn test_is_should_panic_attr_with_multiple_arguments() {
let attr: Attribute = parse_quote!(#[should_panic(expected = "error", another_arg = "value")]);
assert!(attr.is_should_panic_attr(), "Expected the attribute to be recognized as `should_panic` even with multiple arguments.");
}
#[test]
fn test_is_not_should_panic_attr_with_complex_meta() {
let attr: Attribute = parse_quote!(#[some_other_attr(arg = "value")]);
assert!(!attr.is_should_panic_attr(), "Expected the attribute to not be recognized as `should_panic`.");
}
#[test]
fn test_is_not_should_panic_attr_empty() {
let attr: Attribute = parse_quote!(#[some_attr]);
assert!(!attr.is_should_panic_attr(), "Expected the attribute to not be recognized as `should_panic`.");
}
}