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 | |
David Tolnay | c3f9856 | 2018-11-02 08:55:05 -0700 | [diff] [blame] | 12 | mod features; |
| 13 | |
David Tolnay | 65fb566 | 2018-05-20 20:02:28 -0700 | [diff] [blame] | 14 | use proc_macro2::{Ident, Span, TokenStream}; |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 15 | use std::str::FromStr; |
David Tolnay | b625418 | 2018-08-25 08:44:54 -0400 | [diff] [blame] | 16 | use syn::parse::Error; |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 17 | |
David Tolnay | ad4b247 | 2018-08-25 08:25:24 -0400 | [diff] [blame] | 18 | fn parse(s: &str) -> Result<Ident, Error> { |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 19 | syn::parse2(TokenStream::from_str(s).unwrap()) |
| 20 | } |
| 21 | |
| 22 | fn new(s: &str) -> Ident { |
| 23 | Ident::new(s, Span::call_site()) |
| 24 | } |
| 25 | |
| 26 | #[test] |
| 27 | fn ident_parse() { |
| 28 | parse("String").unwrap(); |
| 29 | } |
| 30 | |
| 31 | #[test] |
| 32 | fn ident_parse_keyword() { |
Alex Crichton | 3fec393 | 2018-05-17 12:09:13 -0700 | [diff] [blame] | 33 | parse("abstract").unwrap_err(); |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | #[test] |
| 37 | fn ident_parse_empty() { |
| 38 | parse("").unwrap_err(); |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn ident_parse_lifetime() { |
| 43 | parse("'static").unwrap_err(); |
| 44 | } |
| 45 | |
| 46 | #[test] |
| 47 | fn ident_parse_underscore() { |
Alex Crichton | 3fec393 | 2018-05-17 12:09:13 -0700 | [diff] [blame] | 48 | parse("_").unwrap_err(); |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn ident_parse_number() { |
| 53 | parse("255").unwrap_err(); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn ident_parse_invalid() { |
| 58 | parse("a#").unwrap_err(); |
| 59 | } |
| 60 | |
| 61 | #[test] |
| 62 | fn ident_new() { |
| 63 | new("String"); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn ident_new_keyword() { |
| 68 | new("abstract"); |
| 69 | } |
| 70 | |
| 71 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame] | 72 | #[should_panic(expected = "use Option<Ident>")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 73 | fn ident_new_empty() { |
| 74 | new(""); |
| 75 | } |
| 76 | |
| 77 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame] | 78 | #[should_panic(expected = "not a valid Ident")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 79 | fn ident_new_lifetime() { |
| 80 | new("'static"); |
| 81 | } |
| 82 | |
| 83 | #[test] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 84 | fn ident_new_underscore() { |
| 85 | new("_"); |
| 86 | } |
| 87 | |
| 88 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame] | 89 | #[should_panic(expected = "use Literal instead")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 90 | fn ident_new_number() { |
| 91 | new("255"); |
| 92 | } |
| 93 | |
| 94 | #[test] |
Alex Crichton | 499ed64 | 2018-05-17 11:02:02 -0700 | [diff] [blame] | 95 | #[should_panic(expected = "\"a#\" is not a valid Ident")] |
David Tolnay | d741aa8 | 2018-04-04 07:53:04 -0700 | [diff] [blame] | 96 | fn ident_new_invalid() { |
| 97 | new("a#"); |
| 98 | } |