Repository Public
1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocReferencesTests/TestSupport.swift3// Description: Helpers shared by the references tests: runs rendered as marked-up text, document builders, known-deviation assertions.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import DocModel10@testable import DocReferences1112/// Runs as text with italics between asterisks, so expected strings read like the style guides print them.13func marked(_ runs: [Run]) -> String {14 var out = ""15 var italicOpen = false16 for r in runs {17 let italic = r.props.italic == true18 if italic != italicOpen { out += "*"; italicOpen = italic }19 out += r.text20 }21 if italicOpen { out += "*" }22 return out23}2425func person(_ family: String, _ given: String = "") -> PersonName { PersonName(family: family, given: given) }26func org(_ name: String) -> PersonName { PersonName(family: "", given: "", literal: name) }2728func formatted(_ style: String, _ entry: BibEntry, language: String = "en") -> String {29 marked(CitationEngine(styleID: style, language: language).formattedEntry(entry, number: nil))30}3132/// A document whose body cites the given groups of items, one citation per paragraph.33func document(style: String, language: String = "en", entries: [BibEntry], citations: [[CitationItem]]) -> DocumentModel {34 var doc = DocumentModel(meta: DocumentMetadata(language: language),35 body: citations.map { .paragraph(Paragraph(runs: [Run("See "), Run(inline: .citation(Citation(items: $0)))])) })36 doc.settings.citationStyle = style37 doc.bibliography = entries38 return doc39}4041struct Cited {42 let doc: DocumentModel43 let numbering: Numbering44 let engine: CitationEngine45 init(style: String, language: String = "en", entries: [BibEntry], citations: [[CitationItem]]) {46 doc = document(style: style, language: language, entries: entries, citations: citations)47 numbering = Numbering(doc)48 engine = CitationEngine(document: doc)49 }50 func text(_ items: CitationItem...) -> String { engine.citationText(Citation(items: items), document: doc, numbering: numbering) }51 func text(_ keys: [String]) -> String { engine.citationText(Citation(items: keys.map { CitationItem(key: $0) }), document: doc, numbering: numbering) }52 var bibliography: [String] { engine.bibliographyParagraphs(document: doc, numbering: numbering).map { marked($0.runs) } }53}5455func cite(_ key: String, _ locator: String? = nil, prefix: String? = nil, suffix: String? = nil, suppressAuthor: Bool = false) -> CitationItem {56 CitationItem(key: key, locator: locator, prefix: prefix, suffix: suffix, suppressAuthor: suppressAuthor)57}5859/// Asserts the engine's output where it knowingly differs from the published example. The published string is kept in the test,60/// and the difference must be listed in docs/architecture/references.md.61func expectKnownDeviation(_ actual: String, published: String, ours: String, _ reason: Testing.Comment, sourceLocation: SourceLocation = #_sourceLocation) {62 #expect(actual == ours, reason, sourceLocation: sourceLocation)63 #expect(ours != published, "the deviation no longer exists: assert the published string directly", sourceLocation: sourceLocation)64}6566/// Deterministic generator (SplitMix64) so randomised tests are reproducible.67struct SeededGenerator: RandomNumberGenerator {68 var state: UInt6469 init(seed: UInt64) { state = seed }70 mutating func next() -> UInt64 {71 state &+= 0x9E37_79B9_7F4A_7C1572 var z = state73 z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B974 z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB75 return z ^ (z >> 31)76 }77}78