| 1 |
#![allow( |
| 2 |
clippy::elidable_lifetime_names, |
| 3 |
clippy::needless_lifetimes, |
| 4 |
clippy::uninlined_format_args |
| 5 |
)] |
| 6 |
|
| 7 |
#[macro_use] |
| 8 |
mod snapshot; |
| 9 |
|
| 10 |
mod debug; |
| 11 |
|
| 12 |
use syn::parse::Parser; |
| 13 |
use syn::{Attribute, Meta}; |
| 14 |
|
| 15 |
#[test] |
| 16 |
fn test_meta_item_word() { |
| 17 |
let meta = test("#[foo]"); |
| 18 |
|
| 19 |
snapshot!(meta, @r#" |
| 20 |
Meta::Path { |
| 21 |
segments: [ |
| 22 |
PathSegment { |
| 23 |
ident: "foo", |
| 24 |
}, |
| 25 |
], |
| 26 |
} |
| 27 |
"#); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_meta_item_name_value() { |
| 32 |
let meta = test("#[foo = 5]"); |
| 33 |
|
| 34 |
snapshot!(meta, @r#" |
| 35 |
Meta::NameValue { |
| 36 |
path: Path { |
| 37 |
segments: [ |
| 38 |
PathSegment { |
| 39 |
ident: "foo", |
| 40 |
}, |
| 41 |
], |
| 42 |
}, |
| 43 |
value: Expr::Lit { |
| 44 |
lit: 5, |
| 45 |
}, |
| 46 |
} |
| 47 |
"#); |
| 48 |
} |
| 49 |
|
| 50 |
#[test] |
| 51 |
fn test_meta_item_bool_value() { |
| 52 |
let meta = test("#[foo = true]"); |
| 53 |
|
| 54 |
snapshot!(meta, @r#" |
| 55 |
Meta::NameValue { |
| 56 |
path: Path { |
| 57 |
segments: [ |
| 58 |
PathSegment { |
| 59 |
ident: "foo", |
| 60 |
}, |
| 61 |
], |
| 62 |
}, |
| 63 |
value: Expr::Lit { |
| 64 |
lit: Lit::Bool { |
| 65 |
value: true, |
| 66 |
}, |
| 67 |
}, |
| 68 |
} |
| 69 |
"#); |
| 70 |
|
| 71 |
let meta = test("#[foo = false]"); |
| 72 |
|
| 73 |
snapshot!(meta, @r#" |
| 74 |
Meta::NameValue { |
| 75 |
path: Path { |
| 76 |
segments: [ |
| 77 |
PathSegment { |
| 78 |
ident: "foo", |
| 79 |
}, |
| 80 |
], |
| 81 |
}, |
| 82 |
value: Expr::Lit { |
| 83 |
lit: Lit::Bool { |
| 84 |
value: false, |
| 85 |
}, |
| 86 |
}, |
| 87 |
} |
| 88 |
"#); |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn test_meta_item_list_lit() { |
| 93 |
let meta = test("#[foo(5)]"); |
| 94 |
|
| 95 |
snapshot!(meta, @r#" |
| 96 |
Meta::List { |
| 97 |
path: Path { |
| 98 |
segments: [ |
| 99 |
PathSegment { |
| 100 |
ident: "foo", |
| 101 |
}, |
| 102 |
], |
| 103 |
}, |
| 104 |
delimiter: MacroDelimiter::Paren, |
| 105 |
tokens: TokenStream(`5`), |
| 106 |
} |
| 107 |
"#); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn test_meta_item_list_word() { |
| 112 |
let meta = test("#[foo(bar)]"); |
| 113 |
|
| 114 |
snapshot!(meta, @r#" |
| 115 |
Meta::List { |
| 116 |
path: Path { |
| 117 |
segments: [ |
| 118 |
PathSegment { |
| 119 |
ident: "foo", |
| 120 |
}, |
| 121 |
], |
| 122 |
}, |
| 123 |
delimiter: MacroDelimiter::Paren, |
| 124 |
tokens: TokenStream(`bar`), |
| 125 |
} |
| 126 |
"#); |
| 127 |
} |
| 128 |
|
| 129 |
#[test] |
| 130 |
fn test_meta_item_list_name_value() { |
| 131 |
let meta = test("#[foo(bar = 5)]"); |
| 132 |
|
| 133 |
snapshot!(meta, @r#" |
| 134 |
Meta::List { |
| 135 |
path: Path { |
| 136 |
segments: [ |
| 137 |
PathSegment { |
| 138 |
ident: "foo", |
| 139 |
}, |
| 140 |
], |
| 141 |
}, |
| 142 |
delimiter: MacroDelimiter::Paren, |
| 143 |
tokens: TokenStream(`bar = 5`), |
| 144 |
} |
| 145 |
"#); |
| 146 |
} |
| 147 |
|
| 148 |
#[test] |
| 149 |
fn test_meta_item_list_bool_value() { |
| 150 |
let meta = test("#[foo(bar = true)]"); |
| 151 |
|
| 152 |
snapshot!(meta, @r#" |
| 153 |
Meta::List { |
| 154 |
path: Path { |
| 155 |
segments: [ |
| 156 |
PathSegment { |
| 157 |
ident: "foo", |
| 158 |
}, |
| 159 |
], |
| 160 |
}, |
| 161 |
delimiter: MacroDelimiter::Paren, |
| 162 |
tokens: TokenStream(`bar = true`), |
| 163 |
} |
| 164 |
"#); |
| 165 |
} |
| 166 |
|
| 167 |
#[test] |
| 168 |
fn test_meta_item_multiple() { |
| 169 |
let meta = test("#[foo(word, name = 5, list(name2 = 6), word2)]"); |
| 170 |
|
| 171 |
snapshot!(meta, @r#" |
| 172 |
Meta::List { |
| 173 |
path: Path { |
| 174 |
segments: [ |
| 175 |
PathSegment { |
| 176 |
ident: "foo", |
| 177 |
}, |
| 178 |
], |
| 179 |
}, |
| 180 |
delimiter: MacroDelimiter::Paren, |
| 181 |
tokens: TokenStream(`word , name = 5 , list (name2 = 6) , word2`), |
| 182 |
} |
| 183 |
"#); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn test_bool_lit() { |
| 188 |
let meta = test("#[foo(true)]"); |
| 189 |
|
| 190 |
snapshot!(meta, @r#" |
| 191 |
Meta::List { |
| 192 |
path: Path { |
| 193 |
segments: [ |
| 194 |
PathSegment { |
| 195 |
ident: "foo", |
| 196 |
}, |
| 197 |
], |
| 198 |
}, |
| 199 |
delimiter: MacroDelimiter::Paren, |
| 200 |
tokens: TokenStream(`true`), |
| 201 |
} |
| 202 |
"#); |
| 203 |
} |
| 204 |
|
| 205 |
#[test] |
| 206 |
fn test_negative_lit() { |
| 207 |
let meta = test("#[form(min = -1, max = 200)]"); |
| 208 |
|
| 209 |
snapshot!(meta, @r#" |
| 210 |
Meta::List { |
| 211 |
path: Path { |
| 212 |
segments: [ |
| 213 |
PathSegment { |
| 214 |
ident: "form", |
| 215 |
}, |
| 216 |
], |
| 217 |
}, |
| 218 |
delimiter: MacroDelimiter::Paren, |
| 219 |
tokens: TokenStream(`min = - 1 , max = 200`), |
| 220 |
} |
| 221 |
"#); |
| 222 |
} |
| 223 |
|
| 224 |
fn test(input: &str) -> Meta { |
| 225 |
let attrs = Attribute::parse_outer.parse_str(input).unwrap(); |
| 226 |
|
| 227 |
assert_eq!(attrs.len(), 1); |
| 228 |
let attr = attrs.into_iter().next().unwrap(); |
| 229 |
|
| 230 |
attr.meta |
| 231 |
} |