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