4 fn test_unescape_char_bad() {
5 fn check(literal_text
: &str, expected_error
: EscapeError
) {
6 let actual_result
= unescape_char(literal_text
).map_err(|(_offset
, err
)| err
);
7 assert_eq
!(actual_result
, Err(expected_error
));
10 check("", EscapeError
::ZeroChars
);
11 check(r
"\", EscapeError
::LoneSlash
);
13 check("\n", EscapeError
::EscapeOnlyChar
);
14 check("\t", EscapeError
::EscapeOnlyChar
);
15 check("'", EscapeError
::EscapeOnlyChar
);
16 check("\r", EscapeError
::BareCarriageReturn
);
18 check("spam", EscapeError
::MoreThanOneChar
);
19 check(r
"\x0ff", EscapeError
::MoreThanOneChar
);
20 check(r
#"\"a"#, EscapeError::MoreThanOneChar);
21 check(r
"\na", EscapeError
::MoreThanOneChar
);
22 check(r
"\ra", EscapeError
::MoreThanOneChar
);
23 check(r
"\ta", EscapeError
::MoreThanOneChar
);
24 check(r
"\\a", EscapeError
::MoreThanOneChar
);
25 check(r
"\'a", EscapeError
::MoreThanOneChar
);
26 check(r
"\0a", EscapeError
::MoreThanOneChar
);
27 check(r
"\u{0}x", EscapeError
::MoreThanOneChar
);
28 check(r
"\u{1F63b}}", EscapeError
::MoreThanOneChar
);
30 check(r
"\v", EscapeError
::InvalidEscape
);
31 check(r
"\💩", EscapeError
::InvalidEscape
);
32 check(r
"\●", EscapeError
::InvalidEscape
);
33 check("\\\r", EscapeError
::InvalidEscape
);
35 check(r
"\x", EscapeError
::TooShortHexEscape
);
36 check(r
"\x0", EscapeError
::TooShortHexEscape
);
37 check(r
"\xf", EscapeError
::TooShortHexEscape
);
38 check(r
"\xa", EscapeError
::TooShortHexEscape
);
39 check(r
"\xx", EscapeError
::InvalidCharInHexEscape
);
40 check(r
"\xы", EscapeError
::InvalidCharInHexEscape
);
41 check(r
"\x🦀", EscapeError
::InvalidCharInHexEscape
);
42 check(r
"\xtt", EscapeError
::InvalidCharInHexEscape
);
43 check(r
"\xff", EscapeError
::OutOfRangeHexEscape
);
44 check(r
"\xFF", EscapeError
::OutOfRangeHexEscape
);
45 check(r
"\x80", EscapeError
::OutOfRangeHexEscape
);
47 check(r
"\u", EscapeError
::NoBraceInUnicodeEscape
);
48 check(r
"\u[0123]", EscapeError
::NoBraceInUnicodeEscape
);
49 check(r
"\u{0x}", EscapeError
::InvalidCharInUnicodeEscape
);
50 check(r
"\u{", EscapeError
::UnclosedUnicodeEscape
);
51 check(r
"\u{0000", EscapeError
::UnclosedUnicodeEscape
);
52 check(r
"\u{}", EscapeError
::EmptyUnicodeEscape
);
53 check(r
"\u{_0000}", EscapeError
::LeadingUnderscoreUnicodeEscape
);
54 check(r
"\u{0000000}", EscapeError
::OverlongUnicodeEscape
);
55 check(r
"\u{FFFFFF}", EscapeError
::OutOfRangeUnicodeEscape
);
56 check(r
"\u{ffffff}", EscapeError
::OutOfRangeUnicodeEscape
);
57 check(r
"\u{ffffff}", EscapeError
::OutOfRangeUnicodeEscape
);
59 check(r
"\u{DC00}", EscapeError
::LoneSurrogateUnicodeEscape
);
60 check(r
"\u{DDDD}", EscapeError
::LoneSurrogateUnicodeEscape
);
61 check(r
"\u{DFFF}", EscapeError
::LoneSurrogateUnicodeEscape
);
63 check(r
"\u{D800}", EscapeError
::LoneSurrogateUnicodeEscape
);
64 check(r
"\u{DAAA}", EscapeError
::LoneSurrogateUnicodeEscape
);
65 check(r
"\u{DBFF}", EscapeError
::LoneSurrogateUnicodeEscape
);
69 fn test_unescape_char_good() {
70 fn check(literal_text
: &str, expected_char
: char) {
71 let actual_result
= unescape_char(literal_text
);
72 assert_eq
!(actual_result
, Ok(expected_char
));
90 check(r"\x7f", 127 as char);
92 check(r"\u{0}", '\0');
93 check(r"\u{000000}", '\0');
94 check(r"\u{41}", 'A');
95 check(r"\u{0041}", 'A');
96 check(r"\u{00_41}", 'A');
97 check(r"\u{4__1__}", 'A');
98 check(r"\u{1F63b}", '😻');
102 fn test_unescape_str_good() {
103 fn check(literal_text: &str, expected: &str) {
104 let mut buf = Ok(String::with_capacity(literal_text.len()));
105 unescape_literal(literal_text, Mode::Str, &mut |range, c| {
106 if let Ok(b) = &mut buf {
109 Err(e) => buf = Err((range, e)),
113 let buf = buf.as_ref().map(|it| it.as_ref());
114 assert_eq!(buf, Ok(expected))
119 check(" \t\n", " \t\n");
121 check("hello \\\n world", "hello world");
122 check("thread's", "thread's")
126 fn test_unescape_byte_bad() {
127 fn check(literal_text: &str, expected_error: EscapeError) {
128 let actual_result = unescape_byte(literal_text).map_err(|(_offset, err)| err);
129 assert_eq!(actual_result, Err(expected_error));
132 check("", EscapeError::ZeroChars);
133 check(r"\", EscapeError::LoneSlash);
135 check("\n", EscapeError
::EscapeOnlyChar
);
136 check("\t", EscapeError
::EscapeOnlyChar
);
137 check("'", EscapeError
::EscapeOnlyChar
);
138 check("\r", EscapeError
::BareCarriageReturn
);
140 check("spam", EscapeError
::MoreThanOneChar
);
141 check(r
"\x0ff", EscapeError
::MoreThanOneChar
);
142 check(r
#"\"a"#, EscapeError::MoreThanOneChar);
143 check(r
"\na", EscapeError
::MoreThanOneChar
);
144 check(r
"\ra", EscapeError
::MoreThanOneChar
);
145 check(r
"\ta", EscapeError
::MoreThanOneChar
);
146 check(r
"\\a", EscapeError
::MoreThanOneChar
);
147 check(r
"\'a", EscapeError
::MoreThanOneChar
);
148 check(r
"\0a", EscapeError
::MoreThanOneChar
);
150 check(r
"\v", EscapeError
::InvalidEscape
);
151 check(r
"\💩", EscapeError
::InvalidEscape
);
152 check(r
"\●", EscapeError
::InvalidEscape
);
154 check(r
"\x", EscapeError
::TooShortHexEscape
);
155 check(r
"\x0", EscapeError
::TooShortHexEscape
);
156 check(r
"\xa", EscapeError
::TooShortHexEscape
);
157 check(r
"\xf", EscapeError
::TooShortHexEscape
);
158 check(r
"\xx", EscapeError
::InvalidCharInHexEscape
);
159 check(r
"\xы", EscapeError
::InvalidCharInHexEscape
);
160 check(r
"\x🦀", EscapeError
::InvalidCharInHexEscape
);
161 check(r
"\xtt", EscapeError
::InvalidCharInHexEscape
);
163 check(r
"\u", EscapeError
::NoBraceInUnicodeEscape
);
164 check(r
"\u[0123]", EscapeError
::NoBraceInUnicodeEscape
);
165 check(r
"\u{0x}", EscapeError
::InvalidCharInUnicodeEscape
);
166 check(r
"\u{", EscapeError
::UnclosedUnicodeEscape
);
167 check(r
"\u{0000", EscapeError
::UnclosedUnicodeEscape
);
168 check(r
"\u{}", EscapeError
::EmptyUnicodeEscape
);
169 check(r
"\u{_0000}", EscapeError
::LeadingUnderscoreUnicodeEscape
);
170 check(r
"\u{0000000}", EscapeError
::OverlongUnicodeEscape
);
172 check("ы", EscapeError
::NonAsciiCharInByte
);
173 check("🦀", EscapeError
::NonAsciiCharInByte
);
175 check(r
"\u{0}", EscapeError
::UnicodeEscapeInByte
);
176 check(r
"\u{000000}", EscapeError
::UnicodeEscapeInByte
);
177 check(r
"\u{41}", EscapeError
::UnicodeEscapeInByte
);
178 check(r
"\u{0041}", EscapeError
::UnicodeEscapeInByte
);
179 check(r
"\u{00_41}", EscapeError
::UnicodeEscapeInByte
);
180 check(r
"\u{4__1__}", EscapeError
::UnicodeEscapeInByte
);
181 check(r
"\u{1F63b}", EscapeError
::UnicodeEscapeInByte
);
182 check(r
"\u{0}x", EscapeError
::UnicodeEscapeInByte
);
183 check(r
"\u{1F63b}}", EscapeError
::UnicodeEscapeInByte
);
184 check(r
"\u{FFFFFF}", EscapeError
::UnicodeEscapeInByte
);
185 check(r
"\u{ffffff}", EscapeError
::UnicodeEscapeInByte
);
186 check(r
"\u{ffffff}", EscapeError
::UnicodeEscapeInByte
);
187 check(r
"\u{DC00}", EscapeError
::UnicodeEscapeInByte
);
188 check(r
"\u{DDDD}", EscapeError
::UnicodeEscapeInByte
);
189 check(r
"\u{DFFF}", EscapeError
::UnicodeEscapeInByte
);
190 check(r
"\u{D800}", EscapeError
::UnicodeEscapeInByte
);
191 check(r
"\u{DAAA}", EscapeError
::UnicodeEscapeInByte
);
192 check(r
"\u{DBFF}", EscapeError
::UnicodeEscapeInByte
);
196 fn test_unescape_byte_good() {
197 fn check(literal_text
: &str, expected_byte
: u8) {
198 let actual_result
= unescape_byte(literal_text
);
199 assert_eq
!(actual_result
, Ok(expected_byte
));
204 check(r
#"\""#, b'"');
212 check(r"\x00", b'\0');
213 check(r"\x5a", b'Z');
214 check(r"\x5A", b'Z');
222 fn test_unescape_byte_str_good() {
223 fn check(literal_text: &str, expected: &[u8]) {
224 let mut buf = Ok(Vec::with_capacity(literal_text.len()));
225 unescape_byte_literal(literal_text, Mode::ByteStr, &mut |range, c| {
226 if let Ok(b) = &mut buf {
229 Err(e) => buf = Err((range, e)),
233 let buf = buf.as_ref().map(|it| it.as_ref());
234 assert_eq!(buf, Ok(expected))
237 check("foo", b"foo");
239 check(" \t\n", b" \t\n");
241 check("hello \\\n world", b"hello world");
242 check("thread's", b"thread's")
246 fn test_unescape_raw_str() {
247 fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
248 let mut unescaped = Vec::with_capacity(literal.len());
249 unescape_literal(literal, Mode::RawStr, &mut |range, res| unescaped.push((range, res)));
250 assert_eq!(unescaped, expected);
253 check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
254 check("\rx", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString)), (1..2, Ok('x'))]);
258 fn test_unescape_raw_byte_str() {
259 fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
260 let mut unescaped = Vec::with_capacity(literal.len());
261 unescape_byte_literal(literal, Mode::RawByteStr, &mut |range, res| {
262 unescaped.push((range, res))
264 assert_eq!(unescaped, expected);
267 check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
268 check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByteString))]);
271 &[(0..4, Err(EscapeError::NonAsciiCharInByteString)), (4..5, Ok(byte_from_char('a')))],