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