1 #[cfg(test)]
2 mod test {
3   use crate::{Err, error::ErrorKind, IResult};
4 
5   #[test]
tagtr_succeed()6   fn tagtr_succeed() {
7     const INPUT: &str = "Hello World!";
8     const TAG: &str = "Hello";
9     fn test(input: &str) -> IResult<&str, &str> {
10       tag!(input, TAG)
11     }
12 
13     match test(INPUT) {
14       Ok((extra, output)) => {
15         assert!(
16           extra == " World!",
17           "Parser `tag` consumed leftover input."
18         );
19         assert!(
20           output == TAG,
21           "Parser `tag` doesn't return the tag it matched on success. \
22            Expected `{}`, got `{}`.",
23           TAG,
24           output
25         );
26       }
27       other => panic!(
28         "Parser `tag` didn't succeed when it should have. \
29          Got `{:?}`.",
30         other
31       ),
32     };
33   }
34 
35   #[test]
tagtr_incomplete()36   fn tagtr_incomplete() {
37     const INPUT: &str = "Hello";
38     const TAG: &str = "Hello World!";
39 
40     let res: IResult<_,_,(_, ErrorKind)> = tag!(INPUT, TAG);
41     match res {
42       Err(Err::Incomplete(_)) => (),
43       other => {
44         panic!(
45           "Parser `tag` didn't require more input when it should have. \
46            Got `{:?}`.",
47           other
48         );
49       }
50     };
51   }
52 
53   #[test]
tagtr_error()54   fn tagtr_error() {
55     const INPUT: &str = "Hello World!";
56     const TAG: &str = "Random"; // TAG must be closer than INPUT.
57 
58     let res: IResult<_,_,(_, ErrorKind)> = tag!(INPUT, TAG);
59     match res {
60       Err(Err::Error(_)) => (),
61       other => {
62         panic!(
63           "Parser `tag` didn't fail when it should have. Got `{:?}`.`",
64           other
65         );
66       }
67     };
68   }
69 
70   #[test]
take_s_succeed()71   fn take_s_succeed() {
72     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
73     const CONSUMED: &str = "βèƒôřèÂßÇ";
74     const LEFTOVER: &str = "áƒƭèř";
75 
76     let res: IResult<_,_,(_, ErrorKind)> = take!(INPUT, 9);
77     match res {
78       Ok((extra, output)) => {
79         assert!(
80           extra == LEFTOVER,
81           "Parser `take_s` consumed leftover input. Leftover `{}`.",
82           extra
83         );
84         assert!(
85           output == CONSUMED,
86           "Parser `take_s` doens't return the string it consumed on success. Expected `{}`, got `{}`.",
87           CONSUMED,
88           output
89         );
90       }
91       other => panic!(
92         "Parser `take_s` didn't succeed when it should have. \
93          Got `{:?}`.",
94         other
95       ),
96     };
97   }
98 
99   #[test]
take_until_succeed()100   fn take_until_succeed() {
101     const INPUT: &str = "βèƒôřèÂßÇ∂áƒƭèř";
102     const FIND: &str = "ÂßÇ∂";
103     const CONSUMED: &str = "βèƒôřè";
104     const LEFTOVER: &str = "ÂßÇ∂áƒƭèř";
105 
106     let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND);
107     match res {
108       Ok((extra, output)) => {
109         assert!(
110           extra == LEFTOVER,
111           "Parser `take_until`\
112            consumed leftover input. Leftover `{}`.",
113           extra
114         );
115         assert!(
116           output == CONSUMED,
117           "Parser `take_until`\
118            doens't return the string it consumed on success. Expected `{}`, got `{}`.",
119           CONSUMED,
120           output
121         );
122       }
123       other => panic!(
124         "Parser `take_until` didn't succeed when it should have. \
125          Got `{:?}`.",
126         other
127       ),
128     };
129   }
130 
131   #[test]
take_s_incomplete()132   fn take_s_incomplete() {
133     const INPUT: &str = "βèƒôřèÂßÇá";
134 
135     let res: IResult<_,_,(_, ErrorKind)> = take!(INPUT, 13);
136     match res {
137       Err(Err::Incomplete(_)) => (),
138       other => panic!(
139         "Parser `take` didn't require more input when it should have. \
140          Got `{:?}`.",
141         other
142       ),
143     }
144   }
145 
146   use crate::internal::Needed;
147 
is_alphabetic(c: char) -> bool148   fn is_alphabetic(c: char) -> bool {
149     (c as u8 >= 0x41 && c as u8 <= 0x5A) || (c as u8 >= 0x61 && c as u8 <= 0x7A)
150   }
151 
152   #[test]
take_while()153   fn take_while() {
154     named!(f<&str,&str>, take_while!(is_alphabetic));
155     let a = "";
156     let b = "abcd";
157     let c = "abcd123";
158     let d = "123";
159 
160     assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1))));
161     assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1))));
162     assert_eq!(f(&c[..]), Ok((&d[..], &b[..])));
163     assert_eq!(f(&d[..]), Ok((&d[..], &a[..])));
164   }
165 
166   #[test]
take_while1()167   fn take_while1() {
168     named!(f<&str,&str>, take_while1!(is_alphabetic));
169     let a = "";
170     let b = "abcd";
171     let c = "abcd123";
172     let d = "123";
173 
174     assert_eq!(f(&a[..]), Err(Err::Incomplete(Needed::Size(1))));
175     assert_eq!(f(&b[..]), Err(Err::Incomplete(Needed::Size(1))));
176     assert_eq!(f(&c[..]), Ok((&"123"[..], &b[..])));
177     assert_eq!(
178       f(&d[..]),
179       Err(Err::Error(error_position!(&d[..], ErrorKind::TakeWhile1)))
180     );
181   }
182 
183   #[test]
take_till_s_succeed()184   fn take_till_s_succeed() {
185     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
186     const CONSUMED: &str = "βèƒôřèÂßÇ";
187     const LEFTOVER: &str = "áƒƭèř";
188     fn till_s(c: char) -> bool {
189       c == 'á'
190     }
191     fn test(input: &str) -> IResult<&str, &str> {
192       take_till!(input, till_s)
193     }
194     match test(INPUT) {
195       Ok((extra, output)) => {
196         assert!(
197           extra == LEFTOVER,
198           "Parser `take_till` consumed leftover input."
199         );
200         assert!(
201           output == CONSUMED,
202           "Parser `take_till` doesn't return the string it consumed on success. \
203            Expected `{}`, got `{}`.",
204           CONSUMED,
205           output
206         );
207       }
208       other => panic!(
209         "Parser `take_till` didn't succeed when it should have. \
210          Got `{:?}`.",
211         other
212       ),
213     };
214   }
215 
216   #[test]
take_while_succeed_none()217   fn take_while_succeed_none() {
218     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
219     const CONSUMED: &str = "";
220     const LEFTOVER: &str = "βèƒôřèÂßÇáƒƭèř";
221     fn while_s(c: char) -> bool {
222       c == '9'
223     }
224     fn test(input: &str) -> IResult<&str, &str> {
225       take_while!(input, while_s)
226     }
227     match test(INPUT) {
228       Ok((extra, output)) => {
229         assert!(
230           extra == LEFTOVER,
231           "Parser `take_while` consumed leftover input."
232         );
233         assert!(
234           output == CONSUMED,
235           "Parser `take_while` doesn't return the string it consumed on success. \
236            Expected `{}`, got `{}`.",
237           CONSUMED,
238           output
239         );
240       }
241       other => panic!(
242         "Parser `take_while` didn't succeed when it should have. \
243          Got `{:?}`.",
244         other
245       ),
246     };
247   }
248 
249   #[test]
is_not_succeed()250   fn is_not_succeed() {
251     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
252     const AVOID: &str = "£úçƙ¥á";
253     const CONSUMED: &str = "βèƒôřèÂßÇ";
254     const LEFTOVER: &str = "áƒƭèř";
255     fn test(input: &str) -> IResult<&str, &str> {
256       is_not!(input, AVOID)
257     }
258     match test(INPUT) {
259       Ok((extra, output)) => {
260         assert!(
261           extra == LEFTOVER,
262           "Parser `is_not` consumed leftover input. Leftover `{}`.",
263           extra
264         );
265         assert!(
266           output == CONSUMED,
267           "Parser `is_not` doens't return the string it consumed on success. Expected `{}`, got `{}`.",
268           CONSUMED,
269           output
270         );
271       }
272       other => panic!(
273         "Parser `is_not` didn't succeed when it should have. \
274          Got `{:?}`.",
275         other
276       ),
277     };
278   }
279 
280   #[test]
take_while_succeed_some()281   fn take_while_succeed_some() {
282     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
283     const CONSUMED: &str = "βèƒôřèÂßÇ";
284     const LEFTOVER: &str = "áƒƭèř";
285     fn while_s(c: char) -> bool {
286       c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç'
287     }
288     fn test(input: &str) -> IResult<&str, &str> {
289       take_while!(input, while_s)
290     }
291     match test(INPUT) {
292       Ok((extra, output)) => {
293         assert!(
294           extra == LEFTOVER,
295           "Parser `take_while` consumed leftover input."
296         );
297         assert!(
298           output == CONSUMED,
299           "Parser `take_while` doesn't return the string it consumed on success. \
300            Expected `{}`, got `{}`.",
301           CONSUMED,
302           output
303         );
304       }
305       other => panic!(
306         "Parser `take_while` didn't succeed when it should have. \
307          Got `{:?}`.",
308         other
309       ),
310     };
311   }
312 
313   #[test]
is_not_fail()314   fn is_not_fail() {
315     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
316     const AVOID: &str = "βúçƙ¥";
317     fn test(input: &str) -> IResult<&str, &str> {
318       is_not!(input, AVOID)
319     }
320     match test(INPUT) {
321       Err(Err::Error(_)) => (),
322       other => panic!(
323         "Parser `is_not` didn't fail when it should have. Got `{:?}`.",
324         other
325       ),
326     };
327   }
328 
329   #[test]
take_while1_succeed()330   fn take_while1_succeed() {
331     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
332     const CONSUMED: &str = "βèƒôřèÂßÇ";
333     const LEFTOVER: &str = "áƒƭèř";
334     fn while1_s(c: char) -> bool {
335       c == 'β' || c == 'è' || c == 'ƒ' || c == 'ô' || c == 'ř' || c == 'è' || c == 'Â' || c == 'ß' || c == 'Ç'
336     }
337     fn test(input: &str) -> IResult<&str, &str> {
338       take_while1!(input, while1_s)
339     }
340     match test(INPUT) {
341       Ok((extra, output)) => {
342         assert!(
343           extra == LEFTOVER,
344           "Parser `take_while1` consumed leftover input."
345         );
346         assert!(
347           output == CONSUMED,
348           "Parser `take_while1` doesn't return the string it consumed on success. \
349            Expected `{}`, got `{}`.",
350           CONSUMED,
351           output
352         );
353       }
354       other => panic!(
355         "Parser `take_while1` didn't succeed when it should have. \
356          Got `{:?}`.",
357         other
358       ),
359     };
360   }
361 
362   #[test]
take_until_incomplete()363   fn take_until_incomplete() {
364     const INPUT: &str = "βèƒôřè";
365     const FIND: &str = "βèƒôřèÂßÇ";
366 
367     let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND);
368     match res {
369       Err(Err::Incomplete(_)) => (),
370       other => panic!(
371         "Parser `take_until` didn't require more input when it should have. \
372          Got `{:?}`.",
373         other
374       ),
375     };
376   }
377 
378   #[test]
is_a_succeed()379   fn is_a_succeed() {
380     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
381     const MATCH: &str = "βèƒôřèÂßÇ";
382     const CONSUMED: &str = "βèƒôřèÂßÇ";
383     const LEFTOVER: &str = "áƒƭèř";
384     fn test(input: &str) -> IResult<&str, &str> {
385       is_a!(input, MATCH)
386     }
387     match test(INPUT) {
388       Ok((extra, output)) => {
389         assert!(
390           extra == LEFTOVER,
391           "Parser `is_a` consumed leftover input. Leftover `{}`.",
392           extra
393         );
394         assert!(
395           output == CONSUMED,
396           "Parser `is_a` doens't return the string it consumed on success. Expected `{}`, got `{}`.",
397           CONSUMED,
398           output
399         );
400       }
401       other => panic!(
402         "Parser `is_a` didn't succeed when it should have. \
403          Got `{:?}`.",
404         other
405       ),
406     };
407   }
408 
409   #[test]
take_while1_fail()410   fn take_while1_fail() {
411     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
412     fn while1_s(c: char) -> bool {
413       c == '9'
414     }
415     fn test(input: &str) -> IResult<&str, &str> {
416       take_while1!(input, while1_s)
417     }
418     match test(INPUT) {
419       Err(Err::Error(_)) => (),
420       other => panic!(
421         "Parser `take_while1` didn't fail when it should have. \
422          Got `{:?}`.",
423         other
424       ),
425     };
426   }
427 
428   #[test]
is_a_fail()429   fn is_a_fail() {
430     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
431     const MATCH: &str = "Ûñℓúçƙ¥";
432     fn test(input: &str) -> IResult<&str, &str> {
433       is_a!(input, MATCH)
434     }
435     match test(INPUT) {
436       Err(Err::Error(_)) => (),
437       other => panic!(
438         "Parser `is_a` didn't fail when it should have. Got `{:?}`.",
439         other
440       ),
441     };
442   }
443 
444   #[test]
take_until_error()445   fn take_until_error() {
446     const INPUT: &str = "βèƒôřèÂßÇáƒƭèř";
447     const FIND: &str = "Ráñδô₥";
448 
449     let res: IResult<_,_,(_, ErrorKind)> = take_until!(INPUT, FIND);
450     match res {
451       Err(Err::Incomplete(_)) => (),
452       other => panic!(
453         "Parser `take_until` didn't fail when it should have. \
454          Got `{:?}`.",
455         other
456       ),
457     };
458   }
459 
460   #[test]
461   #[cfg(feature = "alloc")]
recognize_is_a()462   fn recognize_is_a() {
463     let a = "aabbab";
464     let b = "ababcd";
465 
466     named!(f <&str,&str>, recognize!(many1!(complete!(alt!( tag!("a") | tag!("b") )))));
467 
468     assert_eq!(f(&a[..]), Ok((&a[6..], &a[..])));
469     assert_eq!(f(&b[..]), Ok((&b[4..], &b[..4])));
470   }
471 
472   #[test]
utf8_indexing()473   fn utf8_indexing() {
474     named!(dot(&str) -> &str,
475         tag!(".")
476       );
477 
478     let _ = dot("點");
479   }
480 
481   #[cfg(feature = "alloc")]
482   #[test]
case_insensitive()483   fn case_insensitive() {
484     named!(test<&str,&str>, tag_no_case!("ABcd"));
485     assert_eq!(test("aBCdefgh"), Ok(("efgh", "aBCd")));
486     assert_eq!(test("abcdefgh"), Ok(("efgh", "abcd")));
487     assert_eq!(test("ABCDefgh"), Ok(("efgh", "ABCD")));
488 
489     named!(test2<&str,&str>, tag_no_case!("ABcd"));
490     assert_eq!(test2("aBCdefgh"), Ok(("efgh", "aBCd")));
491     assert_eq!(test2("abcdefgh"), Ok(("efgh", "abcd")));
492     assert_eq!(test2("ABCDefgh"), Ok(("efgh", "ABCD")));
493   }
494 }
495