import Testing @testable import PureXML /// `Escaping.text` and `Escaping.attribute` copy the verbatim runs between /// escapable characters in bulk instead of appending each character one at a /// time. These pin the escaped output against the per-character reference for /// markers at the start, end, and adjacent; long verbatim runs around them; /// multibyte runs (the copied run spans whole graphemes); both quote styles; /// and ASCII-only mode (where a non-ASCII character escapes and an ASCII one is /// copied with its run). @Suite("Escaping bulk-copy equivalence") struct EscapingBulkCopyTests { private typealias Escaping = PureXML.Emitting.Escaping /// The per-character escaping the bulk path replaced, kept here as the oracle. private func referenceText(_ value: String, asciiOnly: Bool, escapeCarriageReturn: Bool) -> String { var result = "" for character in value { switch character { case "&": result += "&" case "<": result += "<" case ">": result += ">" case "\r" where escapeCarriageReturn: result += " " default: if asciiOnly { for scalar in character.unicodeScalars { result += scalar.value > 0x7F ? "&#x\(String(scalar.value, radix: 16, uppercase: true));" : String(scalar) } } else { result += String(character) } } } return result } private static let texts = [ "plain text no markup", "a & b < c > d", "&<>", "&start", "end&", "&&&", "café & thé", "e\u{0301} < x", "\u{1F600}&\u{1F601}", "tab\tnewline\ncr\r", "naïve résumé", // Combining marks: in ASCII-only mode the scan's first byte >= 0x80 is the // mark's lead byte, mid-grapheme; the whole cluster must still escape as a // unit, not duplicate or drop the base letter. "e\u{0301}", "a\u{0301}b", "e\u{0301}\u{0301}", "x\u{0301}y\u{0301}z", "a\u{0301}t & u < v"# let tree = try PureXML.parseTree(document) let again = try PureXML.parseTree(PureXML.serialize(tree.node)) #expect(PureXML.serialize(tree.node) == PureXML.serialize(again.node)) } }