| 1 |
use crate::{ |
| 2 |
test_util::{assert_parse_ok_eq, assert_roundtrip}, |
| 3 |
ByteStringLit, Literal, |
| 4 |
}; |
| 5 |
|
| 6 |
// ===== Utility functions ======================================================================= |
| 7 |
|
| 8 |
macro_rules! check { |
| 9 |
($lit:literal, $has_escapes:expr, $num_hashes:expr) => { |
| 10 |
check!($lit, stringify!($lit), $has_escapes, $num_hashes, "") |
| 11 |
}; |
| 12 |
($lit:literal, $input:expr, $has_escapes:expr, $num_hashes:expr, $suffix:literal) => { |
| 13 |
let input = $input; |
| 14 |
let expected = ByteStringLit { |
| 15 |
raw: input, |
| 16 |
value: if $has_escapes { Some($lit.to_vec()) } else { None }, |
| 17 |
num_hashes: $num_hashes, |
| 18 |
start_suffix: input.len() - $suffix.len(), |
| 19 |
}; |
| 20 |
|
| 21 |
assert_parse_ok_eq( |
| 22 |
input, |
| 23 |
ByteStringLit::parse(input), |
| 24 |
expected.clone(), |
| 25 |
"ByteStringLit::parse", |
| 26 |
); |
| 27 |
assert_parse_ok_eq( |
| 28 |
input, |
| 29 |
Literal::parse(input), |
| 30 |
Literal::ByteString(expected.clone()), |
| 31 |
"Literal::parse", |
| 32 |
); |
| 33 |
let lit = ByteStringLit::parse(input).unwrap(); |
| 34 |
assert_eq!(lit.value(), $lit); |
| 35 |
assert_eq!(lit.suffix(), $suffix); |
| 36 |
assert_eq!(lit.into_value().as_ref(), $lit); |
| 37 |
assert_roundtrip(expected.into_owned(), input); |
| 38 |
}; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
// ===== Actual tests ============================================================================ |
| 43 |
|
| 44 |
#[test] |
| 45 |
fn simple() { |
| 46 |
check!(b"", false, None); |
| 47 |
check!(b"a", false, None); |
| 48 |
check!(b"peter", false, None); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn special_whitespace() { |
| 53 |
let strings = ["\n", "\t", "foo\tbar", "baz\n"]; |
| 54 |
|
| 55 |
for &s in &strings { |
| 56 |
let input = format!(r#"b"{}""#, s); |
| 57 |
let input_raw = format!(r#"br"{}""#, s); |
| 58 |
for (input, num_hashes) in vec![(input, None), (input_raw, Some(0))] { |
| 59 |
let expected = ByteStringLit { |
| 60 |
raw: &*input, |
| 61 |
value: None, |
| 62 |
num_hashes, |
| 63 |
start_suffix: input.len(), |
| 64 |
}; |
| 65 |
assert_parse_ok_eq( |
| 66 |
&input, |
| 67 |
ByteStringLit::parse(&*input), |
| 68 |
expected.clone(), |
| 69 |
"ByteStringLit::parse", |
| 70 |
); |
| 71 |
assert_parse_ok_eq( |
| 72 |
&input, |
| 73 |
Literal::parse(&*input), |
| 74 |
Literal::ByteString(expected), |
| 75 |
"Literal::parse", |
| 76 |
); |
| 77 |
assert_eq!(ByteStringLit::parse(&*input).unwrap().value(), s.as_bytes()); |
| 78 |
assert_eq!(ByteStringLit::parse(&*input).unwrap().into_value(), s.as_bytes()); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn simple_escapes() { |
| 85 |
check!(b"a\nb", true, None); |
| 86 |
check!(b"\nb", true, None); |
| 87 |
check!(b"a\n", true, None); |
| 88 |
check!(b"\n", true, None); |
| 89 |
|
| 90 |
check!(b"\x60foo \t bar\rbaz\n banana \0kiwi", true, None); |
| 91 |
check!(b"foo \\ferris", true, None); |
| 92 |
check!(b"baz \\ferris\"box", true, None); |
| 93 |
check!(b"\\foo\\ banana\" baz\"", true, None); |
| 94 |
check!(b"\"foo \\ferris \" baz\\", true, None); |
| 95 |
|
| 96 |
check!(b"\x00", true, None); |
| 97 |
check!(b" \x01", true, None); |
| 98 |
check!(b"\x0c foo", true, None); |
| 99 |
check!(b" foo\x0D ", true, None); |
| 100 |
check!(b"\\x13", true, None); |
| 101 |
check!(b"\"x30", true, None); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
fn non_ascii_escapes() { |
| 106 |
check!(b"\x80", true, None); |
| 107 |
check!(b"\x8a", true, None); |
| 108 |
check!(b"\x8C", true, None); |
| 109 |
check!(b"\x99", true, None); |
| 110 |
check!(b"\xa0", true, None); |
| 111 |
check!(b"\xAd", true, None); |
| 112 |
check!(b"\xfe", true, None); |
| 113 |
check!(b"\xFe", true, None); |
| 114 |
check!(b"\xfF", true, None); |
| 115 |
check!(b"\xFF", true, None); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn string_continue() { |
| 120 |
check!(b"foo\ |
| 121 |
bar", true, None); |
| 122 |
check!(b"foo\ |
| 123 |
bar", true, None); |
| 124 |
|
| 125 |
check!(b"foo\ |
| 126 |
|
| 127 |
banana", true, None); |
| 128 |
|
| 129 |
// Weird whitespace characters |
| 130 |
let lit = ByteStringLit::parse("b\"foo\\\n\t\n \n\tbar\"").expect("failed to parse"); |
| 131 |
assert_eq!(lit.value(), b"foobar"); |
| 132 |
|
| 133 |
// Raw strings do not handle "string continues" |
| 134 |
check!(br"foo\ |
| 135 |
bar", false, Some(0)); |
| 136 |
} |
| 137 |
|
| 138 |
#[test] |
| 139 |
fn raw_byte_string() { |
| 140 |
check!(br"", false, Some(0)); |
| 141 |
check!(br"a", false, Some(0)); |
| 142 |
check!(br"peter", false, Some(0)); |
| 143 |
check!(br"Greetings jason!", false, Some(0)); |
| 144 |
|
| 145 |
check!(br#""#, false, Some(1)); |
| 146 |
check!(br#"a"#, false, Some(1)); |
| 147 |
check!(br##"peter"##, false, Some(2)); |
| 148 |
check!(br###"Greetings # Jason!"###, false, Some(3)); |
| 149 |
check!(br########"we ## need #### more ####### hashtags"########, false, Some(8)); |
| 150 |
|
| 151 |
check!(br#"foo " bar"#, false, Some(1)); |
| 152 |
check!(br##"foo " bar"##, false, Some(2)); |
| 153 |
check!(br#"foo """" '"'" bar"#, false, Some(1)); |
| 154 |
check!(br#""foo""#, false, Some(1)); |
| 155 |
check!(br###""foo'"###, false, Some(3)); |
| 156 |
check!(br#""x'#_#s'"#, false, Some(1)); |
| 157 |
check!(br"#", false, Some(0)); |
| 158 |
check!(br"foo#", false, Some(0)); |
| 159 |
check!(br"##bar", false, Some(0)); |
| 160 |
check!(br###""##foo"##bar'"###, false, Some(3)); |
| 161 |
|
| 162 |
check!(br"foo\n\t\r\0\\x60\u{123}doggo", false, Some(0)); |
| 163 |
check!(br#"cat\n\t\r\0\\x60\u{123}doggo"#, false, Some(1)); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn suffixes() { |
| 168 |
check!(b"hello", r###"b"hello"suffix"###, false, None, "suffix"); |
| 169 |
check!(b"fox", r#"b"fox"peter"#, false, None, "peter"); |
| 170 |
check!(b"a\x0cb\\", r#"b"a\x0cb\\"_jürgen"#, true, None, "_jürgen"); |
| 171 |
check!(br"a\x0cb\\", r###"br#"a\x0cb\\"#_jürgen"###, false, Some(1), "_jürgen"); |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn parse_err() { |
| 176 |
assert_err!(ByteStringLit, r#"b""#, UnterminatedString, None); |
| 177 |
assert_err!(ByteStringLit, r#"b"cat"#, UnterminatedString, None); |
| 178 |
assert_err!(ByteStringLit, r#"b"Jurgen"#, UnterminatedString, None); |
| 179 |
assert_err!(ByteStringLit, r#"b"foo bar baz"#, UnterminatedString, None); |
| 180 |
|
| 181 |
assert_err!(ByteStringLit, r#"b"fox"peter""#, InvalidSuffix, 6); |
| 182 |
assert_err!(ByteStringLit, r###"br#"foo "# bar"#"###, UnexpectedChar, 10); |
| 183 |
|
| 184 |
assert_err!(ByteStringLit, "b\"\r\"", CarriageReturn, 2); |
| 185 |
assert_err!(ByteStringLit, "b\"fo\rx\"", CarriageReturn, 4); |
| 186 |
assert_err!(ByteStringLit, "br\"\r\"", CarriageReturn, 3); |
| 187 |
assert_err!(ByteStringLit, "br\"fo\rx\"", CarriageReturn, 5); |
| 188 |
assert_err!(ByteStringLit, "b\"a\\\r\"", UnknownEscape, 3..5); |
| 189 |
assert_err!(ByteStringLit, "br\"a\\\r\"", CarriageReturn, 5); |
| 190 |
|
| 191 |
assert_err!(ByteStringLit, r##"br####""##, UnterminatedRawString, None); |
| 192 |
assert_err!(ByteStringLit, r#####"br##"foo"#bar"#####, UnterminatedRawString, None); |
| 193 |
assert_err!(ByteStringLit, r##"br####"##, InvalidLiteral, None); |
| 194 |
assert_err!(ByteStringLit, r##"br####x"##, InvalidLiteral, None); |
| 195 |
} |
| 196 |
|
| 197 |
#[test] |
| 198 |
fn non_ascii() { |
| 199 |
assert_err!(ByteStringLit, r#"b"న""#, NonAsciiInByteLiteral, 2); |
| 200 |
assert_err!(ByteStringLit, r#"b"foo犬""#, NonAsciiInByteLiteral, 5); |
| 201 |
assert_err!(ByteStringLit, r#"b"x🦊baz""#, NonAsciiInByteLiteral, 3); |
| 202 |
assert_err!(ByteStringLit, r#"br"న""#, NonAsciiInByteLiteral, 3); |
| 203 |
assert_err!(ByteStringLit, r#"br"foo犬""#, NonAsciiInByteLiteral, 6); |
| 204 |
assert_err!(ByteStringLit, r#"br"x🦊baz""#, NonAsciiInByteLiteral, 4); |
| 205 |
} |
| 206 |
|
| 207 |
#[test] |
| 208 |
fn invalid_escapes() { |
| 209 |
assert_err!(ByteStringLit, r#"b"\a""#, UnknownEscape, 2..4); |
| 210 |
assert_err!(ByteStringLit, r#"b"foo\y""#, UnknownEscape, 5..7); |
| 211 |
assert_err!(ByteStringLit, r#"b"\"#, UnterminatedEscape, 2); |
| 212 |
assert_err!(ByteStringLit, r#"b"\x""#, UnterminatedEscape, 2..4); |
| 213 |
assert_err!(ByteStringLit, r#"b"foo\x1""#, UnterminatedEscape, 5..8); |
| 214 |
assert_err!(ByteStringLit, r#"b" \xaj""#, InvalidXEscape, 3..7); |
| 215 |
assert_err!(ByteStringLit, r#"b"\xjbbaz""#, InvalidXEscape, 2..6); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn unicode_escape_not_allowed() { |
| 220 |
assert_err!(ByteStringLit, r#"b"\u{0}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 221 |
assert_err!(ByteStringLit, r#"b"\u{00}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 222 |
assert_err!(ByteStringLit, r#"b"\u{b}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 223 |
assert_err!(ByteStringLit, r#"b"\u{B}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 224 |
assert_err!(ByteStringLit, r#"b"\u{7e}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 225 |
assert_err!(ByteStringLit, r#"b"\u{E4}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 226 |
assert_err!(ByteStringLit, r#"b"\u{e4}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 227 |
assert_err!(ByteStringLit, r#"b"\u{fc}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 228 |
assert_err!(ByteStringLit, r#"b"\u{Fc}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 229 |
assert_err!(ByteStringLit, r#"b"\u{fC}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 230 |
assert_err!(ByteStringLit, r#"b"\u{FC}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 231 |
assert_err!(ByteStringLit, r#"b"\u{b10}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 232 |
assert_err!(ByteStringLit, r#"b"\u{B10}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 233 |
assert_err!(ByteStringLit, r#"b"\u{0b10}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 234 |
assert_err!(ByteStringLit, r#"b"\u{2764}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 235 |
assert_err!(ByteStringLit, r#"b"\u{1f602}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 236 |
assert_err!(ByteStringLit, r#"b"\u{1F602}""#, UnicodeEscapeInByteLiteral, 2..4); |
| 237 |
} |