Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocReferencesTests/EngineTests.swift3// Description: Citation engine behaviour that is not tied to one published example: numeric ordering and range collapsing, missing keys,4// French localisation, style catalogue, bibliography paragraphs, and the 5 000-entry performance measurement.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import Testing10import DocModel11@testable import DocReferences1213@Suite struct EngineTests {14 static func entries(_ n: Int) -> [BibEntry] {15 (1...n).map { BibEntry(key: "k\($0)", type: "article-journal", authors: [person("Author\($0)", "A.")], year: 2000 + $0,16 fields: ["title": "Title \($0)", "container-title": "Journal", "volume": "\($0)", "page": "1-9"]) }17 }1819 @Test func styleCatalogue() {20 #expect(CitationEngine.styles.map(\.id) == ["apa", "chicago-author-date", "mla", "ieee", "numeric", "vancouver"])21 #expect(CitationEngine(styleID: "no-such-style").styleID == "apa")22 #expect(CitationEngine(styleID: "IEEE").styleID == "ieee")23 #expect(CitationEngine(styleID: "mla").info.kind == .authorPage)24 var doc = DocumentModel()25 doc.settings.citationStyle = "vancouver"26 #expect(CitationEngine(document: doc).styleID == "vancouver")27 }2829 @Test func numericOrderAndRangeCollapsing() {30 // The library is stored in reverse order; numbers follow the order of first citation.31 let lib = Array(Self.entries(7).reversed())32 let cites: [[CitationItem]] = [[cite("k3")], [cite("k1"), cite("k5")], [cite("k2")], [cite("k7"), cite("k4")], [cite("k3")]]33 let ieee = Cited(style: "ieee", entries: lib, citations: cites)34 #expect(ieee.numbering.citedKeys == ["k3", "k1", "k5", "k2", "k7", "k4"])35 #expect(ieee.text(["k3"]) == "[1]")36 #expect(ieee.text(["k5", "k1"]) == "[2], [3]")37 #expect(ieee.text(["k2", "k3", "k1", "k5"]) == "[1]–[4]")38 #expect(ieee.text(["k4", "k3", "k1", "k5"]) == "[1]–[3], [6]")39 #expect(ieee.text(["k3", "k5", "k7", "k4"]) == "[1], [3], [5], [6]")40 #expect(ieee.text(cite("k1", "12")) == "[2, p. 12]")41 #expect(ieee.text(cite("k1", "pp. 12-14"), cite("k3", prefix: "see also")) == "[2, pp. 12–14], [see also 1]")42 let bib = ieee.bibliography43 #expect(bib.count == 6) // k6 is never cited44 #expect(bib[0] == "[1]\tA. Author3, “Title 3,” *Journal*, vol. 3, pp. 1–9, 2003.")45 #expect(bib[5].hasPrefix("[6]\tA. Author4,"))4647 let numeric = Cited(style: "numeric", entries: lib, citations: cites)48 #expect(numeric.text(["k4", "k3", "k1", "k5"]) == "[1–3, 6]")49 #expect(numeric.text(["k3", "k1"]) == "[1, 2]")50 #expect(numeric.text(cite("k1", "12"), cite("k3")) == "[2, p. 12; 1]")51 let vancouver = Cited(style: "vancouver", entries: lib, citations: cites)52 #expect(vancouver.text(["k4", "k3", "k1", "k5"]) == "(1–3,6)")53 #expect(vancouver.text(["k7", "k4"]) == "(5,6)")54 #expect(vancouver.bibliography[0] == "1.\tAuthor3 A. Title 3. Journal. 2003;3:1-9.")55 }5657 @Test func missingKeysAreVisibleAndNeverFabricated() {58 let lib = Self.entries(2)59 let cites: [[CitationItem]] = [[cite("ghost")], [cite("k1"), cite("phantom")], [cite("k2")]]60 for style in ["apa", "chicago-author-date", "mla"] {61 let c = Cited(style: style, entries: lib, citations: cites)62 #expect(c.text(["ghost"]) == "(?ghost)")63 #expect(c.text(["k1", "phantom"]).hasSuffix("; ?phantom)"))64 #expect(c.bibliography.count == 2)65 }66 let ieee = Cited(style: "ieee", entries: lib, citations: cites)67 #expect(ieee.text(["ghost"]) == "[?ghost]")68 #expect(ieee.text(["k1", "phantom"]) == "[1], [?phantom]")69 #expect(ieee.text(["k2"]) == "[2]") // numbers skip the missing works, so they match the reference list70 #expect(ieee.bibliography.map { String($0.prefix(3)) } == ["[1]", "[2]"])71 #expect(Cited(style: "numeric", entries: lib, citations: cites).text(["k1", "phantom", "k2"]) == "[1, 2, ?phantom]")72 #expect(Cited(style: "vancouver", entries: lib, citations: cites).text(["ghost"]) == "(?ghost)")73 // An empty library and an empty citation do not crash.74 let empty = Cited(style: "apa", entries: [], citations: [[cite("x")]])75 #expect(empty.text(["x"]) == "(?x)")76 #expect(empty.bibliography.isEmpty)77 #expect(empty.engine.citationText(Citation(items: []), document: empty.doc, numbering: empty.numbering) == "")78 }7980 @Test func frenchLocalisation() {81 let two = BibEntry(key: "two", type: "article-journal", authors: [person("Tremblay", "Marie"), person("Roy", "Jean-Luc")], year: 2021,82 fields: ["title": "Le titre", "container-title": "Revue", "volume": "4", "issue": "2", "page": "10-20"])83 let three = BibEntry(key: "three", type: "chapter", authors: [person("Gagnon", "A."), person("Côté", "B."), person("Bouchard", "C.")], editors: [person("Lavoie", "D.")],84 year: 2019, fields: ["title": "Un chapitre", "container-title": "Un ouvrage", "edition": "2", "page": "5", "publisher": "PUQ"])85 let nd = BibEntry(key: "nd", type: "webpage", authors: [org("Statistique Canada")], fields: ["title": "Une page", "accessed": "2020-01-09", "URL": "https://www.statcan.gc.ca/"])86 let c = Cited(style: "apa", language: "fr-CA", entries: [two, three, nd], citations: [[cite("two")], [cite("three")], [cite("nd")]])87 #expect(c.text(cite("two", "12")) == "(Tremblay et Roy, 2021, p. 12)")88 #expect(c.text(cite("two", "12-14")) == "(Tremblay et Roy, 2021, p. 12–14)")89 #expect(c.text(cite("three")) == "(Gagnon et al., 2019)")90 #expect(c.text(cite("nd")) == "(Statistique Canada, s. d.)")91 let bib = c.bibliography92 #expect(bib[0] == "Gagnon, A., Côté, B. et Bouchard, C. (2019). Un chapitre. Dans D. Lavoie (dir.), *Un ouvrage* (2e éd., p. 5). PUQ.")93 #expect(bib[1] == "Statistique Canada. (s. d.). *Une page*. Consulté le 9 janvier 2020, à l’adresse https://www.statcan.gc.ca/")94 #expect(bib[2] == "Tremblay, M. et Roy, J.-L. (2021). Le titre. *Revue*, *4*(2), 10–20.")95 let chicago = Cited(style: "chicago-author-date", language: "fr", entries: [two, three, nd], citations: [[cite("two")], [cite("three")], [cite("nd")]])96 #expect(chicago.text(cite("two")) == "(Tremblay et Roy 2021)")97 #expect(chicago.text(cite("three")) == "(Gagnon, Côté et Bouchard 2019)")98 #expect(chicago.bibliography[0] == "Gagnon, A., B. Côté et C. Bouchard. 2019. “Un chapitre.” Dans *Un ouvrage*, 2e éd., sous la direction de D. Lavoie, 5. PUQ.")99 // The same document in English.100 let en = Cited(style: "apa", language: "en", entries: [two, three, nd], citations: [[cite("two")], [cite("three")], [cite("nd")]])101 #expect(en.text(cite("two")) == "(Tremblay & Roy, 2021)")102 #expect(en.bibliography[0] == "Gagnon, A., Côté, B., & Bouchard, C. (2019). Un chapitre. In D. Lavoie (Ed.), *Un ouvrage* (2nd ed., p. 5). PUQ.")103 }104105 @Test func bibliographyParagraphsCarryStyleAndRunProperties() throws {106 let c = Cited(style: "apa", entries: [APAStyleTests.grady], citations: [[cite("grady2019")]])107 let paragraphs = c.engine.bibliographyParagraphs(document: c.doc, numbering: c.numbering)108 let p = try #require(paragraphs.first)109 #expect(p.style == StyleID.bibliography)110 #expect(p.id == "bib-grady2019")111 #expect(p.runs.filter { $0.props.italic == true }.map(\.text) == ["Psychology of Popular Media Culture", "8"])112 #expect(p.runs.last?.props.link == "https://doi.org/10.1037/ppm0000185")113 #expect(p.runs.allSatisfy { $0.inline == nil && !$0.text.isEmpty })114 // Citations inside footnotes and tables are part of the numbering, and the context follows document edits.115 var doc = c.doc116 doc.bibliography.append(APAStyleTests.jackson)117 doc.body.append(.paragraph(Paragraph(runs: [Run(inline: .citation(Citation("jackson2019")))])))118 let n2 = Numbering(doc)119 #expect(c.engine.bibliographyParagraphs(document: doc, numbering: n2).count == 2)120 #expect(c.engine.bibliographyParagraphs(document: c.doc, numbering: c.numbering).count == 1)121 }122123 @Test func incompleteAuthorListsLiteralDatesAndReports() {124 // "and others" in BibTeX: the list is known to be incomplete, whatever its length.125 let others = BibEntry(key: "o", type: "article-journal", authors: [person("LeCun", "Yann"), person("Bengio", "Yoshua"), .others], year: 2015,126 fields: ["title": "Deep learning", "container-title": "Nature", "volume": "521", "page": "436-444", "DOI": "10.1038/nature14539"])127 #expect(formatted("apa", others).hasPrefix("LeCun, Y., Bengio, Y., et al. (2015)."))128 #expect(formatted("chicago-author-date", others).hasPrefix("LeCun, Yann, Yoshua Bengio, et al. 2015."))129 #expect(formatted("mla", others).hasPrefix("LeCun, Yann, et al. “Deep learning.”"))130 #expect(formatted("ieee", others).hasPrefix("Y. LeCun, Y. Bengio et al., “Deep learning,”"))131 #expect(formatted("vancouver", others) == "LeCun Y, Bengio Y, et al. Deep learning. Nature. 2015;521:436-444. doi: 10.1038/nature14539.")132 let c = Cited(style: "apa", entries: [others], citations: [[cite("o")]])133 #expect(c.text(cite("o")) == "(LeCun et al., 2015)")134 // A literal date is printed as stored; "n.d." typed by hand is the same as no date.135 var e = BibEntry(key: "p", type: "article-journal", authors: [person("Roy", "A.")], fields: ["title": "T", "container-title": "J", ReferenceField.issuedLiteral: "in press"])136 #expect(formatted("apa", e) == "Roy, A. (in press). T. *J*.")137 #expect(formatted("chicago-author-date", e) == "Roy, A. in press. “T.” *J*.")138 e.fields[ReferenceField.issuedLiteral] = "n.d."139 #expect(formatted("apa", e) == "Roy, A. (n.d.). T. *J*.")140 #expect(formatted("apa", e, language: "fr") == "Roy, A. (s. d.). T. *J*.")141 #expect(formatted("mla", e) == "Roy, A. “T.” *J*.")142 // Title ending with a question mark: no doubled punctuation, in or out of quotation marks.143 let q = BibEntry(key: "q", type: "article-journal", authors: [person("Roy", "A.")], year: 2001, fields: ["title": "Why?", "container-title": "J", "volume": "2", "page": "3"])144 #expect(formatted("apa", q) == "Roy, A. (2001). Why? *J*, *2*, 3.")145 #expect(formatted("chicago-author-date", q) == "Roy, A. 2001. “Why?” *J* 2: 3.")146 #expect(formatted("ieee", q) == "A. Roy, “Why?” *J*, vol. 2, p. 3, 2001.")147 #expect(formatted("vancouver", q) == "Roy A. Why? J. 2001;2:3.")148 }149150 @Test func titlesAreNeverRecased() {151 // The stored capitalisation is printed as is in every style (APA would want sentence case, Chicago and MLA title case).152 let e = BibEntry(key: "k", type: "article-journal", authors: [person("Doe", "J.")], year: 2020,153 fields: ["title": "A MiXed-case TITLE about GARCH and the iPhone", "container-title": "journal of lower case", "volume": "1", "page": "2"])154 for style in CitationEngine.styles.map(\.id) {155 let s = formatted(style, e)156 #expect(s.contains("A MiXed-case TITLE about GARCH and the iPhone"), "\(style)")157 #expect(s.contains("journal of lower case"), "\(style)")158 }159 }160161 @Test func performanceWith5000Entries() throws {162 let clock = ContinuousClock()163 var bib = ""164 for i in 1...5000 {165 bib += """166 @article{key\(i),167 author = {Tremblay, Marie-\(i % 7 == 0 ? "{\\'E}ve" : "Claude") and van der Berg\(i % 400), Jan and {Institut de la statistique du Qu{\\'e}bec}},168 title = {Volatility clustering in {GARCH} models, part \(i): evidence from \(1900 + i % 120) markets --- a study},169 journal = {Journal of Financial Econometrics},170 year = \(1980 + i % 45), volume = {\(i % 90 + 1)}, number = {\(i % 12 + 1)}, pages = {\(i)--\(i + 17)}, month = \(RefDate.bibMacros[i % 12]),171 doi = {10.1000/jfe.\(i)}, abstract = {Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.},172 keywords = {volatility, GARCH, markets}173 }174175 """176 }177 var parsed = BibliographyParseResult()178 let tParse = clock.measure { parsed = BibTeXParser.parse(bib) }179 #expect(parsed.entries.count == 5000)180 #expect(parsed.issues.isEmpty)181 var written = ""182 let tWrite = clock.measure { written = BibTeXWriter.write(parsed.entries) }183 #expect(BibTeXParser.parse(written).entries == parsed.entries)184 var json = Data()185 let tJSON = try clock.measure { json = try CSLJSON.write(parsed.entries) }186 var fromJSON = BibliographyParseResult()187 let tJSONParse = clock.measure { fromJSON = CSLJSON.parse(json) }188 #expect(fromJSON.entries == parsed.entries)189 var merged = 0190 let tMerge = clock.measure { merged = BibliographyImporter.merge(parsed.entries, into: parsed.entries).entries.count }191 #expect(merged == 5000)192193 var report = "ORIX Docs references performance, 5 000 entries (\(bib.utf8.count / 1024) KiB of BibTeX), debug build:\n"194 report += " BibTeX parse \(tParse) · BibTeX write \(tWrite) · CSL-JSON write \(tJSON) · CSL-JSON parse \(tJSONParse) · merge 5 000 into 5 000 \(tMerge)\n"195 var total = tParse196 for style in ["apa", "ieee"] {197 let c = Cited(style: style, entries: parsed.entries, citations: parsed.entries.map { [cite($0.key)] })198 var paragraphs = 0, characters = 0199 let tBib = clock.measure { paragraphs = c.bibliography.count }200 let tCite = clock.measure { for e in parsed.entries { characters += c.text([e.key]).count } }201 #expect(paragraphs == 5000)202 #expect(characters > 15000)203 report += " \(style): bibliography of 5 000 works \(tBib) · 5 000 in-text citations \(tCite)\n"204 total += tBib + tCite205 }206 print(report)207 #expect(total < .seconds(20), "the whole pipeline should stay interactive even in a debug build")208 }209}210