Cotton code
79 lines · 4.1 KB · RustRaw
use super::*;use serde_json::Value;use std::path::Path;fn vectors_dir() -> std::path::PathBuf {    Path::new(&std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR")).join("vectors")}/// RULE: every host reads a package document the same way. Each vector in/// vectors/opf.json names an OPF in vectors/opf/ and what the rule answers/// for it (`book.opf`: every fact and the book columns they fill); the/// Worker's and the fetcher's tests read the same file. With/// COTTON_WRITE_OPF_VECTORS=1 the answers are written instead (review them).#[test]fn every_vector_reads_as_it_says() {    let path = vectors_dir().join("opf.json");    let mut vectors: Vec<Value> = serde_json::from_str(&std::fs::read_to_string(&path).expect("vectors/opf.json")).expect("JSON");    let write = std::env::var("COTTON_WRITE_OPF_VECTORS").is_ok();    for vector in &mut vectors {        let file = vector["file"].as_str().expect("file").to_string();        let opf = std::fs::read_to_string(vectors_dir().join("opf").join(&file)).expect("the OPF");        let answer = crate::boundary::call("book.opf", &serde_json::json!({ "opf": opf }).to_string()).expect("an answer");        if write {            vector["answer"] = answer;        } else {            assert_eq!(answer, vector["answer"], "{file}");        }    }    if write {        std::fs::write(&path, serde_json::to_string_pretty(&vectors).expect("JSON") + "\n").expect("written");    }}/// RULE: the container names the package; a container naming none is none.#[test]fn the_container_names_the_package() {    let container = br#"<?xml version="1.0"?><container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">  <rootfiles><rootfile full-path=" OEBPS/content.opf " media-type="application/oebps-package+xml"/></rootfiles></container>"#;    assert_eq!(container_rootfile(container).as_deref(), Some("OEBPS/content.opf"));    assert_eq!(container_rootfile(b"<container><rootfiles/></container>"), None);    assert_eq!(crate::boundary::call("book.container", r#"{"xml":"<container/>"}"#), Ok(serde_json::json!({ "opf_path": null })));}/// RULE: text that is no package says nothing, and is never refused.#[test]fn what_is_no_package_says_nothing() {    assert_eq!(read(b"not xml <at all"), OpfFacts::default());    assert_eq!(read(b"<package/>").columns(), serde_json::Map::new());    assert!(crate::boundary::call("book.opf", r#"{"opf":7}"#).unwrap_err().starts_with("book.opf: the input does not read"));}/// RULE: identifiers are read by what they are: Calibre's unknown date is no/// year, a UUID is no DOI, no ISBN and no Open Library id, and the bare/// forms read.#[test]fn identifiers_are_read_by_what_they_are() {    assert_eq!(doi_in("10.1000/182").as_deref(), Some("10.1000/182"));    assert_eq!(doi_in("https://doi.org/10.1000/ABC.1").as_deref(), Some("10.1000/ABC.1"));    assert_eq!(doi_in("urn:uuid:10.1000/x"), None);    assert_eq!(doi_in("10.12/short"), None, "a registrant is four digits at least");    assert_eq!(open_library_in("OL7353617M"), Some(OpenLibrary::Edition("OL7353617M".into())));    assert_eq!(open_library_in("/works/OL45883W"), Some(OpenLibrary::Work("OL45883W".into())));    assert_eq!(open_library_in("OLM"), None);    assert_eq!(open_library_in("urn:uuid:5f9c0a7e"), None);    assert_eq!(isbn_in("urn:uuid:1234567890", false), None, "ten digits nothing calls an ISBN");    let calibre_unknown = read(br#"<package><metadata><dc:date xmlns:dc="d">0101-01-01T00:00:00+00:00</dc:date></metadata></package>"#);    assert_eq!(calibre_unknown.year, None);}/// RULE: a reference in a title reads as what it stands for (quick-xml hands/// it over as its own event), and one XML does not know stays as written.#[test]fn references_in_the_metadata_read_as_their_characters() {    let facts = read(br#"<package><metadata xmlns:dc="d"><dc:title>Pride &amp; Prejudice &#8212; Vol&#xE9; I</dc:title><dc:publisher>Smith&nbsp;&amp; Sons</dc:publisher></metadata></package>"#);    assert_eq!(facts.title.as_deref(), Some("Pride & Prejudice \u{2014} Vol\u{e9} I"));    assert_eq!(facts.publisher.as_deref(), Some("Smith&nbsp;& Sons"));}