Chih-Hung Hsieh | 467ea21 | 2019-10-31 17:36:47 -0700 | [diff] [blame] | 1 | mod features; |
| 2 | |
| 3 | #[macro_use] |
| 4 | mod macros; |
| 5 | |
| 6 | use syn::parse::Parser; |
| 7 | use syn::{Attribute, Meta}; |
| 8 | |
| 9 | #[test] |
| 10 | fn test_meta_item_word() { |
| 11 | let meta = test("#[foo]"); |
| 12 | |
| 13 | snapshot!(meta, @r###" |
| 14 | Path(Path { |
| 15 | segments: [ |
| 16 | PathSegment { |
| 17 | ident: "foo", |
| 18 | arguments: None, |
| 19 | }, |
| 20 | ], |
| 21 | }) |
| 22 | "###); |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn test_meta_item_name_value() { |
| 27 | let meta = test("#[foo = 5]"); |
| 28 | |
| 29 | snapshot!(meta, @r###" |
| 30 | Meta::NameValue { |
| 31 | path: Path { |
| 32 | segments: [ |
| 33 | PathSegment { |
| 34 | ident: "foo", |
| 35 | arguments: None, |
| 36 | }, |
| 37 | ], |
| 38 | }, |
| 39 | lit: 5, |
| 40 | } |
| 41 | "###); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn test_meta_item_bool_value() { |
| 46 | let meta = test("#[foo = true]"); |
| 47 | |
| 48 | snapshot!(meta, @r###" |
| 49 | Meta::NameValue { |
| 50 | path: Path { |
| 51 | segments: [ |
| 52 | PathSegment { |
| 53 | ident: "foo", |
| 54 | arguments: None, |
| 55 | }, |
| 56 | ], |
| 57 | }, |
| 58 | lit: Lit::Bool { |
| 59 | value: true, |
| 60 | }, |
| 61 | } |
| 62 | "###); |
| 63 | |
| 64 | let meta = test("#[foo = false]"); |
| 65 | |
| 66 | snapshot!(meta, @r###" |
| 67 | Meta::NameValue { |
| 68 | path: Path { |
| 69 | segments: [ |
| 70 | PathSegment { |
| 71 | ident: "foo", |
| 72 | arguments: None, |
| 73 | }, |
| 74 | ], |
| 75 | }, |
| 76 | lit: Lit::Bool { |
| 77 | value: false, |
| 78 | }, |
| 79 | } |
| 80 | "###); |
| 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn test_meta_item_list_lit() { |
| 85 | let meta = test("#[foo(5)]"); |
| 86 | |
| 87 | snapshot!(meta, @r###" |
| 88 | Meta::List { |
| 89 | path: Path { |
| 90 | segments: [ |
| 91 | PathSegment { |
| 92 | ident: "foo", |
| 93 | arguments: None, |
| 94 | }, |
| 95 | ], |
| 96 | }, |
| 97 | nested: [ |
| 98 | Lit(5), |
| 99 | ], |
| 100 | } |
| 101 | "###); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn test_meta_item_list_word() { |
| 106 | let meta = test("#[foo(bar)]"); |
| 107 | |
| 108 | snapshot!(meta, @r###" |
| 109 | Meta::List { |
| 110 | path: Path { |
| 111 | segments: [ |
| 112 | PathSegment { |
| 113 | ident: "foo", |
| 114 | arguments: None, |
| 115 | }, |
| 116 | ], |
| 117 | }, |
| 118 | nested: [ |
| 119 | Meta(Path(Path { |
| 120 | segments: [ |
| 121 | PathSegment { |
| 122 | ident: "bar", |
| 123 | arguments: None, |
| 124 | }, |
| 125 | ], |
| 126 | })), |
| 127 | ], |
| 128 | } |
| 129 | "###); |
| 130 | } |
| 131 | |
| 132 | #[test] |
| 133 | fn test_meta_item_list_name_value() { |
| 134 | let meta = test("#[foo(bar = 5)]"); |
| 135 | |
| 136 | snapshot!(meta, @r###" |
| 137 | Meta::List { |
| 138 | path: Path { |
| 139 | segments: [ |
| 140 | PathSegment { |
| 141 | ident: "foo", |
| 142 | arguments: None, |
| 143 | }, |
| 144 | ], |
| 145 | }, |
| 146 | nested: [ |
| 147 | Meta(Meta::NameValue { |
| 148 | path: Path { |
| 149 | segments: [ |
| 150 | PathSegment { |
| 151 | ident: "bar", |
| 152 | arguments: None, |
| 153 | }, |
| 154 | ], |
| 155 | }, |
| 156 | lit: 5, |
| 157 | }), |
| 158 | ], |
| 159 | } |
| 160 | "###); |
| 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn test_meta_item_list_bool_value() { |
| 165 | let meta = test("#[foo(bar = true)]"); |
| 166 | |
| 167 | snapshot!(meta, @r###" |
| 168 | Meta::List { |
| 169 | path: Path { |
| 170 | segments: [ |
| 171 | PathSegment { |
| 172 | ident: "foo", |
| 173 | arguments: None, |
| 174 | }, |
| 175 | ], |
| 176 | }, |
| 177 | nested: [ |
| 178 | Meta(Meta::NameValue { |
| 179 | path: Path { |
| 180 | segments: [ |
| 181 | PathSegment { |
| 182 | ident: "bar", |
| 183 | arguments: None, |
| 184 | }, |
| 185 | ], |
| 186 | }, |
| 187 | lit: Lit::Bool { |
| 188 | value: true, |
| 189 | }, |
| 190 | }), |
| 191 | ], |
| 192 | } |
| 193 | "###); |
| 194 | } |
| 195 | |
| 196 | #[test] |
| 197 | fn test_meta_item_multiple() { |
| 198 | let meta = test("#[foo(word, name = 5, list(name2 = 6), word2)]"); |
| 199 | |
| 200 | snapshot!(meta, @r###" |
| 201 | Meta::List { |
| 202 | path: Path { |
| 203 | segments: [ |
| 204 | PathSegment { |
| 205 | ident: "foo", |
| 206 | arguments: None, |
| 207 | }, |
| 208 | ], |
| 209 | }, |
| 210 | nested: [ |
| 211 | Meta(Path(Path { |
| 212 | segments: [ |
| 213 | PathSegment { |
| 214 | ident: "word", |
| 215 | arguments: None, |
| 216 | }, |
| 217 | ], |
| 218 | })), |
| 219 | Meta(Meta::NameValue { |
| 220 | path: Path { |
| 221 | segments: [ |
| 222 | PathSegment { |
| 223 | ident: "name", |
| 224 | arguments: None, |
| 225 | }, |
| 226 | ], |
| 227 | }, |
| 228 | lit: 5, |
| 229 | }), |
| 230 | Meta(Meta::List { |
| 231 | path: Path { |
| 232 | segments: [ |
| 233 | PathSegment { |
| 234 | ident: "list", |
| 235 | arguments: None, |
| 236 | }, |
| 237 | ], |
| 238 | }, |
| 239 | nested: [ |
| 240 | Meta(Meta::NameValue { |
| 241 | path: Path { |
| 242 | segments: [ |
| 243 | PathSegment { |
| 244 | ident: "name2", |
| 245 | arguments: None, |
| 246 | }, |
| 247 | ], |
| 248 | }, |
| 249 | lit: 6, |
| 250 | }), |
| 251 | ], |
| 252 | }), |
| 253 | Meta(Path(Path { |
| 254 | segments: [ |
| 255 | PathSegment { |
| 256 | ident: "word2", |
| 257 | arguments: None, |
| 258 | }, |
| 259 | ], |
| 260 | })), |
| 261 | ], |
| 262 | } |
| 263 | "###); |
| 264 | } |
| 265 | |
| 266 | #[test] |
| 267 | fn test_bool_lit() { |
| 268 | let meta = test("#[foo(true)]"); |
| 269 | |
| 270 | snapshot!(meta, @r###" |
| 271 | Meta::List { |
| 272 | path: Path { |
| 273 | segments: [ |
| 274 | PathSegment { |
| 275 | ident: "foo", |
| 276 | arguments: None, |
| 277 | }, |
| 278 | ], |
| 279 | }, |
| 280 | nested: [ |
| 281 | Lit(Lit::Bool { |
| 282 | value: true, |
| 283 | }), |
| 284 | ], |
| 285 | } |
| 286 | "###); |
| 287 | } |
| 288 | |
| 289 | fn test(input: &str) -> Meta { |
| 290 | let attrs = Attribute::parse_outer.parse_str(input).unwrap(); |
| 291 | |
| 292 | assert_eq!(attrs.len(), 1); |
| 293 | let attr = attrs.into_iter().next().unwrap(); |
| 294 | |
| 295 | attr.parse_meta().unwrap() |
| 296 | } |