blob: 1a65c391c4b7b0a72893fdafd51c8b610beaab49 [file] [log] [blame]
David Tolnay55535012018-01-05 16:39:23 -08001// 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
David Tolnay360efd22018-01-04 23:35:26 -08009extern crate proc_macro2;
David Tolnay61037c62018-01-05 16:21:03 -080010extern crate quote;
11extern crate syn;
David Tolnay360efd22018-01-04 23:35:26 -080012
David Tolnay61037c62018-01-05 16:21:03 -080013use syn::{FloatSuffix, IntSuffix, Lit};
David Tolnay360efd22018-01-04 23:35:26 -080014use quote::ToTokens;
David Tolnay61037c62018-01-05 16:21:03 -080015use proc_macro2::{Span, TokenNode, TokenStream};
David Tolnay360efd22018-01-04 23:35:26 -080016use std::str::FromStr;
17
18fn lit(s: &str) -> Lit {
David Tolnay61037c62018-01-05 16:21:03 -080019 match TokenStream::from_str(s)
20 .unwrap()
21 .into_iter()
22 .next()
23 .unwrap()
24 .kind
25 {
David Tolnay360efd22018-01-04 23:35:26 -080026 TokenNode::Literal(lit) => Lit::new(lit, Span::default()),
27 _ => panic!(),
28 }
29}
30
31#[test]
32fn strings() {
33 fn test_string(s: &str, value: &str) {
34 match lit(s) {
35 Lit::Str(lit) => {
36 assert_eq!(lit.value(), value);
37 let again = lit.into_tokens().to_string();
38 if again != s {
39 test_string(&again, value);
40 }
41 }
42 wrong => panic!("{:?}", wrong),
43 }
44 }
45
46 test_string("\"a\"", "a");
47 test_string("\"\\n\"", "\n");
48 test_string("\"\\r\"", "\r");
49 test_string("\"\\t\"", "\t");
50 test_string("\"🐕\"", "🐕"); // NOTE: This is an emoji
51 test_string("\"\\\"\"", "\"");
52 test_string("\"'\"", "'");
53 test_string("\"\"", "");
54 test_string("\"\\u{1F415}\"", "\u{1F415}");
David Tolnay61037c62018-01-05 16:21:03 -080055 test_string(
56 "\"contains\nnewlines\\\nescaped newlines\"",
57 "contains\nnewlinesescaped newlines",
58 );
David Tolnay360efd22018-01-04 23:35:26 -080059 test_string("r\"raw\nstring\\\nhere\"", "raw\nstring\\\nhere");
60}
61
62#[test]
63fn byte_strings() {
64 fn test_byte_string(s: &str, value: &[u8]) {
65 match lit(s) {
66 Lit::ByteStr(lit) => {
67 assert_eq!(lit.value(), value);
68 let again = lit.into_tokens().to_string();
69 if again != s {
70 test_byte_string(&again, value);
71 }
72 }
73 wrong => panic!("{:?}", wrong),
74 }
75 }
76
77 test_byte_string("b\"a\"", b"a");
78 test_byte_string("b\"\\n\"", b"\n");
79 test_byte_string("b\"\\r\"", b"\r");
80 test_byte_string("b\"\\t\"", b"\t");
81 test_byte_string("b\"\\\"\"", b"\"");
82 test_byte_string("b\"'\"", b"'");
83 test_byte_string("b\"\"", b"");
David Tolnay61037c62018-01-05 16:21:03 -080084 test_byte_string(
85 "b\"contains\nnewlines\\\nescaped newlines\"",
86 b"contains\nnewlinesescaped newlines",
87 );
David Tolnay360efd22018-01-04 23:35:26 -080088 test_byte_string("br\"raw\nstring\\\nhere\"", b"raw\nstring\\\nhere");
89}
90
91#[test]
92fn bytes() {
93 fn test_byte(s: &str, value: u8) {
94 match lit(s) {
95 Lit::Byte(lit) => {
96 assert_eq!(lit.value(), value);
97 let again = lit.into_tokens().to_string();
98 assert_eq!(again, s);
99 }
100 wrong => panic!("{:?}", wrong),
101 }
102 }
103
104 test_byte("b'a'", b'a');
105 test_byte("b'\\n'", b'\n');
106 test_byte("b'\\r'", b'\r');
107 test_byte("b'\\t'", b'\t');
108 test_byte("b'\\''", b'\'');
109 test_byte("b'\"'", b'"');
110}
111
112#[test]
113fn chars() {
114 fn test_char(s: &str, value: char) {
115 match lit(s) {
116 Lit::Char(lit) => {
117 assert_eq!(lit.value(), value);
118 let again = lit.into_tokens().to_string();
119 if again != s {
120 test_char(&again, value);
121 }
122 }
123 wrong => panic!("{:?}", wrong),
124 }
125 }
126
127 test_char("'a'", 'a');
128 test_char("'\\n'", '\n');
129 test_char("'\\r'", '\r');
130 test_char("'\\t'", '\t');
131 test_char("'🐕'", '🐕'); // NOTE: This is an emoji
132 test_char("'\\''", '\'');
133 test_char("'\"'", '"');
134 test_char("'\\u{1F415}'", '\u{1F415}');
135}
136
137#[test]
138fn ints() {
139 fn test_int(s: &str, value: u64, suffix: IntSuffix) {
140 match lit(s) {
141 Lit::Int(lit) => {
142 assert_eq!(lit.value(), value);
143 assert_eq!(lit.suffix(), suffix);
144 let again = lit.into_tokens().to_string();
145 if again != s {
146 test_int(&again, value, suffix);
147 }
148 }
149 wrong => panic!("{:?}", wrong),
150 }
151 }
152
153 use syn::IntSuffix::*;
154 test_int("5", 5, None);
155 test_int("5u32", 5, U32);
156 test_int("5_0", 50, None);
157 test_int("5_____0_____", 50, None);
158 test_int("0x7f", 127, None);
159 test_int("0x7F", 127, None);
160 test_int("0b1001", 9, None);
161 test_int("0o73", 59, None);
162 test_int("0x7Fu8", 127, U8);
163 test_int("0b1001i8", 9, I8);
164 test_int("0o73u32", 59, U32);
165 test_int("0x__7___f_", 127, None);
166 test_int("0x__7___F_", 127, None);
167 test_int("0b_1_0__01", 9, None);
168 test_int("0o_7__3", 59, None);
169 test_int("0x_7F__u8", 127, U8);
170 test_int("0b__10__0_1i8", 9, I8);
171 test_int("0o__7__________________3u32", 59, U32);
172}
173
174#[test]
175fn floats() {
David Tolnay76ebcdd2018-01-05 17:07:26 -0800176 #[cfg_attr(feature = "cargo-clippy", allow(float_cmp))]
David Tolnay360efd22018-01-04 23:35:26 -0800177 fn test_float(s: &str, value: f64, suffix: FloatSuffix) {
178 match lit(s) {
179 Lit::Float(lit) => {
180 assert_eq!(lit.value(), value);
181 assert_eq!(lit.suffix(), suffix);
182 let again = lit.into_tokens().to_string();
183 if again != s {
184 test_float(&again, value, suffix);
185 }
186 }
187 wrong => panic!("{:?}", wrong),
188 }
189 }
190
191 use syn::FloatSuffix::*;
192 test_float("5.5", 5.5, None);
193 test_float("5.5E12", 5.5e12, None);
194 test_float("5.5e12", 5.5e12, None);
195 test_float("1.0__3e-12", 1.03e-12, None);
196 test_float("1.03e+12", 1.03e12, None);
197}