From f98b43aaba3f891d21fe8aac4d9c86cc3a36776c Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Wed, 19 Jul 2023 11:03:40 +0200 Subject: [PATCH] fixed tests --- src/text/buffer.rs | 1 + src/text/encoding.rs | 24 ++++++++++++------------ src/text/misc_token.rs | 13 ++++++++----- src/text/quoted.rs | 6 +++--- src/text/whitespace.rs | 32 ++++++++++++++++---------------- src/text/words.rs | 8 ++++---- 6 files changed, 44 insertions(+), 40 deletions(-) diff --git a/src/text/buffer.rs b/src/text/buffer.rs index f2973a5..e1b4c1f 100644 --- a/src/text/buffer.rs +++ b/src/text/buffer.rs @@ -26,6 +26,7 @@ impl<'a> Text<'a> { #[cfg(test)] mod tests { use super::*; + use crate::text::ascii; #[test] fn test_text() { diff --git a/src/text/encoding.rs b/src/text/encoding.rs index 610c127..a6427bb 100644 --- a/src/text/encoding.rs +++ b/src/text/encoding.rs @@ -166,20 +166,20 @@ mod tests { #[test] fn test_ptext() { assert_eq!( - ptext("Accus=E9_de_r=E9ception_(affich=E9)"), - Ok(("", vec![ - QuotedChunk::Safe("Accus"), + ptext(b"Accus=E9_de_r=E9ception_(affich=E9)"), + Ok((&b""[..], vec![ + QuotedChunk::Safe(&b"Accus"[..]), QuotedChunk::Encoded(0xe9), QuotedChunk::Space, - QuotedChunk::Safe("de"), + QuotedChunk::Safe(&b"de"[..]), QuotedChunk::Space, - QuotedChunk::Safe("r"), + QuotedChunk::Safe(&b"r"[..]), QuotedChunk::Encoded(0xe9), - QuotedChunk::Safe("ception"), + QuotedChunk::Safe(&b"ception"[..]), QuotedChunk::Space, - QuotedChunk::Safe("(affich"), + QuotedChunk::Safe(&b"(affich"[..]), QuotedChunk::Encoded(0xe9), - QuotedChunk::Safe(")"), + QuotedChunk::Safe(&b")"[..]), ])) ); } @@ -188,8 +188,8 @@ mod tests { #[test] fn test_decode_word() { assert_eq!( - encoded_word("=?iso8859-1?Q?Accus=E9_de_r=E9ception_(affich=E9)?="), - Ok(("", "Accusé de réception (affiché)".into())), + encoded_word(b"=?iso8859-1?Q?Accus=E9_de_r=E9ception_(affich=E9)?=").unwrap().1.to_string(), + "Accusé de réception (affiché)".to_string(), ); } @@ -197,8 +197,8 @@ mod tests { #[test] fn test_decode_word_b64() { assert_eq!( - encoded_word("=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?="), - Ok(("", "If you can read this yo".into())) + encoded_word(b"=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=").unwrap().1.to_string(), + "If you can read this yo".to_string(), ); } } diff --git a/src/text/misc_token.rs b/src/text/misc_token.rs index cb3120a..e51c63e 100644 --- a/src/text/misc_token.rs +++ b/src/text/misc_token.rs @@ -152,14 +152,17 @@ mod tests { use super::*; #[test] fn test_phrase() { - assert_eq!(phrase("hello world"), Ok(("", "hello world".into()))); assert_eq!( - phrase("salut \"le\" monde"), - Ok(("", "salut le monde".into())) + phrase(b"hello world").unwrap().1.to_string(), + "hello world".to_string(), ); assert_eq!( - phrase("fin\r\n du\r\nmonde"), - Ok(("\r\nmonde", "fin du".into())) + phrase(b"salut \"le\" monde").unwrap().1.to_string(), + "salut le monde".to_string(), ); + + let (rest, parsed) = phrase(b"fin\r\n du\r\nmonde").unwrap(); + assert_eq!(rest, &b"\r\nmonde"[..]); + assert_eq!(parsed.to_string(), "fin du".to_string()); } } diff --git a/src/text/quoted.rs b/src/text/quoted.rs index a36066a..2f33688 100644 --- a/src/text/quoted.rs +++ b/src/text/quoted.rs @@ -18,7 +18,7 @@ use crate::text::buffer; /// obs-qp = "\" (%d0 / obs-NO-WS-CTL / LF / CR) /// ``` pub fn quoted_pair(input: &[u8]) -> IResult<&[u8], &[u8]> { - preceded(tag(&[ascii::SLASH]), take(1usize))(input) + preceded(tag(&[ascii::BACKSLASH]), take(1usize))(input) } @@ -93,7 +93,7 @@ mod tests { text.push(b"world"); assert_eq!( quoted_string(b" \"hello\\\"world\" "), - Ok(("", text)) + Ok((&b""[..], text)) ); let mut text = buffer::Text::default(); @@ -102,7 +102,7 @@ mod tests { text.push(b"world"); assert_eq!( quoted_string(b"\"hello\r\n world\""), - Ok(("", text)) + Ok((&b""[..], text)) ); } } diff --git a/src/text/whitespace.rs b/src/text/whitespace.rs index 28050b2..da1134f 100644 --- a/src/text/whitespace.rs +++ b/src/text/whitespace.rs @@ -149,43 +149,43 @@ mod tests { #[test] fn test_obs_crlf() { - assert_eq!(obs_crlf("\rworld"), Ok(("world", "\r"))); - assert_eq!(obs_crlf("\r\nworld"), Ok(("world", "\r\n"))); - assert_eq!(obs_crlf("\nworld"), Ok(("world", "\n"))); + assert_eq!(obs_crlf(b"\rworld"), Ok((&b"world"[..], &b"\r"[..]))); + assert_eq!(obs_crlf(b"\r\nworld"), Ok((&b"world"[..], &b"\r\n"[..]))); + assert_eq!(obs_crlf(b"\nworld"), Ok((&b"world"[..], &b"\n"[..]))); } #[test] fn test_fws() { - assert_eq!(fws("\r\n world"), Ok(("world", ' '))); - assert_eq!(fws(" \r\n \r\n world"), Ok(("world", ' '))); - assert_eq!(fws(" world"), Ok(("world", ' '))); - assert!(fws("\r\nFrom: test").is_err()); + assert_eq!(fws(b"\r\n world"), Ok((&b"world"[..], ascii::SP))); + assert_eq!(fws(b" \r\n \r\n world"), Ok((&b"world"[..], ascii::SP))); + assert_eq!(fws(b" world"), Ok((&b"world"[..], ascii::SP))); + assert!(fws(b"\r\nFrom: test").is_err()); } #[test] fn test_cfws() { assert_eq!( - cfws("(A nice \\) chap) "), + cfws(b"(A nice \\) chap) "), Ok(( - "", - "(A nice \\) chap) " + &b""[..], + &b"(A nice \\) chap) "[..] )) ); assert_eq!( - cfws("(Chris's host.)public.example>,"), - Ok(("public.example>,", "(Chris's host.)")) + cfws(b"(Chris's host.)public.example>,"), + Ok((&b"public.example>,"[..], &b"(Chris's host.)"[..])) ); assert_eq!( - cfws("(double (comment) is fun) wouch"), - Ok(("wouch", "(double (comment) is fun) ")) + cfws(b"(double (comment) is fun) wouch"), + Ok((&b"wouch"[..], &b"(double (comment) is fun) "[..])) ); } #[test] fn test_cfws_encoded_word() { assert_eq!( - cfws("(=?US-ASCII?Q?Keith_Moore?=)"), - Ok(("", "(=?US-ASCII?Q?Keith_Moore?=)")), + cfws(b"(=?US-ASCII?Q?Keith_Moore?=)"), + Ok((&b""[..], &b"(=?US-ASCII?Q?Keith_Moore?=)"[..])), ); } } diff --git a/src/text/words.rs b/src/text/words.rs index 6a50d7a..9e48344 100644 --- a/src/text/words.rs +++ b/src/text/words.rs @@ -118,16 +118,16 @@ mod tests { #[test] fn test_dot_atom_text() { assert_eq!( - dot_atom_text("quentin.dufour.io abcdef"), - Ok((" abcdef", "quentin.dufour.io")) + dot_atom_text(b"quentin.dufour.io abcdef"), + Ok((&b" abcdef"[..], &b"quentin.dufour.io"[..])) ); } #[test] fn test_dot_atom() { assert_eq!( - dot_atom(" (skip) quentin.dufour.io abcdef"), - Ok(("abcdef", "quentin.dufour.io")) + dot_atom(b" (skip) quentin.dufour.io abcdef"), + Ok((&b"abcdef"[..], &b"quentin.dufour.io"[..])) ); } }