Skip to content

Commit

Permalink
Add c-str literal test
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jun 24, 2023
1 parent 3c20a59 commit 5477bd2
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,42 @@ fn literal_byte_string() {
"br\"\u{a0}\"".parse::<TokenStream>().unwrap_err();
}

#[test]
fn literal_c_string() {
let strings = r###"
c"hello\x80我叫\u{1F980}" // from the RFC
cr"\"
cr##"Hello "world"!"##
c"\t\n\r\"\\"
"###;

let mut tokens = strings.parse::<TokenStream>().unwrap().into_iter();

for expected in &[
r#"c"hello\x80我叫\u{1F980}""#,
r#"cr"\""#,
r###"cr##"Hello "world"!"##"###,
r#"c"\t\n\r\"\\""#,
] {
match tokens.next().unwrap() {
TokenTree::Literal(literal) => {
assert_eq!(literal.to_string(), *expected);
}
unexpected => panic!("unexpected token: {:?}", unexpected),
}
}

if let Some(unexpected) = tokens.next() {
panic!("unexpected token: {:?}", unexpected);
}

for invalid in &[r#"c"\0""#, r#"c"\x00""#, r#"c"\u{0}""#, "c\"\0\""] {
if let Ok(unexpected) = invalid.parse::<TokenStream>() {
panic!("unexpected token: {:?}", unexpected);
}
}
}

#[test]
fn literal_character() {
assert_eq!(Literal::character('x').to_string(), "'x'");
Expand Down

0 comments on commit 5477bd2

Please sign in to comment.