oxedyne/fe2o3/fe2o3_text/tests/csl_render.rs
19.4 KiB, 73 runs
created by r1870400018:73179, 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 | //! B2a foundations: the case folder, numbers and the output tree. Renderer tests against Typst |
| 2 | //! 0.15.1 follow in tests/data/csl/expect. |
| 3 | use oxedyne_fe2o3_text::csl::{ |
| 4 | case::Case, |
| 5 | item::{ |
| 6 | ChunkKind, |
| 7 | Chunked, |
| 8 | NumVal, |
| 9 | Numeric, |
| 10 | }, |
| 11 | node::{ |
| 12 | self, |
| 13 | Formatting, |
| 14 | Node, |
| 15 | }, |
| 16 | }; |
| 17 | |
| 18 | #[test] |
| 19 | fn test_title_case_as_typst() { |
| 20 | // From Typst 0.15.1 with chicago-author-date (tests/data/csl/expect/chicago-author-date.html). |
| 21 | assert_eq!(Case::Title.transform("Example Site: the home page"), "Example Site: The Home Page"); |
| 22 | assert_eq!(Case::Title.transform("on the art of war"), "On the Art of War"); |
| 23 | assert_eq!(Case::Title.transform("an NASA report"), "An NASA Report"); |
| 24 | assert_eq!(Case::Sentence.transform("The Study of Paris"), "The study of Paris"); |
| 25 | assert_eq!(Case::FirstUpper.transform("hello world"), "Hello world"); |
| 26 | assert_eq!(Case::AllUpper.transform("hello world"), "Hello World"); |
| 27 | assert_eq!(Case::Uppercase.transform("abc"), "ABC"); |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn test_numeric_parse() { |
| 32 | assert_eq!(Numeric::parse("12").map(|n| n.to_string()), Some("12".to_string())); |
| 33 | assert_eq!(Numeric::parse("12-15").map(|n| n.to_string()), Some("12–15".to_string())); |
| 34 | assert_eq!(Numeric::parse("2nd").map(|n| (n.will_transform(), n.to_string())), Some((false, "2nd".to_string()))); |
| 35 | assert_eq!(Numeric::parse("1, 3 & 5").map(|n| n.is_plural(false)), Some(true)); |
| 36 | assert!(Numeric::parse("twelve").is_none()); |
| 37 | assert!(matches!(NumVal::from_str("Supplement A"), NumVal::Str(_))); |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn test_chunked_and_tree() { |
| 42 | let mut c = Chunked::normal("A Study of "); |
| 43 | c.push(ChunkKind::Verbatim, "DNA"); |
| 44 | c.push(ChunkKind::Math, "x^2"); |
| 45 | assert_eq!(c.to_string(), "A Study of DNA$x^2$"); |
| 46 | let f = Formatting::default(); |
| 47 | let tree = node::simplify(vec![ |
| 48 | Node::Text("a".into(), f), |
| 49 | Node::Text("b".into(), f), |
| 50 | Node::Elem(node::Elem { children: vec![Node::Text("c".into(), f)], display: None, meta: None }), |
| 51 | ]); |
| 52 | assert_eq!(tree.len(), 2); |
| 53 | assert_eq!(node::plain(&tree), "abc"); |
| 54 | } |
| 55 | |
| 56 | // Renderers against Typst 0.15.1 (tests/data/csl/expect). |
| 57 | #[cfg(feature = "csl-archive")] |
| 58 | mod typst { |
| 59 | use oxedyne_fe2o3_core::prelude::*; |
| 60 | use oxedyne_fe2o3_text::csl::{ |
| 61 | archive, |
| 62 | Citation, |
| 63 | CiteForm, |
| 64 | CiteRef, |
| 65 | CiteState, |
| 66 | item::{ |
| 67 | ChunkKind, |
| 68 | Chunked, |
| 69 | DateVal, |
| 70 | Item, |
| 71 | NumVal, |
| 72 | }, |
| 73 | node::{ |
| 74 | Meta, |
| 75 | Node, |
| 76 | }, |
| 77 | page::PageVal, |
| 78 | person::Person, |
| 79 | render_bibliography, |
| 80 | style::{ |
| 81 | Display, |
| 82 | FontStyle, |
| 83 | FontWeight, |
| 84 | VAlign, |
| 85 | VarForm, |
| 86 | }, |
| 87 | }; |
| 88 | |
| 89 | /// A `refs.bib` entry as hayagriva hands it to CSL. |
| 90 | #[derive(Default)] |
| 91 | struct Fx { |
| 92 | key: &'static str, |
| 93 | ty: &'static str, |
| 94 | title: Chunked, |
| 95 | container: Option<&'static str>, |
| 96 | volume: Option<&'static str>, |
| 97 | issue: Option<&'static str>, |
| 98 | edition: Option<&'static str>, |
| 99 | publisher: Option<&'static str>, |
| 100 | doi: Option<&'static str>, |
| 101 | url: Option<&'static str>, |
| 102 | vol_title: Option<&'static str>, |
| 103 | also: Option<&'static str>, // a second CSL type the entry matches |
| 104 | author: Vec<Person>, |
| 105 | editor: Vec<Person>, |
| 106 | issued: Option<DateVal>, |
| 107 | page: Option<&'static str>, |
| 108 | } |
| 109 | |
| 110 | impl Item for Fx { |
| 111 | fn text(&self, var: &str, _form: VarForm) -> Option<Chunked> { |
| 112 | match var { |
| 113 | "title" => Some(self.title.clone()), |
| 114 | "container-title" => self.container.map(Chunked::normal), |
| 115 | "volume-title" => self.vol_title.map(Chunked::normal), |
| 116 | "publisher" => self.publisher.map(Chunked::normal), |
| 117 | "DOI" => self.doi.map(Chunked::normal), |
| 118 | "URL" => self.url.map(Chunked::normal), |
| 119 | _ => None, |
| 120 | } |
| 121 | } |
| 122 | fn number(&self, var: &str) -> Option<String> { |
| 123 | match var { |
| 124 | "volume" => self.volume, |
| 125 | "issue" => self.issue, |
| 126 | "edition" => self.edition, |
| 127 | _ => None, |
| 128 | }.map(str::to_string) |
| 129 | } |
| 130 | fn csl_type(&self) -> &str { |
| 131 | self.ty |
| 132 | } |
| 133 | fn is_type(&self, t: &str) -> bool { |
| 134 | self.ty == t || self.also == Some(t) |
| 135 | } |
| 136 | fn page(&self) -> Option<PageVal> { |
| 137 | self.page.map(PageVal::from_str) |
| 138 | } |
| 139 | fn names(&self, var: &str) -> &[Person] { |
| 140 | match var { |
| 141 | "author" => &self.author, |
| 142 | "editor" => &self.editor, |
| 143 | _ => &[], |
| 144 | } |
| 145 | } |
| 146 | fn date(&self, var: &str) -> Option<DateVal> { |
| 147 | match var { |
| 148 | "issued" => self.issued, |
| 149 | _ => None, |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | fn p(family: &str, given: &str) -> Person { |
| 155 | Person::new(family, Some(given)) |
| 156 | } |
| 157 | |
| 158 | fn ymd(year: i32, month: Option<u8>, day: Option<u8>) -> Option<DateVal> { |
| 159 | Some(DateVal { year, month, day, ..DateVal::default() }) |
| 160 | } |
| 161 | |
| 162 | fn chunks(parts: &[(ChunkKind, &str)]) -> Chunked { |
| 163 | let mut c = Chunked::default(); |
| 164 | for (k, s) in parts { |
| 165 | c.push(*k, s); |
| 166 | } |
| 167 | c |
| 168 | } |
| 169 | |
| 170 | fn fixtures() -> Vec<Fx> { |
| 171 | vec![ |
| 172 | Fx { |
| 173 | key: "a1", ty: "article-journal", |
| 174 | title: chunks(&[(ChunkKind::Normal, "A Study of "), (ChunkKind::Verbatim, "DNA"), (ChunkKind::Normal, " Repair in the Lab")]), |
| 175 | container: Some("Journal of Testing"), volume: Some("12"), issue: Some("3"), doi: Some("10.1000/xyz123"), |
| 176 | ..Fx::default() |
| 177 | }, |
| 178 | Fx { |
| 179 | key: "b1", ty: "book", title: Chunked::normal("The Art of Testing"), |
| 180 | publisher: Some("Acme Press"), edition: Some("2"), |
| 181 | ..Fx::default() |
| 182 | }, |
| 183 | Fx { |
| 184 | key: "w1", ty: "webpage", title: Chunked::normal("Example Site: the home page"), |
| 185 | url: Some("https://example.com/page"), |
| 186 | ..Fx::default() |
| 187 | }, |
| 188 | Fx { |
| 189 | key: "a2", ty: "article-journal", |
| 190 | // biblatex 0.12 reads `On {$x^2$} and ...` with an empty verbatim chunk either side of |
| 191 | // the math. The one after it leaves the folder mid-word, which keeps title case's "and". |
| 192 | title: chunks(&[ |
| 193 | (ChunkKind::Normal, "On "), |
| 194 | (ChunkKind::Verbatim, ""), |
| 195 | (ChunkKind::Math, "x^2"), |
| 196 | (ChunkKind::Verbatim, ""), |
| 197 | (ChunkKind::Normal, " and other things"), |
| 198 | ]), |
| 199 | container: Some("Annals of Maths"), volume: Some("7"), |
| 200 | ..Fx::default() |
| 201 | }, |
| 202 | // biblatex 0.12 reads "Doe, Jr., Jane" with the suffix, and hayagriva sets no comma |
| 203 | // before it (`comma_suffix: false`) for every BibTeX name. |
| 204 | Fx { |
| 205 | key: "n1", ty: "article-journal", title: Chunked::normal("Names in Order"), |
| 206 | container: Some("Journal of Names"), volume: Some("3"), page: Some("123-129"), |
| 207 | author: vec![ |
| 208 | p("Smith", "John"), |
| 209 | p("Beethoven", "Ludwig").with_particle("van"), |
| 210 | p("Doe", "Jane").with_suffix("Jr."), |
| 211 | ], |
| 212 | issued: Some(DateVal::year(2020)), |
| 213 | ..Fx::default() |
| 214 | }, |
| 215 | Fx { |
| 216 | key: "n2", ty: "book", title: Chunked::normal("Annual Report"), |
| 217 | publisher: Some("Acme Press"), |
| 218 | author: vec![Person::literal("Acme Corporation")], |
| 219 | issued: ymd(2018, Some(3), Some(15)), |
| 220 | ..Fx::default() |
| 221 | }, |
| 222 | Fx { |
| 223 | key: "n3", ty: "article-journal", title: Chunked::normal("Eight Authors"), |
| 224 | container: Some("Many Hands"), volume: Some("9"), issue: Some("2"), page: Some("1001-1012"), |
| 225 | author: vec![ |
| 226 | p("Alpha", "Ann"), p("Beta", "Bob"), p("Gamma", "Gus"), p("Delta", "Dan"), |
| 227 | p("Epsilon", "Eve"), p("Zeta", "Zed"), p("Eta", "Ed"), p("Theta", "Tom"), |
| 228 | ], |
| 229 | // `2019/2021` reaches hayagriva as its end date. |
| 230 | issued: Some(DateVal::year(2021)), |
| 231 | ..Fx::default() |
| 232 | }, |
| 233 | // An @inproceedings is an article whose parent is the proceedings, whose title hayagriva |
| 234 | // answers for both container-title and volume-title. |
| 235 | Fx { |
| 236 | key: "n4", ty: "paper-conference", title: Chunked::normal("Approximate Things"), |
| 237 | container: Some("Proceedings of Circa"), vol_title: Some("Proceedings of Circa"), |
| 238 | page: Some("45-47"), |
| 239 | author: vec![p("Müller", "Hans-Peter"), p("Cruz", "María").with_particle("de la")], |
| 240 | editor: vec![p("Editor", "Edith")], |
| 241 | issued: Some(DateVal { year: 2015, approximate: true, ..DateVal::default() }), |
| 242 | ..Fx::default() |
| 243 | }, |
| 244 | // A @misc with a URL matches both webpage and document in hayagriva. |
| 245 | Fx { |
| 246 | key: "n5", ty: "webpage", also: Some("document"), title: Chunked::normal("A Month Only"), |
| 247 | url: Some("https://example.org/m"), |
| 248 | author: vec![p("O'Neil", "Kate")], |
| 249 | issued: ymd(2021, Some(7), None), |
| 250 | ..Fx::default() |
| 251 | }, |
| 252 | Fx { |
| 253 | key: "n6", ty: "article-journal", title: Chunked::normal("Twenty-One Authors"), |
| 254 | container: Some("Crowd Science"), volume: Some("1"), page: Some("321-328"), |
| 255 | author: (1..=21).map(|i| { |
| 256 | let given = format!("A{}", (b'a' + (i as u8 - 1)) as char); |
| 257 | Person::new(&format!("A{:02}", i), Some(&given)) |
| 258 | }).collect(), |
| 259 | issued: Some(DateVal::year(2017)), |
| 260 | ..Fx::default() |
| 261 | }, |
| 262 | ] |
| 263 | } |
| 264 | |
| 265 | fn esc(s: &str) -> String { |
| 266 | s.replace('&', "&").replace('<', "<").replace('>', ">") |
| 267 | } |
| 268 | |
| 269 | fn wrap(s: String, f: &oxedyne_fe2o3_text::csl::node::Formatting) -> String { |
| 270 | let mut s = s; |
| 271 | if f.style != FontStyle::Normal { s = format!("<em>{}</em>", s); } |
| 272 | if f.weight == FontWeight::Bold { s = format!("<strong>{}</strong>", s); } |
| 273 | if f.valign == VAlign::Sup { s = format!("<sup>{}</sup>", s); } |
| 274 | s |
| 275 | } |
| 276 | |
| 277 | fn html(nodes: &[Node], out: &mut String) { |
| 278 | for n in nodes { |
| 279 | match n { |
| 280 | Node::Text(t, f) => out.push_str(&wrap(esc(t), f)), |
| 281 | Node::Math(m) => { out.push('$'); out.push_str(m); out.push('$'); } |
| 282 | Node::Link { text, fmt, url } => out.push_str(&wrap(format!("<a href=\"{}\">{}</a>", url, esc(text)), fmt)), |
| 283 | Node::Cite { .. } => out.push_str("p. 5"), // the only supplement used |
| 284 | Node::Elem(e) => { |
| 285 | let mut inner = String::new(); |
| 286 | html(&e.children, &mut inner); |
| 287 | match (e.display, &e.meta) { |
| 288 | (Some(Display::LeftMargin), _) => { out.push_str("<span class=\"prefix\">"); out.push_str(&inner); out.push_str("</span> "); } |
| 289 | (_, Some(Meta::Entry(_))) => { out.push_str("<a>"); out.push_str(&inner); out.push_str("</a>"); } |
| 290 | _ => out.push_str(&inner), |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /// The entry as Typst writes it: second-field-align's first field as the prefix column, and |
| 298 | /// the rest after one space. |
| 299 | fn entry_html(prefix: Option<Node>, body: Vec<Node>) -> String { |
| 300 | let mut out = String::new(); |
| 301 | if let Some(p) = prefix { |
| 302 | let mut inner = String::new(); |
| 303 | html(&[p], &mut inner); |
| 304 | out.push_str(&format!("<span class=\"prefix\">{}</span> ", inner)); |
| 305 | } |
| 306 | let mut rest = String::new(); |
| 307 | html(&body, &mut rest); |
| 308 | out.push_str(rest.trim_start()); |
| 309 | out.trim().to_string() |
| 310 | } |
| 311 | |
| 312 | /// Each `<li>` of an expected file, keyed by the entry it shows. |
| 313 | fn expected(style: &str) -> Outcome<Vec<String>> { |
| 314 | let path = format!("{}/tests/data/csl/expect/{}.html", env!("CARGO_MANIFEST_DIR"), style); |
| 315 | let src = res!(std::fs::read_to_string(&path)); |
| 316 | Ok(src.split("<li>").skip(1).map(|li| match li.find("</li>") { |
| 317 | Some(end) => li[..end].to_string(), |
| 318 | None => li.to_string(), |
| 319 | }).collect()) |
| 320 | } |
| 321 | |
| 322 | /// The whole bibliography, as Typst's `full: true` cites every entry: the count of entries |
| 323 | /// that match Typst's in the same place. |
| 324 | fn check(style_name: &str) -> Outcome<usize> { |
| 325 | let style = res!(res!(archive::by_name(style_name).ok_or_else(|| err!("No style {}.", style_name; Missing))).style()); |
| 326 | let files = res!(archive::locales_for(&style, Some("en"))); |
| 327 | let chain = files.chain(&style, Some("en")); |
| 328 | let want = res!(expected(style_name)); |
| 329 | let items = fixtures(); |
| 330 | let cites: Vec<Citation> = (0..items.len()).map(|i| Citation { |
| 331 | refs: vec![CiteRef { entry: i, hidden: true, ..CiteRef::default() }], |
| 332 | note: None, |
| 333 | }).collect(); |
| 334 | let state = CiteState::new(&style, &chain, Some("en"), &items, &cites); |
| 335 | let bib = render_bibliography(&style, &chain, Some("en"), &items, &state); |
| 336 | let mut matched = 0; |
| 337 | let mut report = String::new(); |
| 338 | for (i, (prefix, body)) in bib.into_iter().enumerate() { |
| 339 | let got = entry_html(prefix, body); |
| 340 | if want.get(i) == Some(&got) { |
| 341 | matched += 1; |
| 342 | } else { |
| 343 | report.push_str(&format!("\n{} #{} {}: got {}\n want {}\n", style_name, i, items[state.order()[i]].key, got, want.get(i).map_or("", |w| w.as_str()))); |
| 344 | } |
| 345 | } |
| 346 | if !report.is_empty() { |
| 347 | println!("{}", report); |
| 348 | } |
| 349 | Ok(matched) |
| 350 | } |
| 351 | |
| 352 | /// The citations of `cites.typ`, in order, over the fixtures' keys. |
| 353 | fn cites_typ(items: &[Fx]) -> Vec<Citation> { |
| 354 | let at = |k: &str| items.iter().position(|f| f.key == k).unwrap_or(usize::MAX); |
| 355 | let r = |k: &str| CiteRef { entry: at(k), ..CiteRef::default() }; |
| 356 | let loc = |k: &str| CiteRef { locator: Some(("custom".to_string(), NumVal::Transparent(0))), ..r(k) }; |
| 357 | let form = |k: &str, form: CiteForm| CiteRef { form, ..r(k) }; |
| 358 | let c = |refs: Vec<CiteRef>| Citation { refs, note: None }; |
| 359 | vec![ |
| 360 | c(vec![r("a1")]), |
| 361 | c(vec![r("n1")]), |
| 362 | c(vec![r("n1"), r("n2"), r("n3")]), |
| 363 | c(vec![r("n5"), r("n4")]), |
| 364 | c(vec![loc("n1")]), |
| 365 | c(vec![loc("n1")]), |
| 366 | c(vec![r("n1")]), |
| 367 | c(vec![r("n2"), r("n3"), r("a1")]), |
| 368 | c(vec![form("n1", CiteForm::Author)]), |
| 369 | c(vec![form("n1", CiteForm::Year)]), |
| 370 | c(vec![form("n1", CiteForm::Prose)]), |
| 371 | c(vec![form("n1", CiteForm::Full)]), |
| 372 | ] |
| 373 | } |
| 374 | |
| 375 | /// Typst's `<a ... role="doc-biblioref">` written as a bare `<a>`. |
| 376 | fn bare_links(s: &str) -> String { |
| 377 | let mut out = String::new(); |
| 378 | let mut rest = s; |
| 379 | while let Some(i) = rest.find("<a ") { |
| 380 | out.push_str(&rest[..i]); |
| 381 | let tail = &rest[i..]; |
| 382 | match (tail.find('>'), tail.find("role=\"doc-biblioref\">")) { |
| 383 | (Some(e), Some(r)) if r < e => { out.push_str("<a>"); rest = &tail[e + 1..]; } |
| 384 | (Some(e), _) => { out.push_str(&tail[..e + 1]); rest = &tail[e + 1..]; } |
| 385 | (None, _) => { out.push_str(tail); rest = ""; } |
| 386 | } |
| 387 | } |
| 388 | out.push_str(rest); |
| 389 | // A superscript delimiter carries the role too. |
| 390 | out.replace("<sup role=\"doc-biblioref\">", "<sup>") |
| 391 | } |
| 392 | |
| 393 | /// Each citation of `cites.typ` against Typst's `cites-<style>.html`: the indices that match. |
| 394 | fn check_cites(style_name: &str, items: Vec<Fx>, cites: Vec<Citation>, file: &str) -> Outcome<Vec<usize>> { |
| 395 | let style = res!(res!(archive::by_name(style_name).ok_or_else(|| err!("No style {}.", style_name; Missing))).style()); |
| 396 | let files = res!(archive::locales_for(&style, Some("en"))); |
| 397 | let chain = files.chain(&style, Some("en")); |
| 398 | let path = format!("{}/tests/data/csl/expect/{}", env!("CARGO_MANIFEST_DIR"), file); |
| 399 | let src = res!(std::fs::read_to_string(&path)); |
| 400 | let want: Vec<String> = src.lines().map(|l| bare_links(l.trim_start_matches("<li>").trim_end_matches("</li>"))).collect(); |
| 401 | let state = CiteState::new(&style, &chain, Some("en"), &items, &cites); |
| 402 | let mut ok = Vec::new(); |
| 403 | let mut report = String::new(); |
| 404 | for i in 0..cites.len() { |
| 405 | let mut got = String::new(); |
| 406 | html(state.citation(i), &mut got); |
| 407 | if want.get(i) == Some(&got) { |
| 408 | ok.push(i); |
| 409 | } else { |
| 410 | report.push_str(&format!("\n{} cite {}: got {}\n want {}\n", style_name, i, got, want.get(i).map_or("", |w| w.as_str()))); |
| 411 | } |
| 412 | } |
| 413 | if !report.is_empty() { |
| 414 | println!("{}", report); |
| 415 | } |
| 416 | Ok(ok) |
| 417 | } |
| 418 | |
| 419 | #[test] |
| 420 | fn test_ieee_citations_as_typst() -> Outcome<()> { |
| 421 | let items = fixtures(); |
| 422 | let cites = cites_typ(&items); |
| 423 | let ok = res!(check_cites("ieee", items, cites, "cites-ieee.html")); |
| 424 | assert_eq!(ok, (0..8).collect::<Vec<_>>()); |
| 425 | Ok(()) |
| 426 | } |
| 427 | |
| 428 | #[test] |
| 429 | fn test_apa_citations_as_typst() -> Outcome<()> { |
| 430 | let items = fixtures(); |
| 431 | let cites = cites_typ(&items); |
| 432 | let ok = res!(check_cites("apa", items, cites, "cites-apa.html")); |
| 433 | assert_eq!(ok, (0..8).collect::<Vec<_>>()); |
| 434 | Ok(()) |
| 435 | } |
| 436 | |
| 437 | /// `group.bib`: one author's three works, two in one year; a second Smith; two Browns et al. |
| 438 | fn group_fixtures() -> Vec<Fx> { |
| 439 | let art = |key, title, author: Vec<Person>, year| Fx { |
| 440 | key, ty: "article-journal", title: Chunked::normal(title), |
| 441 | container: Some("Journal of Pairs"), author, issued: Some(DateVal::year(year)), |
| 442 | ..Fx::default() |
| 443 | }; |
| 444 | let book = |key, title, author: Vec<Person>, year| Fx { |
| 445 | key, ty: "book", title: Chunked::normal(title), publisher: Some("Acme Press"), |
| 446 | author, issued: Some(DateVal::year(year)), |
| 447 | ..Fx::default() |
| 448 | }; |
| 449 | vec![ |
| 450 | art("d1", "First Paper", vec![p("Smith", "John")], 2020), |
| 451 | art("d2", "Second Paper", vec![p("Smith", "John")], 2020), |
| 452 | book("d3", "Earlier Book", vec![p("Smith", "John")], 2018), |
| 453 | art("d4", "Another Smith", vec![p("Smith", "Mary")], 2019), |
| 454 | art("d5", "Three Colours", vec![p("Brown", "Alice"), p("Green", "Bob"), p("White", "Carl")], 2021), |
| 455 | art("d6", "Three More Colours", vec![p("Brown", "Alice"), p("Black", "Dan"), p("Blue", "Eve")], 2021), |
| 456 | book("d7", "A Later Book", vec![p("Smith", "Mary")], 2018), |
| 457 | ] |
| 458 | } |
| 459 | |
| 460 | /// The citations of `group.typ`. |
| 461 | fn group_cites(items: &[Fx]) -> Vec<Citation> { |
| 462 | let at = |k: &str| items.iter().position(|f| f.key == k).unwrap_or(usize::MAX); |
| 463 | let c = |keys: &[&str]| Citation { |
| 464 | refs: keys.iter().map(|k| CiteRef { entry: at(k), ..CiteRef::default() }).collect(), |
| 465 | note: None, |
| 466 | }; |
| 467 | vec![ |
| 468 | c(&["d1", "d2", "d3"]), |
| 469 | c(&["d1", "d4", "d3", "d2"]), |
| 470 | c(&["d4"]), |
| 471 | c(&["d5"]), |
| 472 | c(&["d6"]), |
| 473 | c(&["d5", "d6", "d1"]), |
| 474 | c(&["d3", "d1", "d2", "d5", "d6"]), |
| 475 | c(&["d7"]), |
| 476 | c(&["d3", "d7"]), |
| 477 | ] |
| 478 | } |
| 479 | |
| 480 | fn check_group(style: &str) -> Outcome<Vec<usize>> { |
| 481 | let items = group_fixtures(); |
| 482 | let cites = group_cites(&items); |
| 483 | check_cites(style, items, cites, &format!("group-{}.html", style)) |
| 484 | } |
| 485 | |
| 486 | /// Citation-number collapsing: `1–3`, and the after-collapse delimiter. |
| 487 | #[test] |
| 488 | fn test_ama_collapse_as_typst() -> Outcome<()> { |
| 489 | assert_eq!(res!(check_group("american-medical-association")), (0..9).collect::<Vec<_>>()); |
| 490 | Ok(()) |
| 491 | } |
| 492 | |
| 493 | /// Grouping by author with year collapsing (apa), and year-suffix collapsing (ams). |
| 494 | #[test] |
| 495 | fn test_group_collapse_as_typst() -> Outcome<()> { |
| 496 | assert_eq!(res!(check_group("apa")), (0..9).collect::<Vec<_>>()); |
| 497 | assert_eq!(res!(check_group("american-meteorological-society")), (0..9).collect::<Vec<_>>()); |
| 498 | Ok(()) |
| 499 | } |
| 500 | |
| 501 | #[test] |
| 502 | fn test_ieee_as_typst() -> Outcome<()> { |
| 503 | assert_eq!(res!(check("ieee")), 10); |
| 504 | Ok(()) |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn test_apa_as_typst() -> Outcome<()> { |
| 509 | assert_eq!(res!(check("apa")), 10); |
| 510 | Ok(()) |
| 511 | } |
| 512 | |
| 513 | #[test] |
| 514 | fn test_american_medical_association_as_typst() -> Outcome<()> { |
| 515 | assert_eq!(res!(check("american-medical-association")), 10); |
| 516 | Ok(()) |
| 517 | } |
| 518 | |
| 519 | #[test] |
| 520 | fn test_chicago_author_date_as_typst() -> Outcome<()> { |
| 521 | assert_eq!(res!(check("chicago-author-date")), 10); |
| 522 | Ok(()) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // B2b types: page ranges and persons, as hayagriva and citationberg give them. |
| 527 | mod b2b_types { |
| 528 | use oxedyne_fe2o3_text::csl::{ |
| 529 | page::{ |
| 530 | format_range, |
| 531 | PageVal, |
| 532 | }, |
| 533 | person::Person, |
| 534 | style::PageRange, |
| 535 | }; |
| 536 | |
| 537 | fn fr(f: PageRange, a: &str, b: &str) -> String { |
| 538 | let mut s = String::new(); |
| 539 | format_range(&mut s, f, a, b, None); |
| 540 | s |
| 541 | } |
| 542 | |
| 543 | #[test] |
| 544 | fn test_page_range_formats() { |
| 545 | assert_eq!(fr(PageRange::Chicago16, "321", "328"), "321–28"); |
| 546 | assert_eq!(fr(PageRange::Chicago16, "1001", "1012"), "1001–12"); |
| 547 | assert_eq!(fr(PageRange::Chicago16, "123", "129"), "123–29"); |
| 548 | assert_eq!(fr(PageRange::Chicago16, "100", "104"), "100–104"); |
| 549 | assert_eq!(fr(PageRange::Chicago16, "101", "108"), "101–8"); |
| 550 | assert_eq!(fr(PageRange::Chicago15, "1496", "1504"), "1496–1504"); |
| 551 | assert_eq!(fr(PageRange::Expanded, "42", "5"), "42–45"); |
| 552 | assert_eq!(fr(PageRange::Minimal, "321", "328"), "321–8"); |
| 553 | assert_eq!(fr(PageRange::MinimalTwo, "321", "328"), "321–28"); |
| 554 | assert_eq!(fr(PageRange::Expanded, "S1", "S12"), "S1–S12"); |
| 555 | let mut s = String::new(); |
| 556 | if let PageVal::Typed(p) = PageVal::from_str("1-5, 7 & 9") { |
| 557 | assert!(p.is_plural()); |
| 558 | p.write(&mut s, None, None); |
| 559 | } |
| 560 | assert_eq!(s, "1–5, 7 & 9"); |
| 561 | assert!(!PageVal::from_str("12").is_plural()); |
| 562 | assert!(matches!(PageVal::from_str("i-x 3"), PageVal::Str(_))); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn test_person_parts() { |
| 567 | let mut s = String::new(); |
| 568 | let _ = Person::new("Müller", Some("Hans-Peter")).initials(&mut s, Some(". "), true); |
| 569 | assert_eq!(s, "H.-P."); |
| 570 | s.clear(); |
| 571 | let _ = Person::new("Müller", Some("Hans-Peter")).initials(&mut s, Some(""), false); |
| 572 | assert_eq!(s, "HP"); |
| 573 | let p = Person::new("van der Berg", Some("Anna")); |
| 574 | assert_eq!(p.particle_in_family(), Some("van der")); |
| 575 | assert_eq!(p.family_without_particle(), "Berg"); |
| 576 | assert!(Person::literal("Acme Corporation").is_institutional()); |
| 577 | assert_eq!(Person::literal("The Acme Group").family_without_article(), "Acme Group"); |
| 578 | } |
| 579 | } |