| 1 | use proc_macro2::{Ident, Span, TokenStream}; |
| 2 | use std::str::FromStr; |
| 3 | use syn::Result; |
| 4 | |
| 5 | fn parse(s: &str) -> Result<Ident> { |
| 6 | syn::parse2(TokenStream::from_str(s).unwrap()) |
| 7 | } |
| 8 | |
| 9 | fn new(s: &str) -> Ident { |
| 10 | Ident::new(s, Span::call_site()) |
| 11 | } |
| 12 | |
| 13 | #[test] |
| 14 | fn ident_parse() { |
| 15 | parse("String" ).unwrap(); |
| 16 | } |
| 17 | |
| 18 | #[test] |
| 19 | fn ident_parse_keyword() { |
| 20 | parse("abstract" ).unwrap_err(); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn ident_parse_empty() { |
| 25 | parse("" ).unwrap_err(); |
| 26 | } |
| 27 | |
| 28 | #[test] |
| 29 | fn ident_parse_lifetime() { |
| 30 | parse("'static" ).unwrap_err(); |
| 31 | } |
| 32 | |
| 33 | #[test] |
| 34 | fn ident_parse_underscore() { |
| 35 | parse("_" ).unwrap_err(); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn ident_parse_number() { |
| 40 | parse("255" ).unwrap_err(); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn ident_parse_invalid() { |
| 45 | parse("a#" ).unwrap_err(); |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn ident_new() { |
| 50 | new("String" ); |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn ident_new_keyword() { |
| 55 | new("abstract" ); |
| 56 | } |
| 57 | |
| 58 | #[test] |
| 59 | #[should_panic (expected = "use Option<Ident>" )] |
| 60 | fn ident_new_empty() { |
| 61 | new("" ); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | #[should_panic (expected = "not a valid Ident" )] |
| 66 | fn ident_new_lifetime() { |
| 67 | new("'static" ); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn ident_new_underscore() { |
| 72 | new("_" ); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | #[should_panic (expected = "use Literal instead" )] |
| 77 | fn ident_new_number() { |
| 78 | new("255" ); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | #[should_panic (expected = " \"a# \" is not a valid Ident" )] |
| 83 | fn ident_new_invalid() { |
| 84 | new("a#" ); |
| 85 | } |
| 86 | |