|
| 1 | +// ignore-tidy-linelength |
| 2 | + |
| 3 | +use proc_macro::Literal; |
| 4 | + |
| 5 | +pub fn test() { |
| 6 | + test_display_literal(); |
| 7 | + test_parse_literal(); |
| 8 | +} |
| 9 | + |
| 10 | +fn test_display_literal() { |
| 11 | + assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "-10"); |
| 12 | + assert_eq!(Literal::isize_suffixed(-10).to_string(), "-10isize"); |
| 13 | + assert_eq!(Literal::f32_unsuffixed(-10.0).to_string(), "-10.0"); |
| 14 | + assert_eq!(Literal::f32_suffixed(-10.0).to_string(), "-10f32"); |
| 15 | + assert_eq!(Literal::f64_unsuffixed(-10.0).to_string(), "-10.0"); |
| 16 | + assert_eq!(Literal::f64_suffixed(-10.0).to_string(), "-10f64"); |
| 17 | + assert_eq!( |
| 18 | + Literal::f64_unsuffixed(1e100).to_string(), |
| 19 | + "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0", |
| 20 | + ); |
| 21 | + |
| 22 | + assert_eq!(Literal::string("aA").to_string(), r#" "aA" "#.trim()); |
| 23 | + assert_eq!(Literal::string("\t").to_string(), r#" "\t" "#.trim()); |
| 24 | + assert_eq!(Literal::string("❤").to_string(), r#" "❤" "#.trim()); |
| 25 | + assert_eq!(Literal::string("'").to_string(), r#" "'" "#.trim()); |
| 26 | + assert_eq!(Literal::string("\"").to_string(), r#" "\"" "#.trim()); |
| 27 | + assert_eq!(Literal::string("\0").to_string(), r#" "\0" "#.trim()); |
| 28 | + assert_eq!(Literal::string("\u{1}").to_string(), r#" "\u{1}" "#.trim()); |
| 29 | + |
| 30 | + assert_eq!(Literal::byte_string(b"aA").to_string(), r#" b"aA" "#.trim()); |
| 31 | + assert_eq!(Literal::byte_string(b"\t").to_string(), r#" b"\t" "#.trim()); |
| 32 | + assert_eq!(Literal::byte_string(b"'").to_string(), r#" b"'" "#.trim()); |
| 33 | + assert_eq!(Literal::byte_string(b"\"").to_string(), r#" b"\"" "#.trim()); |
| 34 | + assert_eq!(Literal::byte_string(b"\0").to_string(), r#" b"\0" "#.trim()); |
| 35 | + assert_eq!(Literal::byte_string(b"\x01").to_string(), r#" b"\x01" "#.trim()); |
| 36 | + |
| 37 | + assert_eq!(Literal::c_string(c"aA").to_string(), r#" c"aA" "#.trim()); |
| 38 | + assert_eq!(Literal::c_string(c"\t").to_string(), r#" c"\t" "#.trim()); |
| 39 | + assert_eq!(Literal::c_string(c"❤").to_string(), r#" c"❤" "#.trim()); |
| 40 | + assert_eq!(Literal::c_string(c"\'").to_string(), r#" c"'" "#.trim()); |
| 41 | + assert_eq!(Literal::c_string(c"\"").to_string(), r#" c"\"" "#.trim()); |
| 42 | + assert_eq!(Literal::c_string(c"\x7f\xff\xfe\u{333}").to_string(), r#" c"\u{7f}\xff\xfe\u{333}" "#.trim()); |
| 43 | + |
| 44 | + assert_eq!(Literal::character('a').to_string(), r#" 'a' "#.trim()); |
| 45 | + assert_eq!(Literal::character('\t').to_string(), r#" '\t' "#.trim()); |
| 46 | + assert_eq!(Literal::character('❤').to_string(), r#" '❤' "#.trim()); |
| 47 | + assert_eq!(Literal::character('\'').to_string(), r#" '\'' "#.trim()); |
| 48 | + assert_eq!(Literal::character('"').to_string(), r#" '"' "#.trim()); |
| 49 | + assert_eq!(Literal::character('\0').to_string(), r#" '\0' "#.trim()); |
| 50 | + assert_eq!(Literal::character('\u{1}').to_string(), r#" '\u{1}' "#.trim()); |
| 51 | + |
| 52 | + assert_eq!(Literal::byte_character(b'a').to_string(), r#" b'a' "#.trim()); |
| 53 | + assert_eq!(Literal::byte_character(b'\t').to_string(), r#" b'\t' "#.trim()); |
| 54 | + assert_eq!(Literal::byte_character(b'\'').to_string(), r#" b'\'' "#.trim()); |
| 55 | + assert_eq!(Literal::byte_character(b'"').to_string(), r#" b'"' "#.trim()); |
| 56 | + assert_eq!(Literal::byte_character(0).to_string(), r#" b'\0' "#.trim()); |
| 57 | + assert_eq!(Literal::byte_character(1).to_string(), r#" b'\x01' "#.trim()); |
| 58 | +} |
| 59 | + |
| 60 | +fn test_parse_literal() { |
| 61 | + assert_eq!("1".parse::<Literal>().unwrap().to_string(), "1"); |
| 62 | + assert_eq!("1.0".parse::<Literal>().unwrap().to_string(), "1.0"); |
| 63 | + assert_eq!("'a'".parse::<Literal>().unwrap().to_string(), "'a'"); |
| 64 | + assert_eq!("b'a'".parse::<Literal>().unwrap().to_string(), "b'a'"); |
| 65 | + assert_eq!("\"\n\"".parse::<Literal>().unwrap().to_string(), "\"\n\""); |
| 66 | + assert_eq!("b\"\"".parse::<Literal>().unwrap().to_string(), "b\"\""); |
| 67 | + assert_eq!("c\"\"".parse::<Literal>().unwrap().to_string(), "c\"\""); |
| 68 | + assert_eq!("r##\"\"##".parse::<Literal>().unwrap().to_string(), "r##\"\"##"); |
| 69 | + assert_eq!("10ulong".parse::<Literal>().unwrap().to_string(), "10ulong"); |
| 70 | + assert_eq!("-10ulong".parse::<Literal>().unwrap().to_string(), "-10ulong"); |
| 71 | + |
| 72 | + assert!("true".parse::<Literal>().is_err()); |
| 73 | + assert!(".8".parse::<Literal>().is_err()); |
| 74 | + assert!("0 1".parse::<Literal>().is_err()); |
| 75 | + assert!("'a".parse::<Literal>().is_err()); |
| 76 | + assert!(" 0".parse::<Literal>().is_err()); |
| 77 | + assert!("0 ".parse::<Literal>().is_err()); |
| 78 | + assert!("/* comment */0".parse::<Literal>().is_err()); |
| 79 | + assert!("0/* comment */".parse::<Literal>().is_err()); |
| 80 | + assert!("0// comment".parse::<Literal>().is_err()); |
| 81 | + assert!("- 10".parse::<Literal>().is_err()); |
| 82 | + assert!("-'x'".parse::<Literal>().is_err()); |
| 83 | +} |
0 commit comments