Add failure tests for parse_meta
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-13 21:38:36 +01:00
parent 5293048580
commit 66a611fa6e
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -176,23 +176,15 @@ fn parse_meta(s: Span) -> IResult<Span, Metadata> {
delimited(tag("#"), take_while1(|c| c != '#' && c != '\n'), tag("\n")), delimited(tag("#"), take_while1(|c| c != '#' && c != '\n'), tag("\n")),
))(s)?; ))(s)?;
let optdesc = opt(tuple(( let (s, optdesc) = opt(tuple((
position, position,
delimited(tag("#"), take_while1(|c| c != '#' && c != '\n'), tag("\n")), delimited(tag("#"), take_while1(|c| c != '#' && c != '\n'), tag("\n")),
)))(s)?; )))(s)?;
let description = optdesc.map(|(_pos, d)| String::from(d.trim()));
let mut desc: Option<&str> = None;
let s = match optdesc {
(s, Some((_pos, val))) => {
desc = Some(val.trim());
s
}
(s, None) => s,
};
let (s, tags) = parse_meta_tags(s)?; let (s, tags) = parse_meta_tags(s)?;
let meta = Metadata::new(title.trim(), desc).with_tags(tags); let meta = Metadata::new(title.trim(), description).with_tags(tags);
Ok((s, meta)) Ok((s, meta))
} }
@ -390,7 +382,7 @@ mod tests {
} }
#[test] #[test]
fn test_meta() { fn test_meta_success() {
let buf1 = "# Title.\n"; let buf1 = "# Title.\n";
let buf2 = "#Foo\n# Desc\n"; let buf2 = "#Foo\n# Desc\n";
let buf3 = "#Foo\n# Desc\n## tag1\n"; let buf3 = "#Foo\n# Desc\n## tag1\n";
@ -400,20 +392,57 @@ mod tests {
parse_meta(buf1.into()).unwrap().1, parse_meta(buf1.into()).unwrap().1,
Metadata::new("Title.", None::<String>) Metadata::new("Title.", None::<String>)
); );
assert_eq!( assert_eq!(
parse_meta(buf2.into()).unwrap().1, parse_meta(buf2.into()).unwrap().1,
Metadata::new("Foo", Some("Desc")) Metadata::new("Foo", Some("Desc"))
); );
assert_eq!( assert_eq!(
parse_meta(buf3.into()).unwrap().1, parse_meta(buf3.into()).unwrap().1,
Metadata::new("Foo", Some("Desc")).with_tags(vec![String::from("tag1")]) Metadata::new("Foo", Some("Desc")).with_tags(vec![String::from("tag1")])
); );
assert_eq!( assert_eq!(
parse_meta(buf4.into()).unwrap().1, parse_meta(buf4.into()).unwrap().1,
Metadata::new("Foo", None::<String>).with_tags(vec![String::from("tag1")]) Metadata::new("Foo", None::<String>).with_tags(vec![String::from("tag1")])
); );
} }
#[test]
fn test_meta_failure() {
let buf1 = "# Title.";
let buf2 = "// Foo Title\n# Desc\n";
let buf3 = "##Foo\n# Desc\n## tag1\n";
// Missing newline
match parse_meta(buf1.into()) {
Err(nom::Err::Error(nom::error::Error { input, .. })) => {
assert_eq!(input.location_offset(), 8);
assert_eq!(input.location_line(), 1);
}
err => panic!("Expected Err, found: {err:?}"),
}
// Invalid starting tag (// not #)
match parse_meta(buf2.into()) {
Err(nom::Err::Error(nom::error::Error { input, .. })) => {
assert_eq!(input.location_offset(), 0);
assert_eq!(input.location_line(), 1);
}
err => panic!("Expected Err, found: {err:?}"),
}
// Only taga, missing title
match parse_meta(buf3.into()) {
Err(nom::Err::Error(nom::error::Error { input, .. })) => {
assert_eq!(input.location_offset(), 1);
assert_eq!(input.location_line(), 1);
}
err => panic!("Expected Err, found: {err:?}"),
}
}
#[test] #[test]
fn test_client() { fn test_client() {
let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n"; let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n";