David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 1 | extern crate syn; |
| 2 | |
| 3 | mod features; |
| 4 | |
| 5 | #[macro_use] |
| 6 | mod macros; |
| 7 | |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 8 | use syn::{Meta, MetaList, MetaNameValue, NestedMeta}; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 9 | |
| 10 | #[test] |
| 11 | fn test_parse_meta_item_word() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 12 | let input = "hello"; |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 13 | |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 14 | snapshot!(input as Meta, @r###"Word("hello")"###); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn test_parse_meta_name_value() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 19 | let input = "foo = 5"; |
| 20 | let (inner, meta) = (input, input); |
| 21 | |
| 22 | snapshot!(inner as MetaNameValue, @r###" |
| 23 | ⋮MetaNameValue { |
| 24 | ⋮ ident: "foo", |
| 25 | ⋮ lit: 5, |
| 26 | ⋮} |
| 27 | "###); |
| 28 | |
| 29 | snapshot!(meta as Meta, @r###" |
| 30 | ⋮Meta::NameValue { |
| 31 | ⋮ ident: "foo", |
| 32 | ⋮ lit: 5, |
| 33 | ⋮} |
| 34 | "###); |
| 35 | |
| 36 | assert_eq!(meta, inner.into()); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn test_parse_meta_name_value_with_keyword() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 41 | let input = "static = 5"; |
| 42 | let (inner, meta) = (input, input); |
| 43 | |
| 44 | snapshot!(inner as MetaNameValue, @r###" |
| 45 | ⋮MetaNameValue { |
| 46 | ⋮ ident: "static", |
| 47 | ⋮ lit: 5, |
| 48 | ⋮} |
| 49 | "###); |
| 50 | |
| 51 | snapshot!(meta as Meta, @r###" |
| 52 | ⋮Meta::NameValue { |
| 53 | ⋮ ident: "static", |
| 54 | ⋮ lit: 5, |
| 55 | ⋮} |
| 56 | "###); |
| 57 | |
| 58 | assert_eq!(meta, inner.into()); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn test_parse_meta_name_value_with_bool() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 63 | let input = "true = 5"; |
| 64 | let (inner, meta) = (input, input); |
| 65 | |
| 66 | snapshot!(inner as MetaNameValue, @r###" |
| 67 | ⋮MetaNameValue { |
| 68 | ⋮ ident: "true", |
| 69 | ⋮ lit: 5, |
| 70 | ⋮} |
| 71 | "###); |
| 72 | |
| 73 | snapshot!(meta as Meta, @r###" |
| 74 | ⋮Meta::NameValue { |
| 75 | ⋮ ident: "true", |
| 76 | ⋮ lit: 5, |
| 77 | ⋮} |
| 78 | "###); |
| 79 | |
| 80 | assert_eq!(meta, inner.into()); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #[test] |
| 84 | fn test_parse_meta_item_list_lit() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 85 | let input = "foo(5)"; |
| 86 | let (inner, meta) = (input, input); |
| 87 | |
| 88 | snapshot!(inner as MetaList, @r###" |
| 89 | ⋮MetaList { |
| 90 | ⋮ ident: "foo", |
| 91 | ⋮ nested: [ |
| 92 | ⋮ Literal(5), |
| 93 | ⋮ ], |
| 94 | ⋮} |
| 95 | "###); |
| 96 | |
| 97 | snapshot!(meta as Meta, @r###" |
| 98 | ⋮Meta::List { |
| 99 | ⋮ ident: "foo", |
| 100 | ⋮ nested: [ |
| 101 | ⋮ Literal(5), |
| 102 | ⋮ ], |
| 103 | ⋮} |
| 104 | "###); |
| 105 | |
| 106 | assert_eq!(meta, inner.into()); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn test_parse_meta_item_multiple() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 111 | let input = "foo(word, name = 5, list(name2 = 6), word2)"; |
| 112 | let (inner, meta) = (input, input); |
| 113 | |
| 114 | snapshot!(inner as MetaList, @r###" |
| 115 | ⋮MetaList { |
| 116 | ⋮ ident: "foo", |
| 117 | ⋮ nested: [ |
| 118 | ⋮ Meta(Word("word")), |
| 119 | ⋮ Meta(Meta::NameValue { |
| 120 | ⋮ ident: "name", |
| 121 | ⋮ lit: 5, |
| 122 | ⋮ }), |
| 123 | ⋮ Meta(Meta::List { |
| 124 | ⋮ ident: "list", |
| 125 | ⋮ nested: [ |
| 126 | ⋮ Meta(Meta::NameValue { |
| 127 | ⋮ ident: "name2", |
| 128 | ⋮ lit: 6, |
| 129 | ⋮ }), |
| 130 | ⋮ ], |
| 131 | ⋮ }), |
| 132 | ⋮ Meta(Word("word2")), |
| 133 | ⋮ ], |
| 134 | ⋮} |
| 135 | "###); |
| 136 | |
| 137 | snapshot!(meta as Meta, @r###" |
| 138 | ⋮Meta::List { |
| 139 | ⋮ ident: "foo", |
| 140 | ⋮ nested: [ |
| 141 | ⋮ Meta(Word("word")), |
| 142 | ⋮ Meta(Meta::NameValue { |
| 143 | ⋮ ident: "name", |
| 144 | ⋮ lit: 5, |
| 145 | ⋮ }), |
| 146 | ⋮ Meta(Meta::List { |
| 147 | ⋮ ident: "list", |
| 148 | ⋮ nested: [ |
| 149 | ⋮ Meta(Meta::NameValue { |
| 150 | ⋮ ident: "name2", |
| 151 | ⋮ lit: 6, |
| 152 | ⋮ }), |
| 153 | ⋮ ], |
| 154 | ⋮ }), |
| 155 | ⋮ Meta(Word("word2")), |
| 156 | ⋮ ], |
| 157 | ⋮} |
| 158 | "###); |
| 159 | |
| 160 | assert_eq!(meta, inner.into()); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | #[test] |
| 164 | fn test_parse_nested_meta() { |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 165 | let input = "5"; |
| 166 | snapshot!(input as NestedMeta, @"Literal(5)"); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 167 | |
David Tolnay | fcd53cf | 2019-05-09 13:06:59 -0700 | [diff] [blame] | 168 | let input = "list(name2 = 6)"; |
| 169 | snapshot!(input as NestedMeta, @r###" |
| 170 | ⋮Meta(Meta::List { |
| 171 | ⋮ ident: "list", |
| 172 | ⋮ nested: [ |
| 173 | ⋮ Meta(Meta::NameValue { |
| 174 | ⋮ ident: "name2", |
| 175 | ⋮ lit: 6, |
| 176 | ⋮ }), |
| 177 | ⋮ ], |
| 178 | ⋮}) |
| 179 | "###); |
David Tolnay | 7d8b331 | 2019-03-10 01:26:11 -0800 | [diff] [blame] | 180 | } |