Skip to content

Commit

Permalink
Rollup merge of #61996 - Xanewok:unescape-raw-strings, r=matklad
Browse files Browse the repository at this point in the history
Add unit tests for unescaping raw (byte) strings

Adds unit tests for functionality introduced in #60793.

r? @matklad @petrochenkov
  • Loading branch information
Centril committed Jun 20, 2019
2 parents fdbc4ce + 047421e commit 7e9ecfa
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/libsyntax/parse/unescape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,34 @@ mod tests {
check("hello \\\r\n world", b"hello world");
check("thread's", b"thread's")
}

#[test]
fn test_unescape_raw_str() {
fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
let mut unescaped = Vec::with_capacity(literal.len());
unescape_raw_str(literal, &mut |range, res| unescaped.push((range, res)));
assert_eq!(unescaped, expected);
}

check("\r\n", &[(0..2, Ok('\n'))]);
check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
check("\rx", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString)), (1..2, Ok('x'))]);
}

#[test]
fn test_unescape_raw_byte_str() {
fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
let mut unescaped = Vec::with_capacity(literal.len());
unescape_raw_byte_str(literal, &mut |range, res| unescaped.push((range, res)));
assert_eq!(unescaped, expected);
}

check("\r\n", &[(0..2, Ok(byte_from_char('\n')))]);
check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByteString))]);
check(
"🦀a",
&[(0..4, Err(EscapeError::NonAsciiCharInByteString)), (4..5, Ok(byte_from_char('a')))],
);
}
}

0 comments on commit 7e9ecfa

Please sign in to comment.