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