]> git.lizzy.rs Git - rust.git/commitdiff
Add test code for unescaping byte strings
authorNoritada Kobayashi <noritada.kobayashi@gmail.com>
Mon, 7 Nov 2022 14:39:02 +0000 (23:39 +0900)
committerNoritada Kobayashi <noritada.kobayashi@gmail.com>
Mon, 7 Nov 2022 14:39:02 +0000 (23:39 +0900)
crates/syntax/src/ast/token_ext.rs

index 32dd2db405c89fa0b6d9d6fa5224d86922fc6122..8990f7a7d4e8ec4b85d5906667216f5835e8cb80 100644 (file)
@@ -456,6 +456,31 @@ fn test_string_escape() {
         );
     }
 
+    fn check_byte_string_value<'a, const N: usize>(
+        lit: &str,
+        expected: impl Into<Option<&'a [u8; N]>>,
+    ) {
+        assert_eq!(
+            ast::ByteString { syntax: make::tokens::literal(&format!("b\"{}\"", lit)) }
+                .value()
+                .as_deref(),
+            expected.into().map(|value| &value[..])
+        );
+    }
+
+    #[test]
+    fn test_byte_string_escape() {
+        check_byte_string_value(r"foobar", b"foobar");
+        check_byte_string_value(r"\foobar", None::<&[u8; 0]>);
+        check_byte_string_value(r"\nfoobar", b"\nfoobar");
+        check_byte_string_value(r"C:\\Windows\\System32\\", b"C:\\Windows\\System32\\");
+        check_byte_string_value(r"\x61bcde", b"abcde");
+        check_byte_string_value(
+            r"a\
+bcde", b"abcde",
+        );
+    }
+
     #[test]
     fn test_value_underscores() {
         check_float_value("3.141592653589793_f64", 3.141592653589793_f64);