David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 1 | // Copyright 2018 Syn Developers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | extern crate proc_macro2; |
| 10 | extern crate syn; |
| 11 | |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 12 | use proc_macro2::{Span, TokenStream, Ident}; |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 13 | use std::str::FromStr; |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 14 | use syn::synom::ParseError; |
| 15 | |
| 16 | fn parse(s: &str) -> Result<Ident, ParseError> { |
| 17 | syn::parse2(TokenStream::from_str(s).unwrap()) |
| 18 | } |
| 19 | |
| 20 | fn new(s: &str) -> Ident { |
| 21 | Ident::new(s, Span::call_site()) |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn ident_parse() { |
| 26 | parse("String").unwrap(); |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn ident_parse_keyword() { |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 31 | parse("abstract").unwrap(); |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn ident_parse_empty() { |
| 36 | parse("").unwrap_err(); |
| 37 | } |
| 38 | |
| 39 | #[test] |
| 40 | fn ident_parse_lifetime() { |
| 41 | parse("'static").unwrap_err(); |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn ident_parse_underscore() { |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 46 | parse("_").unwrap(); |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn ident_parse_number() { |
| 51 | parse("255").unwrap_err(); |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn ident_parse_invalid() { |
| 56 | parse("a#").unwrap_err(); |
| 57 | } |
| 58 | |
| 59 | #[test] |
| 60 | fn ident_new() { |
| 61 | new("String"); |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn ident_new_keyword() { |
| 66 | new("abstract"); |
| 67 | } |
| 68 | |
| 69 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 70 | #[should_panic(expected = "use Option<Ident>")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 71 | fn ident_new_empty() { |
| 72 | new(""); |
| 73 | } |
| 74 | |
| 75 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 76 | #[should_panic(expected = "not a valid Ident")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 77 | fn ident_new_lifetime() { |
| 78 | new("'static"); |
| 79 | } |
| 80 | |
| 81 | #[test] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 82 | fn ident_new_underscore() { |
| 83 | new("_"); |
| 84 | } |
| 85 | |
| 86 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 87 | #[should_panic(expected = "use Literal instead")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 88 | fn ident_new_number() { |
| 89 | new("255"); |
| 90 | } |
| 91 | |
| 92 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame^] | 93 | #[should_panic(expected = "\"a#\" is not a valid Ident")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 94 | fn ident_new_invalid() { |
| 95 | new("a#"); |
| 96 | } |