Skip to content

Commit 7e9ecfa

Browse files
authored
Rollup merge of #61996 - Xanewok:unescape-raw-strings, r=matklad
Add unit tests for unescaping raw (byte) strings Adds unit tests for functionality introduced in #60793. r? @matklad @petrochenkov
2 parents fdbc4ce + 047421e commit 7e9ecfa

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/libsyntax/parse/unescape.rs

+30
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,34 @@ mod tests {
569569
check("hello \\\r\n world", b"hello world");
570570
check("thread's", b"thread's")
571571
}
572+
573+
#[test]
574+
fn test_unescape_raw_str() {
575+
fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
576+
let mut unescaped = Vec::with_capacity(literal.len());
577+
unescape_raw_str(literal, &mut |range, res| unescaped.push((range, res)));
578+
assert_eq!(unescaped, expected);
579+
}
580+
581+
check("\r\n", &[(0..2, Ok('\n'))]);
582+
check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
583+
check("\rx", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString)), (1..2, Ok('x'))]);
584+
}
585+
586+
#[test]
587+
fn test_unescape_raw_byte_str() {
588+
fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
589+
let mut unescaped = Vec::with_capacity(literal.len());
590+
unescape_raw_byte_str(literal, &mut |range, res| unescaped.push((range, res)));
591+
assert_eq!(unescaped, expected);
592+
}
593+
594+
check("\r\n", &[(0..2, Ok(byte_from_char('\n')))]);
595+
check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
596+
check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByteString))]);
597+
check(
598+
"🦀a",
599+
&[(0..4, Err(EscapeError::NonAsciiCharInByteString)), (4..5, Ok(byte_from_char('a')))],
600+
);
601+
}
572602
}

0 commit comments

Comments
 (0)