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