Debian Codegraph @

.cargo/vendor/nom/src/sequence/tests.rs

raw
1 use super::*;
2 use crate::bytes::streaming::{tag, take};
3 use crate::error::{Error, ErrorKind};
4 use crate::internal::{Err, IResult, Needed};
5 use crate::number::streaming::be_u16;
6
7 #[test]
8 fn single_element_tuples() {
9 use crate::character::complete::alpha1;
10 use crate::{error::ErrorKind, Err};
11
12 let mut parser = (alpha1,);
13 assert_eq!(
14 crate::Parser::parse(&mut parser, "abc123def"),
15 Ok(("123def", ("abc",)))
16 );
17 assert_eq!(
18 crate::Parser::parse(&mut parser, "123def"),
19 Err(Err::Error(("123def", ErrorKind::Alpha)))
20 );
21 }
22
23 /*FIXME: convert code examples to new error management
24 use util::{add_error_pattern, error_to_list, print_error};
25
26 #[cfg(feature = "std")]
27 #[rustfmt::skip]
28 fn error_to_string<P: Clone + PartialEq>(e: &Context<P, u32>) -> &'static str {
29 let v: Vec<(P, ErrorKind<u32>)> = error_to_list(e);
30 // do it this way if you can use slice patterns
31 //match &v[..] {
32 // [ErrorKind::Custom(42), ErrorKind::Tag] => "missing `ijkl` tag",
33 // [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] => "missing `mnop` tag after `ijkl`",
34 // _ => "unrecognized error"
35 //}
36
37 let collected: Vec<ErrorKind<u32>> = v.iter().map(|&(_, ref e)| e.clone()).collect();
38 if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Tag] {
39 "missing `ijkl` tag"
40 } else if &collected[..] == [ErrorKind::Custom(42), ErrorKind::Custom(128), ErrorKind::Tag] {
41 "missing `mnop` tag after `ijkl`"
42 } else {
43 "unrecognized error"
44 }
45 }
46
47 // do it this way if you can use box patterns
48 //use $crate::lib::std::str;
49 //fn error_to_string(e:Err) -> String
50 // match e {
51 // NodePosition(ErrorKind::Custom(42), i1, box Position(ErrorKind::Tag, i2)) => {
52 // format!("missing `ijkl` tag, found '{}' instead", str::from_utf8(i2).unwrap())
53 // },
54 // NodePosition(ErrorKind::Custom(42), i1, box NodePosition(ErrorKind::Custom(128), i2, box Position(ErrorKind::Tag, i3))) => {
55 // format!("missing `mnop` tag after `ijkl`, found '{}' instead", str::from_utf8(i3).unwrap())
56 // },
57 // _ => "unrecognized error".to_string()
58 // }
59 //}
60 */
61
62 #[test]
63 fn complete() {
64 use crate::bytes::complete::tag;
65 fn err_test(i: &[u8]) -> IResult<&[u8], &[u8]> {
66 let (i, _) = tag("ijkl")(i)?;
67 tag("mnop")(i)
68 }
69 let a = &b"ijklmn"[..];
70
71 let res_a = err_test(a);
72 assert_eq!(
73 res_a,
74 Err(Err::Error(error_position!(&b"mn"[..], ErrorKind::Tag)))
75 );
76 }
77
78 #[test]
79 fn pair_test() {
80 fn pair_abc_def(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
81 pair(tag("abc"), tag("def")).parse(i)
82 }
83
84 assert_eq!(
85 pair_abc_def(&b"abcdefghijkl"[..]),
86 Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])))
87 );
88 assert_eq!(
89 pair_abc_def(&b"ab"[..]),
90 Err(Err::Incomplete(Needed::new(1)))
91 );
92 assert_eq!(
93 pair_abc_def(&b"abcd"[..]),
94 Err(Err::Incomplete(Needed::new(2)))
95 );
96 assert_eq!(
97 pair_abc_def(&b"xxx"[..]),
98 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
99 );
100 assert_eq!(
101 pair_abc_def(&b"xxxdef"[..]),
102 Err(Err::Error(error_position!(&b"xxxdef"[..], ErrorKind::Tag)))
103 );
104 assert_eq!(
105 pair_abc_def(&b"abcxxx"[..]),
106 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
107 );
108 }
109
110 #[test]
111 fn separated_pair_test() {
112 fn sep_pair_abc_def(i: &[u8]) -> IResult<&[u8], (&[u8], &[u8])> {
113 separated_pair(tag("abc"), tag(","), tag("def")).parse(i)
114 }
115
116 assert_eq!(
117 sep_pair_abc_def(&b"abc,defghijkl"[..]),
118 Ok((&b"ghijkl"[..], (&b"abc"[..], &b"def"[..])))
119 );
120 assert_eq!(
121 sep_pair_abc_def(&b"ab"[..]),
122 Err(Err::Incomplete(Needed::new(1)))
123 );
124 assert_eq!(
125 sep_pair_abc_def(&b"abc,d"[..]),
126 Err(Err::Incomplete(Needed::new(2)))
127 );
128 assert_eq!(
129 sep_pair_abc_def(&b"xxx"[..]),
130 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
131 );
132 assert_eq!(
133 sep_pair_abc_def(&b"xxx,def"[..]),
134 Err(Err::Error(error_position!(&b"xxx,def"[..], ErrorKind::Tag)))
135 );
136 assert_eq!(
137 sep_pair_abc_def(&b"abc,xxx"[..]),
138 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
139 );
140 }
141
142 #[test]
143 fn preceded_test() {
144 fn preceded_abcd_efgh(i: &[u8]) -> IResult<&[u8], &[u8]> {
145 preceded(tag("abcd"), tag("efgh")).parse(i)
146 }
147
148 assert_eq!(
149 preceded_abcd_efgh(&b"abcdefghijkl"[..]),
150 Ok((&b"ijkl"[..], &b"efgh"[..]))
151 );
152 assert_eq!(
153 preceded_abcd_efgh(&b"ab"[..]),
154 Err(Err::Incomplete(Needed::new(2)))
155 );
156 assert_eq!(
157 preceded_abcd_efgh(&b"abcde"[..]),
158 Err(Err::Incomplete(Needed::new(3)))
159 );
160 assert_eq!(
161 preceded_abcd_efgh(&b"xxx"[..]),
162 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
163 );
164 assert_eq!(
165 preceded_abcd_efgh(&b"xxxxdef"[..]),
166 Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag)))
167 );
168 assert_eq!(
169 preceded_abcd_efgh(&b"abcdxxx"[..]),
170 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
171 );
172 }
173
174 #[test]
175 fn terminated_test() {
176 fn terminated_abcd_efgh(i: &[u8]) -> IResult<&[u8], &[u8]> {
177 terminated(tag("abcd"), tag("efgh")).parse(i)
178 }
179
180 assert_eq!(
181 terminated_abcd_efgh(&b"abcdefghijkl"[..]),
182 Ok((&b"ijkl"[..], &b"abcd"[..]))
183 );
184 assert_eq!(
185 terminated_abcd_efgh(&b"ab"[..]),
186 Err(Err::Incomplete(Needed::new(2)))
187 );
188 assert_eq!(
189 terminated_abcd_efgh(&b"abcde"[..]),
190 Err(Err::Incomplete(Needed::new(3)))
191 );
192 assert_eq!(
193 terminated_abcd_efgh(&b"xxx"[..]),
194 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
195 );
196 assert_eq!(
197 terminated_abcd_efgh(&b"xxxxdef"[..]),
198 Err(Err::Error(error_position!(&b"xxxxdef"[..], ErrorKind::Tag)))
199 );
200 assert_eq!(
201 terminated_abcd_efgh(&b"abcdxxxx"[..]),
202 Err(Err::Error(error_position!(&b"xxxx"[..], ErrorKind::Tag)))
203 );
204 }
205
206 #[test]
207 fn delimited_test() {
208 fn delimited_abc_def_ghi(i: &[u8]) -> IResult<&[u8], &[u8]> {
209 delimited(tag("abc"), tag("def"), tag("ghi")).parse(i)
210 }
211
212 assert_eq!(
213 delimited_abc_def_ghi(&b"abcdefghijkl"[..]),
214 Ok((&b"jkl"[..], &b"def"[..]))
215 );
216 assert_eq!(
217 delimited_abc_def_ghi(&b"ab"[..]),
218 Err(Err::Incomplete(Needed::new(1)))
219 );
220 assert_eq!(
221 delimited_abc_def_ghi(&b"abcde"[..]),
222 Err(Err::Incomplete(Needed::new(1)))
223 );
224 assert_eq!(
225 delimited_abc_def_ghi(&b"abcdefgh"[..]),
226 Err(Err::Incomplete(Needed::new(1)))
227 );
228 assert_eq!(
229 delimited_abc_def_ghi(&b"xxx"[..]),
230 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
231 );
232 assert_eq!(
233 delimited_abc_def_ghi(&b"xxxdefghi"[..]),
234 Err(Err::Error(error_position!(
235 &b"xxxdefghi"[..],
236 ErrorKind::Tag
237 ),))
238 );
239 assert_eq!(
240 delimited_abc_def_ghi(&b"abcxxxghi"[..]),
241 Err(Err::Error(error_position!(&b"xxxghi"[..], ErrorKind::Tag)))
242 );
243 assert_eq!(
244 delimited_abc_def_ghi(&b"abcdefxxx"[..]),
245 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
246 );
247 }
248
249 #[test]
250 fn tuple_test() {
251 #[allow(clippy::type_complexity)]
252 fn tuple_3(i: &[u8]) -> IResult<&[u8], (u16, &[u8], &[u8])> {
253 crate::Parser::parse(&mut (be_u16, take(3u8), tag("fg")), i)
254 }
255
256 assert_eq!(
257 tuple_3(&b"abcdefgh"[..]),
258 Ok((&b"h"[..], (0x6162u16, &b"cde"[..], &b"fg"[..])))
259 );
260 assert_eq!(tuple_3(&b"abcd"[..]), Err(Err::Incomplete(Needed::new(1))));
261 assert_eq!(tuple_3(&b"abcde"[..]), Err(Err::Incomplete(Needed::new(2))));
262 assert_eq!(
263 tuple_3(&b"abcdejk"[..]),
264 Err(Err::Error(error_position!(&b"jk"[..], ErrorKind::Tag)))
265 );
266 }
267
268 #[test]
269 #[allow(deprecated)]
270 fn unit_type() {
271 assert_eq!(
272 tuple::<&'static str, (), Error<&'static str>, ()>(())("abxsbsh"),
273 Ok(("abxsbsh", ()))
274 );
275 assert_eq!(
276 tuple::<&'static str, (), Error<&'static str>, ()>(())("sdfjakdsas"),
277 Ok(("sdfjakdsas", ()))
278 );
279 assert_eq!(
280 tuple::<&'static str, (), Error<&'static str>, ()>(())(""),
281 Ok(("", ()))
282 );
283 }