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 | |
| 12 | use std::str::FromStr; |
| 13 | use proc_macro2::{TokenStream, Span}; |
| 14 | use syn::Ident; |
| 15 | use syn::synom::ParseError; |
| 16 | |
| 17 | fn parse(s: &str) -> Result<Ident, ParseError> { |
| 18 | syn::parse2(TokenStream::from_str(s).unwrap()) |
| 19 | } |
| 20 | |
| 21 | fn new(s: &str) -> Ident { |
| 22 | Ident::new(s, Span::call_site()) |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn ident_parse() { |
| 27 | parse("String").unwrap(); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn ident_parse_keyword() { |
| 32 | parse("abstract").unwrap_err(); |
| 33 | } |
| 34 | |
| 35 | #[test] |
| 36 | fn ident_parse_empty() { |
| 37 | parse("").unwrap_err(); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn ident_parse_lifetime() { |
| 42 | parse("'static").unwrap_err(); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn ident_parse_underscore() { |
| 47 | parse("_").unwrap_err(); |
| 48 | } |
| 49 | |
| 50 | #[test] |
| 51 | fn ident_parse_number() { |
| 52 | parse("255").unwrap_err(); |
| 53 | } |
| 54 | |
| 55 | #[test] |
| 56 | fn ident_parse_invalid() { |
| 57 | parse("a#").unwrap_err(); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn ident_new() { |
| 62 | new("String"); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn ident_new_keyword() { |
| 67 | new("abstract"); |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | #[should_panic(expected = "ident is not allowed to be empty; use Option<Ident>")] |
| 72 | fn ident_new_empty() { |
| 73 | new(""); |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | #[should_panic(expected = "ident is not allowed to be a lifetime; use syn::Lifetime")] |
| 78 | fn ident_new_lifetime() { |
| 79 | new("'static"); |
| 80 | } |
| 81 | |
| 82 | #[test] |
| 83 | #[should_panic(expected = "`_` is not a valid ident; use syn::token::Underscore")] |
| 84 | fn ident_new_underscore() { |
| 85 | new("_"); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | #[should_panic(expected = "ident cannot be a number, use syn::Index instead")] |
| 90 | fn ident_new_number() { |
| 91 | new("255"); |
| 92 | } |
| 93 | |
| 94 | #[test] |
| 95 | #[should_panic(expected = "\"a#\" is not a valid ident")] |
| 96 | fn ident_new_invalid() { |
| 97 | new("a#"); |
| 98 | } |