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