Skip to content

Commit

Permalink
Merge pull request #24 from epage/tests
Browse files Browse the repository at this point in the history
refactor(tests): Clearly divide streaming from complete
  • Loading branch information
epage authored Nov 28, 2022
2 parents 2a07b71 + 14b2b9b commit 913c811
Show file tree
Hide file tree
Showing 7 changed files with 700 additions and 704 deletions.
216 changes: 216 additions & 0 deletions src/bytes/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,14 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::character::complete::{alpha1 as alpha, digit1 as digit};
#[cfg(feature = "alloc")]
use crate::{
branch::alt,
combinator::{map, value},
lib::std::string::String,
lib::std::vec::Vec,
};

#[test]
fn complete_take_while_m_n_utf8_all_matching() {
Expand Down Expand Up @@ -752,4 +760,212 @@ mod tests {
fn escaped_hang_1118() {
assert_eq!(unquote(r#""""#), Ok(("", "")));
}

#[cfg(feature = "alloc")]
#[allow(unused_variables)]
#[test]
fn escaping() {
use crate::character::complete::one_of;

fn esc(i: &[u8]) -> IResult<&[u8], &[u8]> {
escaped(alpha, '\\', one_of("\"n\\"))(i)
}
assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], &b"abcd"[..])));
assert_eq!(esc(&b"ab\\\"cd;"[..]), Ok((&b";"[..], &b"ab\\\"cd"[..])));
assert_eq!(esc(&b"\\\"abcd;"[..]), Ok((&b";"[..], &b"\\\"abcd"[..])));
assert_eq!(esc(&b"\\n;"[..]), Ok((&b";"[..], &b"\\n"[..])));
assert_eq!(esc(&b"ab\\\"12"[..]), Ok((&b"12"[..], &b"ab\\\""[..])));
assert_eq!(
esc(&b"AB\\"[..]),
Err(Err::Error(error_position!(
&b"AB\\"[..],
ErrorKind::Escaped
)))
);
assert_eq!(
esc(&b"AB\\A"[..]),
Err(Err::Error(error_node_position!(
&b"AB\\A"[..],
ErrorKind::Escaped,
error_position!(&b"A"[..], ErrorKind::OneOf)
)))
);

fn esc2(i: &[u8]) -> IResult<&[u8], &[u8]> {
escaped(digit, '\\', one_of("\"n\\"))(i)
}
assert_eq!(esc2(&b"12\\nnn34"[..]), Ok((&b"nn34"[..], &b"12\\n"[..])));
}

#[cfg(feature = "alloc")]
#[test]
fn escaping_str() {
use crate::character::complete::one_of;

fn esc(i: &str) -> IResult<&str, &str> {
escaped(alpha, '\\', one_of("\"n\\"))(i)
}
assert_eq!(esc("abcd;"), Ok((";", "abcd")));
assert_eq!(esc("ab\\\"cd;"), Ok((";", "ab\\\"cd")));
assert_eq!(esc("\\\"abcd;"), Ok((";", "\\\"abcd")));
assert_eq!(esc("\\n;"), Ok((";", "\\n")));
assert_eq!(esc("ab\\\"12"), Ok(("12", "ab\\\"")));
assert_eq!(
esc("AB\\"),
Err(Err::Error(error_position!("AB\\", ErrorKind::Escaped)))
);
assert_eq!(
esc("AB\\A"),
Err(Err::Error(error_node_position!(
"AB\\A",
ErrorKind::Escaped,
error_position!("A", ErrorKind::OneOf)
)))
);

fn esc2(i: &str) -> IResult<&str, &str> {
escaped(digit, '\\', one_of("\"n\\"))(i)
}
assert_eq!(esc2("12\\nnn34"), Ok(("nn34", "12\\n")));

fn esc3(i: &str) -> IResult<&str, &str> {
escaped(alpha, '\u{241b}', one_of("\"n"))(i)
}
assert_eq!(esc3("ab␛ncd;"), Ok((";", "ab␛ncd")));
}

#[cfg(feature = "alloc")]
fn to_s(i: Vec<u8>) -> String {
String::from_utf8_lossy(&i).into_owned()
}

#[cfg(feature = "alloc")]
#[test]
fn escape_transform() {
fn esc(i: &[u8]) -> IResult<&[u8], String> {
map(
escaped_transform(
alpha,
'\\',
alt((
value(&b"\\"[..], tag("\\")),
value(&b"\""[..], tag("\"")),
value(&b"\n"[..], tag("n")),
)),
),
to_s,
)(i)
}

assert_eq!(esc(&b"abcd;"[..]), Ok((&b";"[..], String::from("abcd"))));
assert_eq!(
esc(&b"ab\\\"cd;"[..]),
Ok((&b";"[..], String::from("ab\"cd")))
);
assert_eq!(
esc(&b"\\\"abcd;"[..]),
Ok((&b";"[..], String::from("\"abcd")))
);
assert_eq!(esc(&b"\\n;"[..]), Ok((&b";"[..], String::from("\n"))));
assert_eq!(
esc(&b"ab\\\"12"[..]),
Ok((&b"12"[..], String::from("ab\"")))
);
assert_eq!(
esc(&b"AB\\"[..]),
Err(Err::Error(error_position!(
&b"\\"[..],
ErrorKind::EscapedTransform
)))
);
assert_eq!(
esc(&b"AB\\A"[..]),
Err(Err::Error(error_node_position!(
&b"AB\\A"[..],
ErrorKind::EscapedTransform,
error_position!(&b"A"[..], ErrorKind::Tag)
)))
);

fn esc2(i: &[u8]) -> IResult<&[u8], String> {
map(
escaped_transform(
alpha,
'&',
alt((
value("è".as_bytes(), tag("egrave;")),
value("à".as_bytes(), tag("agrave;")),
)),
),
to_s,
)(i)
}
assert_eq!(
esc2(&b"ab&egrave;DEF;"[..]),
Ok((&b";"[..], String::from("abèDEF")))
);
assert_eq!(
esc2(&b"ab&egrave;D&agrave;EF;"[..]),
Ok((&b";"[..], String::from("abèDàEF")))
);
}

#[cfg(feature = "std")]
#[test]
fn escape_transform_str() {
fn esc(i: &str) -> IResult<&str, String> {
escaped_transform(
alpha,
'\\',
alt((
value("\\", tag("\\")),
value("\"", tag("\"")),
value("\n", tag("n")),
)),
)(i)
}

assert_eq!(esc("abcd;"), Ok((";", String::from("abcd"))));
assert_eq!(esc("ab\\\"cd;"), Ok((";", String::from("ab\"cd"))));
assert_eq!(esc("\\\"abcd;"), Ok((";", String::from("\"abcd"))));
assert_eq!(esc("\\n;"), Ok((";", String::from("\n"))));
assert_eq!(esc("ab\\\"12"), Ok(("12", String::from("ab\""))));
assert_eq!(
esc("AB\\"),
Err(Err::Error(error_position!(
"\\",
ErrorKind::EscapedTransform
)))
);
assert_eq!(
esc("AB\\A"),
Err(Err::Error(error_node_position!(
"AB\\A",
ErrorKind::EscapedTransform,
error_position!("A", ErrorKind::Tag)
)))
);

fn esc2(i: &str) -> IResult<&str, String> {
escaped_transform(
alpha,
'&',
alt((value("è", tag("egrave;")), value("à", tag("agrave;")))),
)(i)
}
assert_eq!(esc2("ab&egrave;DEF;"), Ok((";", String::from("abèDEF"))));
assert_eq!(
esc2("ab&egrave;D&agrave;EF;"),
Ok((";", String::from("abèDàEF")))
);

fn esc3(i: &str) -> IResult<&str, String> {
escaped_transform(
alpha,
'␛',
alt((value("\0", tag("0")), value("\n", tag("n")))),
)(i)
}
assert_eq!(esc3("a␛0bc␛n"), Ok(("", String::from("a\0bc\n"))));
}
}
2 changes: 0 additions & 2 deletions src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@

pub mod complete;
pub mod streaming;
#[cfg(test)]
mod tests;
Loading

0 comments on commit 913c811

Please sign in to comment.