use pith_digest::Error;
use pith_zip::{XmlEvent, XmlReader};
fn first_error(input: &str) -> Error {
let mut reader = XmlReader::new(input);
loop {
match reader.next() {
Some(Ok(XmlEvent::Text(_))) | Some(Ok(_)) => continue,
Some(Err(e)) => return e,
None => panic!("input {input:?} produced no error"),
}
}
}
#[test]
fn unterminated_cdata_is_truncated() {
assert_eq!(
first_error("<a><![CDATA[never ends"),
Error::truncated("xml cdata", 13, 10)
);
}
#[test]
fn truncated_end_tag_is_truncated() {
assert_eq!(first_error("<a></a"), Error::truncated("xml end tag", 1, 0));
}
#[test]
fn truncated_self_closing_tag_is_truncated() {
assert_eq!(first_error("<a/"), Error::truncated("xml tag", 1, 0));
}
#[test]
fn junk_after_self_closing_slash_is_bad_value() {
assert_eq!(first_error("<a/b"), Error::BadValue("xml self-closing tag"));
}
#[test]
fn attribute_without_equals_is_bad_value() {
assert_eq!(first_error("<a b x/>"), Error::BadValue("xml attribute"));
}
#[test]
fn attribute_cut_before_equals_is_truncated() {
assert_eq!(first_error("<a b"), Error::truncated("xml attribute", 1, 0));
}
#[test]
fn unquoted_attribute_value_is_bad_value() {
assert_eq!(
first_error("<a b=c/>"),
Error::BadValue("xml attribute quote")
);
}
#[test]
fn unterminated_attribute_value_is_truncated() {
assert_eq!(
first_error("<a b=\"unclosed/>"),
Error::truncated("xml attribute value", 11, 10)
);
}
#[test]
fn less_than_in_attribute_value_is_bad_value() {
assert_eq!(
first_error("<a b=\"<"),
Error::BadValue("xml '<' in attribute value")
);
}
#[test]
fn whitespace_entity_body_is_bad_value() {
assert_eq!(first_error("<a>& ;</a>"), Error::BadValue("xml entity"));
}
#[test]
fn empty_name_position_is_truncated() {
assert_eq!(
first_error("<a></"),
Error::truncated("xml end tag name", 1, 0)
);
}
#[test]
fn non_name_character_is_bad_value() {
assert_eq!(first_error("< </a>"), Error::BadValue("xml tag name"));
}
#[test]
fn unterminated_comment_is_truncated() {
assert_eq!(
first_error("<a><!-- forever"),
Error::truncated("xml comment", 11, 8)
);
}
#[test]
fn unterminated_processing_instruction_is_truncated() {
assert_eq!(
first_error("<?xml version=\"1.0\""),
Error::truncated("xml processing instruction", 19, 17)
);
}
#[test]
fn unterminated_declaration_is_truncated() {
assert_eq!(
first_error("<!DOCTYPE note ["),
Error::truncated("xml declaration", 15, 14)
);
}