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