Debian Codegraph @

third_party/rust/syn/tests/test_lit.rs

raw
1 #![allow(
2 clippy::float_cmp,
3 clippy::needless_lifetimes,
4 clippy::needless_raw_string_hashes,
5 clippy::non_ascii_literal,
6 clippy::single_match_else,
7 clippy::uninlined_format_args
8 )]
9
10 #[macro_use]
11 mod macros;
12
13 use proc_macro2::{Delimiter, Group, Literal, Span, TokenStream, TokenTree};
14 use quote::ToTokens;
15 use std::ffi::CStr;
16 use std::str::FromStr;
17 use syn::{Lit, LitFloat, LitInt, LitStr};
18
19 #[track_caller]
20 fn lit(s: &str) -> Lit {
21 let mut tokens = TokenStream::from_str(s).unwrap().into_iter();
22 match tokens.next().unwrap() {
23 TokenTree::Literal(lit) => {
24 assert!(tokens.next().is_none());
25 Lit::new(lit)
26 }
27 wrong => panic!("{:?}", wrong),
28 }
29 }
30
31 #[test]
32 fn strings() {
33 #[track_caller]
34 fn test_string(s: &str, value: &str) {
35 let s = s.trim();
36 match lit(s) {
37 Lit::Str(lit) => {
38 assert_eq!(lit.value(), value);
39 let again = lit.into_token_stream().to_string();
40 if again != s {
41 test_string(&again, value);
42 }
43 }
44 wrong => panic!("{:?}", wrong),
45 }
46 }
47
48 test_string(r#" "" "#, "");
49 test_string(r#" "a" "#, "a");
50 test_string(r#" "\n" "#, "\n");
51 test_string(r#" "\r" "#, "\r");
52 test_string(r#" "\t" "#, "\t");
53 test_string(r#" "🐕" "#, "🐕"); // NOTE: This is an emoji
54 test_string(r#" "\"" "#, "\"");
55 test_string(r#" "'" "#, "'");
56 test_string(r#" "\u{1F415}" "#, "\u{1F415}");
57 test_string(r#" "\u{1_2__3_}" "#, "\u{123}");
58 test_string(
59 "\"contains\nnewlines\\\nescaped newlines\"",
60 "contains\nnewlinesescaped newlines",
61 );
62 test_string(
63 "\"escaped newline\\\n \x0C unsupported whitespace\"",
64 "escaped newline\x0C unsupported whitespace",
65 );
66 test_string("r\"raw\nstring\\\nhere\"", "raw\nstring\\\nhere");
67 test_string("\"...\"q", "...");
68 test_string("r\"...\"q", "...");
69 test_string("r##\"...\"##q", "...");
70 }
71
72 #[test]
73 fn byte_strings() {
74 #[track_caller]
75 fn test_byte_string(s: &str, value: &[u8]) {
76 let s = s.trim();
77 match lit(s) {
78 Lit::ByteStr(lit) => {
79 assert_eq!(lit.value(), value);
80 let again = lit.into_token_stream().to_string();
81 if again != s {
82 test_byte_string(&again, value);
83 }
84 }
85 wrong => panic!("{:?}", wrong),
86 }
87 }
88
89 test_byte_string(r#" b"" "#, b"");
90 test_byte_string(r#" b"a" "#, b"a");
91 test_byte_string(r#" b"\n" "#, b"\n");
92 test_byte_string(r#" b"\r" "#, b"\r");
93 test_byte_string(r#" b"\t" "#, b"\t");
94 test_byte_string(r#" b"\"" "#, b"\"");
95 test_byte_string(r#" b"'" "#, b"'");
96 test_byte_string(
97 "b\"contains\nnewlines\\\nescaped newlines\"",
98 b"contains\nnewlinesescaped newlines",
99 );
100 test_byte_string("br\"raw\nstring\\\nhere\"", b"raw\nstring\\\nhere");
101 test_byte_string("b\"...\"q", b"...");
102 test_byte_string("br\"...\"q", b"...");
103 test_byte_string("br##\"...\"##q", b"...");
104 }
105
106 #[test]
107 fn c_strings() {
108 #[track_caller]
109 fn test_c_string(s: &str, value: &CStr) {
110 let s = s.trim();
111 match lit(s) {
112 Lit::CStr(lit) => {
113 assert_eq!(*lit.value(), *value);
114 let again = lit.into_token_stream().to_string();
115 if again != s {
116 test_c_string(&again, value);
117 }
118 }
119 wrong => panic!("{:?}", wrong),
120 }
121 }
122
123 test_c_string(r#" c"" "#, c"");
124 test_c_string(r#" c"a" "#, c"a");
125 test_c_string(r#" c"\n" "#, c"\n");
126 test_c_string(r#" c"\r" "#, c"\r");
127 test_c_string(r#" c"\t" "#, c"\t");
128 test_c_string(r#" c"\\" "#, c"\\");
129 test_c_string(r#" c"\'" "#, c"'");
130 test_c_string(r#" c"\"" "#, c"\"");
131 test_c_string(
132 "c\"contains\nnewlines\\\nescaped newlines\"",
133 c"contains\nnewlinesescaped newlines",
134 );
135 test_c_string("cr\"raw\nstring\\\nhere\"", c"raw\nstring\\\nhere");
136 test_c_string("c\"...\"q", c"...");
137 test_c_string("cr\"...\"", c"...");
138 test_c_string("cr##\"...\"##", c"...");
139 test_c_string(
140 r#" c"hello\x80我叫\u{1F980}" "#, // from the RFC
141 c"hello\x80我叫\u{1F980}",
142 );
143 }
144
145 #[test]
146 fn bytes() {
147 #[track_caller]
148 fn test_byte(s: &str, value: u8) {
149 let s = s.trim();
150 match lit(s) {
151 Lit::Byte(lit) => {
152 assert_eq!(lit.value(), value);
153 let again = lit.into_token_stream().to_string();
154 assert_eq!(again, s);
155 }
156 wrong => panic!("{:?}", wrong),
157 }
158 }
159
160 test_byte(r#" b'a' "#, b'a');
161 test_byte(r#" b'\n' "#, b'\n');
162 test_byte(r#" b'\r' "#, b'\r');
163 test_byte(r#" b'\t' "#, b'\t');
164 test_byte(r#" b'\'' "#, b'\'');
165 test_byte(r#" b'"' "#, b'"');
166 test_byte(r#" b'a'q "#, b'a');
167 }
168
169 #[test]
170 fn chars() {
171 #[track_caller]
172 fn test_char(s: &str, value: char) {
173 let s = s.trim();
174 match lit(s) {
175 Lit::Char(lit) => {
176 assert_eq!(lit.value(), value);
177 let again = lit.into_token_stream().to_string();
178 if again != s {
179 test_char(&again, value);
180 }
181 }
182 wrong => panic!("{:?}", wrong),
183 }
184 }
185
186 test_char(r#" 'a' "#, 'a');
187 test_char(r#" '\n' "#, '\n');
188 test_char(r#" '\r' "#, '\r');
189 test_char(r#" '\t' "#, '\t');
190 test_char(r#" '🐕' "#, '🐕'); // NOTE: This is an emoji
191 test_char(r#" '\'' "#, '\'');
192 test_char(r#" '"' "#, '"');
193 test_char(r#" '\u{1F415}' "#, '\u{1F415}');
194 test_char(r#" 'a'q "#, 'a');
195 }
196
197 #[test]
198 fn ints() {
199 #[track_caller]
200 fn test_int(s: &str, value: u64, suffix: &str) {
201 match lit(s) {
202 Lit::Int(lit) => {
203 assert_eq!(lit.base10_digits().parse::<u64>().unwrap(), value);
204 assert_eq!(lit.suffix(), suffix);
205 let again = lit.into_token_stream().to_string();
206 if again != s {
207 test_int(&again, value, suffix);
208 }
209 }
210 wrong => panic!("{:?}", wrong),
211 }
212 }
213
214 test_int("5", 5, "");
215 test_int("5u32", 5, "u32");
216 test_int("0E", 0, "E");
217 test_int("0ECMA", 0, "ECMA");
218 test_int("0o0A", 0, "A");
219 test_int("5_0", 50, "");
220 test_int("5_____0_____", 50, "");
221 test_int("0x7f", 127, "");
222 test_int("0x7F", 127, "");
223 test_int("0b1001", 9, "");
224 test_int("0o73", 59, "");
225 test_int("0x7Fu8", 127, "u8");
226 test_int("0b1001i8", 9, "i8");
227 test_int("0o73u32", 59, "u32");
228 test_int("0x__7___f_", 127, "");
229 test_int("0x__7___F_", 127, "");
230 test_int("0b_1_0__01", 9, "");
231 test_int("0o_7__3", 59, "");
232 test_int("0x_7F__u8", 127, "u8");
233 test_int("0b__10__0_1i8", 9, "i8");
234 test_int("0o__7__________________3u32", 59, "u32");
235 test_int("0e1\u{5c5}", 0, "e1\u{5c5}");
236 }
237
238 #[test]
239 fn floats() {
240 #[track_caller]
241 fn test_float(s: &str, value: f64, suffix: &str) {
242 match lit(s) {
243 Lit::Float(lit) => {
244 assert_eq!(lit.base10_digits().parse::<f64>().unwrap(), value);
245 assert_eq!(lit.suffix(), suffix);
246 let again = lit.into_token_stream().to_string();
247 if again != s {
248 test_float(&again, value, suffix);
249 }
250 }
251 wrong => panic!("{:?}", wrong),
252 }
253 }
254
255 test_float("5.5", 5.5, "");
256 test_float("5.5E12", 5.5e12, "");
257 test_float("5.5e12", 5.5e12, "");
258 test_float("1.0__3e-12", 1.03e-12, "");
259 test_float("1.03e+12", 1.03e12, "");
260 test_float("9e99e99", 9e99, "e99");
261 test_float("1e_0", 1.0, "");
262 test_float("0.0ECMA", 0.0, "ECMA");
263 }
264
265 #[test]
266 fn negative() {
267 let span = Span::call_site();
268 assert_eq!("-1", LitInt::new("-1", span).to_string());
269 assert_eq!("-1i8", LitInt::new("-1i8", span).to_string());
270 assert_eq!("-1i16", LitInt::new("-1i16", span).to_string());
271 assert_eq!("-1i32", LitInt::new("-1i32", span).to_string());
272 assert_eq!("-1i64", LitInt::new("-1i64", span).to_string());
273 assert_eq!("-1.5", LitFloat::new("-1.5", span).to_string());
274 assert_eq!("-1.5f32", LitFloat::new("-1.5f32", span).to_string());
275 assert_eq!("-1.5f64", LitFloat::new("-1.5f64", span).to_string());
276 }
277
278 #[test]
279 fn suffix() {
280 #[track_caller]
281 fn get_suffix(token: &str) -> String {
282 let lit = syn::parse_str::<Lit>(token).unwrap();
283 match lit {
284 Lit::Str(lit) => lit.suffix().to_owned(),
285 Lit::ByteStr(lit) => lit.suffix().to_owned(),
286 Lit::CStr(lit) => lit.suffix().to_owned(),
287 Lit::Byte(lit) => lit.suffix().to_owned(),
288 Lit::Char(lit) => lit.suffix().to_owned(),
289 Lit::Int(lit) => lit.suffix().to_owned(),
290 Lit::Float(lit) => lit.suffix().to_owned(),
291 _ => unimplemented!(),
292 }
293 }
294
295 assert_eq!(get_suffix("\"\"s"), "s");
296 assert_eq!(get_suffix("r\"\"r"), "r");
297 assert_eq!(get_suffix("r#\"\"#r"), "r");
298 assert_eq!(get_suffix("b\"\"b"), "b");
299 assert_eq!(get_suffix("br\"\"br"), "br");
300 assert_eq!(get_suffix("br#\"\"#br"), "br");
301 assert_eq!(get_suffix("c\"\"c"), "c");
302 assert_eq!(get_suffix("cr\"\"cr"), "cr");
303 assert_eq!(get_suffix("cr#\"\"#cr"), "cr");
304 assert_eq!(get_suffix("'c'c"), "c");
305 assert_eq!(get_suffix("b'b'b"), "b");
306 assert_eq!(get_suffix("1i32"), "i32");
307 assert_eq!(get_suffix("1_i32"), "i32");
308 assert_eq!(get_suffix("1.0f32"), "f32");
309 assert_eq!(get_suffix("1.0_f32"), "f32");
310 }
311
312 #[test]
313 fn test_deep_group_empty() {
314 let tokens = TokenStream::from_iter([TokenTree::Group(Group::new(
315 Delimiter::None,
316 TokenStream::from_iter([TokenTree::Group(Group::new(
317 Delimiter::None,
318 TokenStream::from_iter([TokenTree::Literal(Literal::string("hi"))]),
319 ))]),
320 ))]);
321
322 snapshot!(tokens as Lit, @r#""hi""# );
323 }
324
325 #[test]
326 fn test_error() {
327 let err = syn::parse_str::<LitStr>("...").unwrap_err();
328 assert_eq!("expected string literal", err.to_string());
329
330 let err = syn::parse_str::<LitStr>("5").unwrap_err();
331 assert_eq!("expected string literal", err.to_string());
332 }