gather_all_code_from_crates/
extract_attributes.rs

1crate::ix!();
2
3/// Extracts attributes from an iterator of `ast::Attr`. Returns `(attributes, is_test)`.
4/// `is_test` is true if any attribute is `#[test]`.
5pub fn extract_attributes(attrs: impl Iterator<Item=ast::Attr>) -> (Vec<String>, bool) {
6    let mut attributes = Vec::new();
7    let mut is_test = false;
8    for attr in attrs {
9        let txt = attr.syntax().text().to_string();
10        attributes.push(txt.trim().to_string());
11        if txt.contains("#[test]") {
12            is_test = true;
13        }
14    }
15    (attributes, is_test)
16}
17
18#[cfg(test)]
19mod extract_attributes_tests {
20    use super::*;
21
22    #[test]
23    fn test_extract_attributes() {
24        let code = r#"
25#[inline]
26#[test]
27#[some_attr]
28fn myfunc() {}
29"#;
30        let syntax = parse_source(code);
31        let fn_node = syntax.descendants().find_map(ast::Fn::cast).unwrap();
32        let (attrs, is_test) = extract_attributes(fn_node.attrs());
33        assert_eq!(attrs.len(), 3);
34        assert!(attrs.iter().any(|a| a.contains("#[test]")));
35        assert!(is_test);
36    }
37}