use rbook::xml::Find; use rbook::Ebook; #[test] fn metadata_test() { let epub = rbook::Epub::new("tests/ebooks/childrens-literature.epub").unwrap(); // epub specification required metadata let title = epub.metadata().title().unwrap(); let identifier = epub.metadata().unique_identifier().unwrap(); assert_eq!("title", title.name()); assert_eq!("Children's Literature", title.value()); assert_eq!("http://www.gutenberg.org/ebooks/25545", identifier.value()); // Not epub specification required metadata let creators1 = epub.metadata().creators(); // Alternate way of retrieval let creators2 = epub.metadata().get("creator"); // Namespace/prefix is not required assert_eq!(creators1, creators2); let creator1 = creators1.first().unwrap(); let creator2 = creators1.last().unwrap(); assert_eq!("Charles Madison Curry", creator1.value()); assert_eq!("Erle Elsworth Clippinger", creator2.value()); } #[test] fn metadat_test_alt() { let epub = rbook::Epub::new("tests/ebooks/childrens-literature.epub").unwrap(); // epub specification required metadata let title = epub.metadata().find_value("title").unwrap(); let identifier = epub.metadata().find_value("identifier").unwrap(); assert_eq!("Children's Literature", title); assert_eq!("http://www.gutenberg.org/ebooks/25545", identifier); // `get_value()` finds the first match // Find first `creator` element and return its value let creator1 = epub.metadata().find_value("creator").unwrap(); // Find first `creator` element that contains an `id` attribute let creator1_alt = epub.metadata().find_value("creator[id]").unwrap(); // Find first `creator` element that contains an `id` equal to a `curry` let creator1_alt2 = epub.metadata().find_value("creator[id=curry]").unwrap(); assert_eq!(creator1, creator1_alt); assert_eq!(creator1_alt, creator1_alt2); let creator2 = epub .metadata() .find_value("creator[id=clippinger]") .unwrap(); assert_eq!("Charles Madison Curry", creator1); assert_eq!("Erle Elsworth Clippinger", creator2); let creator1_role = epub .metadata() .find_value("creator[id=curry] > file-as") .unwrap(); let creator2_role = epub .metadata() .find_value("creator[id=clippinger] > file-as[refines=#clippinger]") .unwrap(); assert_eq!("Curry, Charles Madison", creator1_role); assert_eq!("Clippinger, Erle Elsworth", creator2_role); } #[test] fn metadata_test2() { let epub = rbook::Epub::new("tests/ebooks/moby-dick.epub").unwrap(); let creators = epub.metadata().creators(); let creator1 = creators.first().unwrap(); assert_eq!("Herman Melville", creator1.value()); // Refining (children) metadata and attributes let role = creator1.get_child("role").unwrap(); // Child metadata element let scheme = role.get_attribute("scheme").unwrap(); // Attribute of an element assert_eq!("aut", role.value()); assert_eq!("marc:relators", scheme) } #[test] fn manifest_test() { let epub = rbook::Epub::new("tests/ebooks/childrens-literature.epub").unwrap(); // Retrieve manifest element by id let element = epub.manifest().by_id("s04").unwrap(); let media_type = element.get_attribute("media-type").unwrap(); assert_eq!("s04", element.name()); // id attribute value assert_eq!("s04.xhtml", element.value()); // href attribute value assert_eq!("application/xhtml+xml", media_type); // Retrieve file content using href let content = epub.read_file(element.value()).unwrap(); assert!(content.starts_with("