1 use super::*;
2 use crate::bytes::complete::take;
3 use crate::bytes::streaming::tag;
4 use crate::error::ErrorKind;
5 use crate::error::ParseError;
6 use crate::internal::{Err, IResult, Needed};
7 #[cfg(feature = "alloc")]
8 use crate::lib::std::boxed::Box;
9 use crate::number::complete::u8;
10
11 macro_rules! assert_parse(
12 ($left: expr, $right: expr) => {
13 let res: $crate::IResult<_, _, (_, ErrorKind)> = $left;
14 assert_eq!(res, $right);
15 };
16 );
17
18 /*#[test]
19 fn t1() {
20 let v1:Vec<u8> = vec![1,2,3];
21 let v2:Vec<u8> = vec![4,5,6];
22 let d = Ok((&v1[..], &v2[..]));
23 let res = d.flat_map(print);
24 assert_eq!(res, Ok((&v2[..], ())));
25 }*/
26
27 #[test]
eof_on_slices()28 fn eof_on_slices() {
29 let not_over: &[u8] = &b"Hello, world!"[..];
30 let is_over: &[u8] = &b""[..];
31
32 let res_not_over = eof(not_over);
33 assert_parse!(
34 res_not_over,
35 Err(Err::Error(error_position!(not_over, ErrorKind::Eof)))
36 );
37
38 let res_over = eof(is_over);
39 assert_parse!(res_over, Ok((is_over, is_over)));
40 }
41
42 #[test]
eof_on_strs()43 fn eof_on_strs() {
44 let not_over: &str = "Hello, world!";
45 let is_over: &str = "";
46
47 let res_not_over = eof(not_over);
48 assert_parse!(
49 res_not_over,
50 Err(Err::Error(error_position!(not_over, ErrorKind::Eof)))
51 );
52
53 let res_over = eof(is_over);
54 assert_parse!(res_over, Ok((is_over, is_over)));
55 }
56
57 /*
58 #[test]
59 fn end_of_input() {
60 let not_over = &b"Hello, world!"[..];
61 let is_over = &b""[..];
62 named!(eof_test, eof!());
63
64 let res_not_over = eof_test(not_over);
65 assert_eq!(res_not_over, Err(Err::Error(error_position!(not_over, ErrorKind::Eof))));
66
67 let res_over = eof_test(is_over);
68 assert_eq!(res_over, Ok((is_over, is_over)));
69 }
70 */
71
72 #[test]
rest_on_slices()73 fn rest_on_slices() {
74 let input: &[u8] = &b"Hello, world!"[..];
75 let empty: &[u8] = &b""[..];
76 assert_parse!(rest(input), Ok((empty, input)));
77 }
78
79 #[test]
rest_on_strs()80 fn rest_on_strs() {
81 let input: &str = "Hello, world!";
82 let empty: &str = "";
83 assert_parse!(rest(input), Ok((empty, input)));
84 }
85
86 #[test]
rest_len_on_slices()87 fn rest_len_on_slices() {
88 let input: &[u8] = &b"Hello, world!"[..];
89 assert_parse!(rest_len(input), Ok((input, input.len())));
90 }
91
92 use crate::lib::std::convert::From;
93 impl From<u32> for CustomError {
from(_: u32) -> Self94 fn from(_: u32) -> Self {
95 CustomError
96 }
97 }
98
99 impl<I> ParseError<I> for CustomError {
from_error_kind(_: I, _: ErrorKind) -> Self100 fn from_error_kind(_: I, _: ErrorKind) -> Self {
101 CustomError
102 }
103
append(_: I, _: ErrorKind, _: CustomError) -> Self104 fn append(_: I, _: ErrorKind, _: CustomError) -> Self {
105 CustomError
106 }
107 }
108
109 struct CustomError;
110 #[allow(dead_code)]
custom_error(input: &[u8]) -> IResult<&[u8], &[u8], CustomError>111 fn custom_error(input: &[u8]) -> IResult<&[u8], &[u8], CustomError> {
112 //fix_error!(input, CustomError, alphanumeric)
113 crate::character::streaming::alphanumeric1(input)
114 }
115
116 #[test]
test_flat_map()117 fn test_flat_map() {
118 let input: &[u8] = &[3, 100, 101, 102, 103, 104][..];
119 assert_parse!(
120 flat_map(u8, take)(input),
121 Ok((&[103, 104][..], &[100, 101, 102][..]))
122 );
123 }
124
125 #[test]
test_map_opt()126 fn test_map_opt() {
127 let input: &[u8] = &[50][..];
128 assert_parse!(
129 map_opt(u8, |u| if u < 20 { Some(u) } else { None })(input),
130 Err(Err::Error((&[50][..], ErrorKind::MapOpt)))
131 );
132 assert_parse!(
133 map_opt(u8, |u| if u > 20 { Some(u) } else { None })(input),
134 Ok((&[][..], 50))
135 );
136 }
137
138 #[test]
test_map_parser()139 fn test_map_parser() {
140 let input: &[u8] = &[100, 101, 102, 103, 104][..];
141 assert_parse!(
142 map_parser(take(4usize), take(2usize))(input),
143 Ok((&[104][..], &[100, 101][..]))
144 );
145 }
146
147 #[test]
test_all_consuming()148 fn test_all_consuming() {
149 let input: &[u8] = &[100, 101, 102][..];
150 assert_parse!(
151 all_consuming(take(2usize))(input),
152 Err(Err::Error((&[102][..], ErrorKind::Eof)))
153 );
154 assert_parse!(
155 all_consuming(take(3usize))(input),
156 Ok((&[][..], &[100, 101, 102][..]))
157 );
158 }
159
160 #[test]
161 #[allow(unused)]
test_verify_ref()162 fn test_verify_ref() {
163 use crate::bytes::complete::take;
164
165 let mut parser1 = verify(take(3u8), |s: &[u8]| s == &b"abc"[..]);
166
167 assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], &b"abc"[..])));
168 assert_eq!(
169 parser1(&b"defg"[..]),
170 Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))
171 );
172
173 fn parser2(i: &[u8]) -> IResult<&[u8], u32> {
174 verify(crate::number::streaming::be_u32, |val: &u32| *val < 3)(i)
175 }
176 }
177
178 #[test]
179 #[cfg(feature = "alloc")]
test_verify_alloc()180 fn test_verify_alloc() {
181 use crate::bytes::complete::take;
182 let mut parser1 = verify(map(take(3u8), |s: &[u8]| s.to_vec()), |s: &[u8]| {
183 s == &b"abc"[..]
184 });
185
186 assert_eq!(parser1(&b"abcd"[..]), Ok((&b"d"[..], (&b"abc").to_vec())));
187 assert_eq!(
188 parser1(&b"defg"[..]),
189 Err(Err::Error((&b"defg"[..], ErrorKind::Verify)))
190 );
191 }
192
193 #[test]
194 #[cfg(feature = "std")]
test_into()195 fn test_into() {
196 use crate::bytes::complete::take;
197 use crate::{
198 error::{Error, ParseError},
199 Err,
200 };
201
202 let mut parser = into(take::<_, _, Error<_>>(3u8));
203 let result: IResult<&[u8], Vec<u8>> = parser(&b"abcdefg"[..]);
204
205 assert_eq!(result, Ok((&b"defg"[..], vec![97, 98, 99])));
206 }
207
208 #[test]
opt_test()209 fn opt_test() {
210 fn opt_abcd(i: &[u8]) -> IResult<&[u8], Option<&[u8]>> {
211 opt(tag("abcd"))(i)
212 }
213
214 let a = &b"abcdef"[..];
215 let b = &b"bcdefg"[..];
216 let c = &b"ab"[..];
217 assert_eq!(opt_abcd(a), Ok((&b"ef"[..], Some(&b"abcd"[..]))));
218 assert_eq!(opt_abcd(b), Ok((&b"bcdefg"[..], None)));
219 assert_eq!(opt_abcd(c), Err(Err::Incomplete(Needed::new(2))));
220 }
221
222 #[test]
peek_test()223 fn peek_test() {
224 fn peek_tag(i: &[u8]) -> IResult<&[u8], &[u8]> {
225 peek(tag("abcd"))(i)
226 }
227
228 assert_eq!(peek_tag(&b"abcdef"[..]), Ok((&b"abcdef"[..], &b"abcd"[..])));
229 assert_eq!(peek_tag(&b"ab"[..]), Err(Err::Incomplete(Needed::new(2))));
230 assert_eq!(
231 peek_tag(&b"xxx"[..]),
232 Err(Err::Error(error_position!(&b"xxx"[..], ErrorKind::Tag)))
233 );
234 }
235
236 #[test]
not_test()237 fn not_test() {
238 fn not_aaa(i: &[u8]) -> IResult<&[u8], ()> {
239 not(tag("aaa"))(i)
240 }
241
242 assert_eq!(
243 not_aaa(&b"aaa"[..]),
244 Err(Err::Error(error_position!(&b"aaa"[..], ErrorKind::Not)))
245 );
246 assert_eq!(not_aaa(&b"aa"[..]), Err(Err::Incomplete(Needed::new(1))));
247 assert_eq!(not_aaa(&b"abcd"[..]), Ok((&b"abcd"[..], ())));
248 }
249
250 #[test]
verify_test()251 fn verify_test() {
252 use crate::bytes::streaming::take;
253
254 fn test(i: &[u8]) -> IResult<&[u8], &[u8]> {
255 verify(take(5u8), |slice: &[u8]| slice[0] == b'a')(i)
256 }
257 assert_eq!(test(&b"bcd"[..]), Err(Err::Incomplete(Needed::new(2))));
258 assert_eq!(
259 test(&b"bcdefg"[..]),
260 Err(Err::Error(error_position!(
261 &b"bcdefg"[..],
262 ErrorKind::Verify
263 )))
264 );
265 assert_eq!(test(&b"abcdefg"[..]), Ok((&b"fg"[..], &b"abcde"[..])));
266 }
267
268 #[test]
fail_test()269 fn fail_test() {
270 let a = "string";
271 let b = "another string";
272
273 assert_eq!(fail::<_, &str, _>(a), Err(Err::Error((a, ErrorKind::Fail))));
274 assert_eq!(fail::<_, &str, _>(b), Err(Err::Error((b, ErrorKind::Fail))));
275 }
276