oxedyne/fe2o3/fe2o3_text/tests/csl.rs
7.6 KiB, 1 run
created by r1870400018:72724, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_text::{ |
| 2 | csl::{ |
| 3 | Locale, |
| 4 | Locales, |
| 5 | Style, |
| 6 | style::{ |
| 7 | Elem, |
| 8 | TermForm, |
| 9 | walk, |
| 10 | }, |
| 11 | }, |
| 12 | xml::{ |
| 13 | Elem as XElem, |
| 14 | Xml, |
| 15 | }, |
| 16 | }; |
| 17 | |
| 18 | use oxedyne_fe2o3_core::prelude::*; |
| 19 | |
| 20 | use std::path::Path; |
| 21 | |
| 22 | |
| 23 | const RENDERING: &[&str] = &["text", "number", "label", "names", "date", "group", "choose"]; |
| 24 | |
| 25 | /// Counts the rendering elements under an XML element, by name, descending through everything |
| 26 | /// except an in-style `locale` and `info`. |
| 27 | fn xml_count(e: &XElem, name: &str) -> usize { |
| 28 | let mut n = 0; |
| 29 | for k in e.elems() { |
| 30 | let l = k.name.local(); |
| 31 | if l == "locale" || l == "info" { |
| 32 | continue; |
| 33 | } |
| 34 | if l == name { |
| 35 | n += 1; |
| 36 | } |
| 37 | n += xml_count(k, name); |
| 38 | } |
| 39 | n |
| 40 | } |
| 41 | |
| 42 | fn model_count(style: &Style, name: &str) -> usize { |
| 43 | let mut lists: Vec<&[Elem]> = style.macros.values().map(|v| v.as_slice()).collect(); |
| 44 | lists.push(&style.citation.layout.elems); |
| 45 | if let Some(b) = &style.bibliography { |
| 46 | lists.push(&b.layout.elems); |
| 47 | } |
| 48 | let mut n = 0; |
| 49 | for l in lists { |
| 50 | walk(l, &mut |e| { |
| 51 | let hit = match e { |
| 52 | Elem::Text(_) => "text", |
| 53 | Elem::Number(_) => "number", |
| 54 | Elem::Label(_) => "label", |
| 55 | Elem::Names(_) => "names", |
| 56 | Elem::Date(_) => "date", |
| 57 | Elem::Group(_) => "group", |
| 58 | Elem::Choose(_) => "choose", |
| 59 | }; |
| 60 | if hit == name { |
| 61 | n += 1; |
| 62 | } |
| 63 | // A label inside names is held by the names, not as an element of a list. |
| 64 | if name == "label" { |
| 65 | if let Elem::Names(ns) = e { |
| 66 | if ns.label.is_some() { |
| 67 | n += 1; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | }); |
| 72 | } |
| 73 | n |
| 74 | } |
| 75 | |
| 76 | /// Parses a style and checks that the model holds every macro and every rendering element the XML |
| 77 | /// does. |
| 78 | fn round_trip(path: &Path) -> Outcome<Option<Style>> { |
| 79 | let src = res!(std::fs::read_to_string(path)); |
| 80 | if src.contains("rel=\"independent-parent\"") { |
| 81 | // A dependent style is refused by name; the caller loads its parent. |
| 82 | return match Style::parse(&src) { |
| 83 | Err(e) if format!("{}", e).contains("dependent style") => Ok(None), |
| 84 | _ => Err(err!("{}: a dependent style was not refused as one.", path.display(); Test, Mismatch)), |
| 85 | }; |
| 86 | } |
| 87 | let style = match Style::parse(&src) { |
| 88 | Ok(s) => s, |
| 89 | Err(e) => return Err(err!("{}: {}", path.display(), e; Test, Invalid)), |
| 90 | }; |
| 91 | let xml = res!(Xml::parse(&src)); |
| 92 | let root = res!(xml.root()); |
| 93 | let macros = root.children("macro").len(); |
| 94 | if style.macros.len() != macros { |
| 95 | return Err(err!("{}: {} macros in the XML, {} in the model.", path.display(), macros, style.macros.len(); |
| 96 | Test, Mismatch)); |
| 97 | } |
| 98 | for name in RENDERING { |
| 99 | let (x, m) = (xml_count(root, name), model_count(&style, name)); |
| 100 | if x != m { |
| 101 | return Err(err!("{}: {} <{}> in the XML, {} in the model.", path.display(), x, name, m; |
| 102 | Test, Mismatch)); |
| 103 | } |
| 104 | } |
| 105 | Ok(Some(style)) |
| 106 | } |
| 107 | |
| 108 | fn csl_files(dir: &str) -> Outcome<Vec<std::path::PathBuf>> { |
| 109 | let mut out = Vec::new(); |
| 110 | for e in res!(std::fs::read_dir(dir)) { |
| 111 | let p = res!(e).path(); |
| 112 | if p.extension().map(|x| x == "csl").unwrap_or(false) { |
| 113 | out.push(p); |
| 114 | } |
| 115 | } |
| 116 | out.sort(); |
| 117 | Ok(out) |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn csl_corpus_styles_parse_and_round_trip() -> Outcome<()> { |
| 122 | let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data/csl/corpus"); |
| 123 | let files = res!(csl_files(dir)); |
| 124 | if files.len() < 60 { |
| 125 | return Err(err!("Only {} corpus styles found in {}.", files.len(), dir; Test, Missing)); |
| 126 | } |
| 127 | let mut bad = Vec::new(); |
| 128 | for f in &files { |
| 129 | if let Err(e) = round_trip(f) { |
| 130 | bad.push(format!("{}", e)); |
| 131 | } |
| 132 | } |
| 133 | match bad.is_empty() { |
| 134 | true => Ok(()), |
| 135 | false => Err(err!("{} of {} styles fail:\n{}", bad.len(), files.len(), bad.join("\n"); Test, Mismatch)), |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | const EN_US: &str = r#"<?xml version="1.0"?> |
| 140 | <locale xmlns="http://purl.org/net/xbiblio/csl" version="1.0" xml:lang="en-US"> |
| 141 | <style-options punctuation-in-quote="true"/> |
| 142 | <date form="numeric"><date-part name="month" form="numeric" suffix="/"/><date-part name="year"/></date> |
| 143 | <terms> |
| 144 | <term name="and">and</term> |
| 145 | <term name="editor"><single>editor</single><multiple>editors</multiple></term> |
| 146 | <term name="editor" form="short"><single>ed.</single><multiple>eds.</multiple></term> |
| 147 | <term name="page" form="short"><single>p.</single><multiple>pp.</multiple></term> |
| 148 | <term name="accessed">accessed</term> |
| 149 | </terms> |
| 150 | </locale>"#; |
| 151 | |
| 152 | const DE_DE: &str = r#"<?xml version="1.0"?> |
| 153 | <locale xmlns="http://purl.org/net/xbiblio/csl" version="1.0" xml:lang="de-DE"> |
| 154 | <style-options punctuation-in-quote="false"/> |
| 155 | <terms> |
| 156 | <term name="and">und</term> |
| 157 | <term name="page" form="short"><single>S.</single><multiple>S.</multiple></term> |
| 158 | </terms> |
| 159 | </locale>"#; |
| 160 | |
| 161 | const STYLE: &str = r#"<?xml version="1.0"?> |
| 162 | <style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0"> |
| 163 | <info><id>urn:test</id><title>Test</title><updated>2026-10-12T00:00:00+00:00</updated></info> |
| 164 | <locale xml:lang="de"><terms><term name="accessed">abgerufen</term></terms></locale> |
| 165 | <locale xml:lang="de-AT"><terms><term name="and">sowie</term></terms></locale> |
| 166 | <locale><terms><term name="page" form="short">Seite</term></terms></locale> |
| 167 | <citation><layout><text variable="title"/></layout></citation> |
| 168 | </style>"#; |
| 169 | |
| 170 | fn locales() -> Outcome<Locales> { |
| 171 | let mut ls = Locales::new(); |
| 172 | res!(ls.insert(res!(Locale::parse(EN_US)))); |
| 173 | res!(ls.insert(res!(Locale::parse(DE_DE)))); |
| 174 | Ok(ls) |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn csl_locale_language_alone_reaches_its_primary_dialect() -> Outcome<()> { |
| 179 | let ls = res!(locales()); |
| 180 | let style = res!(Style::parse(STYLE)); |
| 181 | let chain = ls.chain(&style, Some("de")); |
| 182 | // In-style `de`, then in-style without a language, then de-DE by its primary dialect, then en-US. |
| 183 | assert_eq!(chain.term_text("and", TermForm::Long, false), Some("und")); |
| 184 | assert_eq!(chain.term_text("accessed", TermForm::Long, false), Some("abgerufen")); |
| 185 | assert_eq!(chain.term_text("page", TermForm::Short, false), Some("Seite")); |
| 186 | assert_eq!(chain.term_text("editor", TermForm::Long, true), Some("editors")); |
| 187 | assert!(!chain.punctuation_in_quote()); |
| 188 | assert_eq!(Locales::wanted(&style, Some("de")), vec!["de".to_string(), "de-DE".into(), "en-US".into()]); |
| 189 | Ok(()) |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn csl_locale_in_style_dialect_comes_first() -> Outcome<()> { |
| 194 | let ls = res!(locales()); |
| 195 | let style = res!(Style::parse(STYLE)); |
| 196 | let chain = ls.chain(&style, Some("de-AT")); |
| 197 | assert_eq!(chain.term_text("and", TermForm::Long, false), Some("sowie")); |
| 198 | assert_eq!(chain.term_text("accessed", TermForm::Long, false), Some("abgerufen")); |
| 199 | // en-US has the date format the German files lack. |
| 200 | assert!(chain.date(oxedyne_fe2o3_text::csl::style::DateForm::Numeric).is_some()); |
| 201 | let en = ls.chain(&style, None); |
| 202 | assert_eq!(en.code, "en-US"); |
| 203 | // Only the language-free in-style locale applies to English. |
| 204 | assert_eq!(en.term_text("page", TermForm::Short, false), Some("Seite")); |
| 205 | assert_eq!(en.term_text("accessed", TermForm::Long, false), Some("accessed")); |
| 206 | assert!(en.punctuation_in_quote()); |
| 207 | Ok(()) |
| 208 | } |
| 209 | |
| 210 | #[test] |
| 211 | fn csl_term_forms_fall_back_after_the_whole_chain() -> Outcome<()> { |
| 212 | let ls = res!(locales()); |
| 213 | let style = res!(Style::parse(STYLE)); |
| 214 | let chain = ls.chain(&style, Some("en-US")); |
| 215 | assert_eq!(chain.term_text("editor", TermForm::VerbShort, false), Some("editor")); |
| 216 | assert_eq!(chain.term_text("editor", TermForm::Symbol, true), Some("eds.")); |
| 217 | assert_eq!(chain.term_text("nothing", TermForm::Long, false), None); |
| 218 | Ok(()) |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn csl_refuses_a_dependent_style_by_name() -> Outcome<()> { |
| 223 | let dep = r#"<style xmlns="http://purl.org/net/xbiblio/csl" class="in-text" version="1.0"> |
| 224 | <info><id>urn:dep</id><link href="http://www.zotero.org/styles/apa" rel="independent-parent"/></info> |
| 225 | </style>"#; |
| 226 | match Style::parse(dep) { |
| 227 | Ok(_) => Err(err!("A dependent style parsed."; Test, Unexpected)), |
| 228 | Err(e) => match format!("{}", e).contains("http://www.zotero.org/styles/apa") { |
| 229 | true => Ok(()), |
| 230 | false => Err(err!("The error does not name the parent: {}", e; Test, Mismatch)), |
| 231 | }, |
| 232 | } |
| 233 | } |