]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_lexer/src/unescape/tests.rs
1f4dbb20f4e9896e292e44e3208146c8ab23830e
[rust.git] / compiler / rustc_lexer / src / unescape / tests.rs
1 use super::*;
2
3 #[test]
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));
8     }
9
10     check("", EscapeError::ZeroChars);
11     check(r"\", EscapeError::LoneSlash);
12
13     check("\n", EscapeError::EscapeOnlyChar);
14     check("\t", EscapeError::EscapeOnlyChar);
15     check("'", EscapeError::EscapeOnlyChar);
16     check("\r", EscapeError::BareCarriageReturn);
17
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);
29
30     check(r"\v", EscapeError::InvalidEscape);
31     check(r"\💩", EscapeError::InvalidEscape);
32     check(r"\●", EscapeError::InvalidEscape);
33     check("\\\r", EscapeError::InvalidEscape);
34
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);
46
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);
58
59     check(r"\u{DC00}", EscapeError::LoneSurrogateUnicodeEscape);
60     check(r"\u{DDDD}", EscapeError::LoneSurrogateUnicodeEscape);
61     check(r"\u{DFFF}", EscapeError::LoneSurrogateUnicodeEscape);
62
63     check(r"\u{D800}", EscapeError::LoneSurrogateUnicodeEscape);
64     check(r"\u{DAAA}", EscapeError::LoneSurrogateUnicodeEscape);
65     check(r"\u{DBFF}", EscapeError::LoneSurrogateUnicodeEscape);
66 }
67
68 #[test]
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));
73     }
74
75     check("a", 'a');
76     check("ы", 'ы');
77     check("🦀", '🦀');
78
79     check(r#"\""#, '"');
80     check(r"\n", '\n');
81     check(r"\r", '\r');
82     check(r"\t", '\t');
83     check(r"\\", '\\');
84     check(r"\'", '\'');
85     check(r"\0", '\0');
86
87     check(r"\x00", '\0');
88     check(r"\x5a", 'Z');
89     check(r"\x5A", 'Z');
90     check(r"\x7f", 127 as char);
91
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}", '😻');
99 }
100
101 #[test]
102 fn test_unescape_str_warn() {
103     fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
104         let mut unescaped = Vec::with_capacity(literal.len());
105         unescape_literal(literal, Mode::Str, &mut |range, res| unescaped.push((range, res)));
106         assert_eq!(unescaped, expected);
107     }
108
109     check(
110         "\\\n \u{a0} x",
111         &[
112             (0..5, Err(EscapeError::UnskippedWhitespaceWarning)),
113             (3..5, Ok('\u{a0}')),
114             (5..6, Ok(' ')),
115             (6..7, Ok('x')),
116         ],
117     );
118 }
119
120 #[test]
121 fn test_unescape_str_good() {
122     fn check(literal_text: &str, expected: &str) {
123         let mut buf = Ok(String::with_capacity(literal_text.len()));
124         unescape_literal(literal_text, Mode::Str, &mut |range, c| {
125             if let Ok(b) = &mut buf {
126                 match c {
127                     Ok(c) => b.push(c),
128                     Err(e) => buf = Err((range, e)),
129                 }
130             }
131         });
132         let buf = buf.as_ref().map(|it| it.as_ref());
133         assert_eq!(buf, Ok(expected))
134     }
135
136     check("foo", "foo");
137     check("", "");
138     check(" \t\n", " \t\n");
139
140     check("hello \\\n     world", "hello world");
141     check("thread's", "thread's")
142 }
143
144 #[test]
145 fn test_unescape_byte_bad() {
146     fn check(literal_text: &str, expected_error: EscapeError) {
147         let actual_result = unescape_byte(literal_text).map_err(|(_offset, err)| err);
148         assert_eq!(actual_result, Err(expected_error));
149     }
150
151     check("", EscapeError::ZeroChars);
152     check(r"\", EscapeError::LoneSlash);
153
154     check("\n", EscapeError::EscapeOnlyChar);
155     check("\t", EscapeError::EscapeOnlyChar);
156     check("'", EscapeError::EscapeOnlyChar);
157     check("\r", EscapeError::BareCarriageReturn);
158
159     check("spam", EscapeError::MoreThanOneChar);
160     check(r"\x0ff", EscapeError::MoreThanOneChar);
161     check(r#"\"a"#, EscapeError::MoreThanOneChar);
162     check(r"\na", EscapeError::MoreThanOneChar);
163     check(r"\ra", EscapeError::MoreThanOneChar);
164     check(r"\ta", EscapeError::MoreThanOneChar);
165     check(r"\\a", EscapeError::MoreThanOneChar);
166     check(r"\'a", EscapeError::MoreThanOneChar);
167     check(r"\0a", EscapeError::MoreThanOneChar);
168
169     check(r"\v", EscapeError::InvalidEscape);
170     check(r"\💩", EscapeError::InvalidEscape);
171     check(r"\●", EscapeError::InvalidEscape);
172
173     check(r"\x", EscapeError::TooShortHexEscape);
174     check(r"\x0", EscapeError::TooShortHexEscape);
175     check(r"\xa", EscapeError::TooShortHexEscape);
176     check(r"\xf", EscapeError::TooShortHexEscape);
177     check(r"\xx", EscapeError::InvalidCharInHexEscape);
178     check(r"\xы", EscapeError::InvalidCharInHexEscape);
179     check(r"\x🦀", EscapeError::InvalidCharInHexEscape);
180     check(r"\xtt", EscapeError::InvalidCharInHexEscape);
181
182     check(r"\u", EscapeError::NoBraceInUnicodeEscape);
183     check(r"\u[0123]", EscapeError::NoBraceInUnicodeEscape);
184     check(r"\u{0x}", EscapeError::InvalidCharInUnicodeEscape);
185     check(r"\u{", EscapeError::UnclosedUnicodeEscape);
186     check(r"\u{0000", EscapeError::UnclosedUnicodeEscape);
187     check(r"\u{}", EscapeError::EmptyUnicodeEscape);
188     check(r"\u{_0000}", EscapeError::LeadingUnderscoreUnicodeEscape);
189     check(r"\u{0000000}", EscapeError::OverlongUnicodeEscape);
190
191     check("ы", EscapeError::NonAsciiCharInByte);
192     check("🦀", EscapeError::NonAsciiCharInByte);
193
194     check(r"\u{0}", EscapeError::UnicodeEscapeInByte);
195     check(r"\u{000000}", EscapeError::UnicodeEscapeInByte);
196     check(r"\u{41}", EscapeError::UnicodeEscapeInByte);
197     check(r"\u{0041}", EscapeError::UnicodeEscapeInByte);
198     check(r"\u{00_41}", EscapeError::UnicodeEscapeInByte);
199     check(r"\u{4__1__}", EscapeError::UnicodeEscapeInByte);
200     check(r"\u{1F63b}", EscapeError::UnicodeEscapeInByte);
201     check(r"\u{0}x", EscapeError::UnicodeEscapeInByte);
202     check(r"\u{1F63b}}", EscapeError::UnicodeEscapeInByte);
203     check(r"\u{FFFFFF}", EscapeError::UnicodeEscapeInByte);
204     check(r"\u{ffffff}", EscapeError::UnicodeEscapeInByte);
205     check(r"\u{ffffff}", EscapeError::UnicodeEscapeInByte);
206     check(r"\u{DC00}", EscapeError::UnicodeEscapeInByte);
207     check(r"\u{DDDD}", EscapeError::UnicodeEscapeInByte);
208     check(r"\u{DFFF}", EscapeError::UnicodeEscapeInByte);
209     check(r"\u{D800}", EscapeError::UnicodeEscapeInByte);
210     check(r"\u{DAAA}", EscapeError::UnicodeEscapeInByte);
211     check(r"\u{DBFF}", EscapeError::UnicodeEscapeInByte);
212 }
213
214 #[test]
215 fn test_unescape_byte_good() {
216     fn check(literal_text: &str, expected_byte: u8) {
217         let actual_result = unescape_byte(literal_text);
218         assert_eq!(actual_result, Ok(expected_byte));
219     }
220
221     check("a", b'a');
222
223     check(r#"\""#, b'"');
224     check(r"\n", b'\n');
225     check(r"\r", b'\r');
226     check(r"\t", b'\t');
227     check(r"\\", b'\\');
228     check(r"\'", b'\'');
229     check(r"\0", b'\0');
230
231     check(r"\x00", b'\0');
232     check(r"\x5a", b'Z');
233     check(r"\x5A", b'Z');
234     check(r"\x7f", 127);
235     check(r"\x80", 128);
236     check(r"\xff", 255);
237     check(r"\xFF", 255);
238 }
239
240 #[test]
241 fn test_unescape_byte_str_good() {
242     fn check(literal_text: &str, expected: &[u8]) {
243         let mut buf = Ok(Vec::with_capacity(literal_text.len()));
244         unescape_byte_literal(literal_text, Mode::ByteStr, &mut |range, c| {
245             if let Ok(b) = &mut buf {
246                 match c {
247                     Ok(c) => b.push(c),
248                     Err(e) => buf = Err((range, e)),
249                 }
250             }
251         });
252         let buf = buf.as_ref().map(|it| it.as_ref());
253         assert_eq!(buf, Ok(expected))
254     }
255
256     check("foo", b"foo");
257     check("", b"");
258     check(" \t\n", b" \t\n");
259
260     check("hello \\\n     world", b"hello world");
261     check("thread's", b"thread's")
262 }
263
264 #[test]
265 fn test_unescape_raw_str() {
266     fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
267         let mut unescaped = Vec::with_capacity(literal.len());
268         unescape_literal(literal, Mode::RawStr, &mut |range, res| unescaped.push((range, res)));
269         assert_eq!(unescaped, expected);
270     }
271
272     check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
273     check("\rx", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString)), (1..2, Ok('x'))]);
274 }
275
276 #[test]
277 fn test_unescape_raw_byte_str() {
278     fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
279         let mut unescaped = Vec::with_capacity(literal.len());
280         unescape_byte_literal(literal, Mode::RawByteStr, &mut |range, res| {
281             unescaped.push((range, res))
282         });
283         assert_eq!(unescaped, expected);
284     }
285
286     check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
287     check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByteString))]);
288     check(
289         "🦀a",
290         &[(0..4, Err(EscapeError::NonAsciiCharInByteString)), (4..5, Ok(byte_from_char('a')))],
291     );
292 }