//! ICML: one story and what it uses, as one XML document.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use craft_idml::icml::{MAX_ICML_BYTES, icml_xml, is_icml, read_icml};
use craft_idml::{El, Error};
const ICML: &str = r#"
Hello
Anchored
"#;
#[test]
fn an_icml_file_reads_as_its_resources_and_stories() {
assert!(is_icml(ICML.as_bytes()));
assert!(is_icml(format!("\u{FEFF} {ICML}").as_bytes()));
assert!(!is_icml(b""));
assert!(!is_icml(b"SnippetType=\"InCopyInterchange\""));
let icml = read_icml(ICML.as_bytes()).unwrap();
assert_eq!(icml.top.iter().map(|e| e.name.as_str()).collect::>(), ["Color", "RootParagraphStyleGroup", "Story", "Story", "Hyperlink"]);
assert_eq!(icml.warnings, ["Resources/Graphic.xml: an ICML file has no package parts; it was not read"]);
let stories = icml.stories();
assert_eq!(stories.iter().map(|s| s.text(|_| None)).collect::>(), ["Hello", "Anchored"]);
}
#[test]
fn an_icml_file_written_reads_back() {
let icml = read_icml(ICML.as_bytes()).unwrap();
let mut root = El::new("Document").with_attr("DOMVersion", craft_idml::DOM_VERSION).with_attr("Self", "d");
root.children.extend(icml.top.iter().cloned().map(craft_idml::Node::El));
let bytes = icml_xml(&root);
let text = String::from_utf8(bytes.clone()).unwrap();
assert!(text.starts_with("\n\n\n"), Err(Error::NotIdml(m)) if m.contains("")));
assert!(matches!(read_icml(b""), Err(Error::NotIdml(m)) if m.contains("no story")));
assert!(matches!(read_icml(b""), Err(Error::Part { .. })), "cut off");
let huge = vec![b' '; MAX_ICML_BYTES + 1];
assert_eq!(read_icml(&huge), Err(Error::TooLarge { part: "ICML".into(), limit: MAX_ICML_BYTES as u64 }));
}