blob: 99596a6784a6bf69df4b489835fcf4fc9d7383da [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
Alex Crichton499ed642018-05-17 11:02:02 -070012use proc_macro2::{Span, TokenStream, Ident};
David Tolnayd741aa82018-04-04 07:53:04 -070013use std::str::FromStr;
David Tolnayd741aa82018-04-04 07:53:04 -070014use syn::synom::ParseError;
15
16fn parse(s: &str) -> Result<Ident, ParseError> {
17 syn::parse2(TokenStream::from_str(s).unwrap())
18}
19
20fn new(s: &str) -> Ident {
21 Ident::new(s, Span::call_site())
22}
23
24#[test]
25fn ident_parse() {
26 parse("String").unwrap();
27}
28
29#[test]
30fn ident_parse_keyword() {
Alex Crichton499ed642018-05-17 11:02:02 -070031 parse("abstract").unwrap();
David Tolnayd741aa82018-04-04 07:53:04 -070032}
33
34#[test]
35fn ident_parse_empty() {
36 parse("").unwrap_err();
37}
38
39#[test]
40fn ident_parse_lifetime() {
41 parse("'static").unwrap_err();
42}
43
44#[test]
45fn ident_parse_underscore() {
Alex Crichton499ed642018-05-17 11:02:02 -070046 parse("_").unwrap();
David Tolnayd741aa82018-04-04 07:53:04 -070047}
48
49#[test]
50fn ident_parse_number() {
51 parse("255").unwrap_err();
52}
53
54#[test]
55fn ident_parse_invalid() {
56 parse("a#").unwrap_err();
57}
58
59#[test]
60fn ident_new() {
61 new("String");
62}
63
64#[test]
65fn ident_new_keyword() {
66 new("abstract");
67}
68
69#[test]
Alex Crichton499ed642018-05-17 11:02:02 -070070#[should_panic(expected = "use Option<Ident>")]
David Tolnayd741aa82018-04-04 07:53:04 -070071fn ident_new_empty() {
72 new("");
73}
74
75#[test]
Alex Crichton499ed642018-05-17 11:02:02 -070076#[should_panic(expected = "not a valid Ident")]
David Tolnayd741aa82018-04-04 07:53:04 -070077fn ident_new_lifetime() {
78 new("'static");
79}
80
81#[test]
David Tolnayd741aa82018-04-04 07:53:04 -070082fn ident_new_underscore() {
83 new("_");
84}
85
86#[test]
Alex Crichton499ed642018-05-17 11:02:02 -070087#[should_panic(expected = "use Literal instead")]
David Tolnayd741aa82018-04-04 07:53:04 -070088fn ident_new_number() {
89 new("255");
90}
91
92#[test]
Alex Crichton499ed642018-05-17 11:02:02 -070093#[should_panic(expected = "\"a#\" is not a valid Ident")]
David Tolnayd741aa82018-04-04 07:53:04 -070094fn ident_new_invalid() {
95 new("a#");
96}