@testable import PureYAML import Testing /// YAML 1.2.2, section 5.2: a stream may begin with a byte order mark (U+FEFF), which is not part of the content. Through each entry point, a document gives the same /// value, the same nodes and the same positions with the mark as without it; the mark does not count as a column of the first line, as in libyaml, whose reader takes it /// before the scanner sees a character. Before, it was skipped but counted, so a block sequence or a comment on the first line did not parse. @Suite("Byte order mark at the start of a stream") struct ByteOrderMarkTests { static let single = [ "a: 1\n", "a: 1\nb:\n - x\n - y\n", "{a: 1}\n", "plain\n", "- a\n- b\n", "- a:\n b: 1\n c: 2\n", "# comment\na: 1\n", "---\na: 1\n", "a: |\n text\n more\n", "? key\n: value\n", "a: &x 1\nb: *x\n", "", "\n", "- - a\n - b\n- c\n", "a:\n- x\n- y\n", ] static let streams = ["--- 1\n--- 2\n", "a: 1\n---\nb: 2\n", "# c\n--- a\n...\n--- b\n"] @Test("The value read is the same with the mark as without it", arguments: single) func parse(_ body: String) throws { #expect(try PureYAML.parse("\u{FEFF}" + body) == PureYAML.parse(body)) } @Test("The located value has the same shape and the same positions, the mark not counted as a column", arguments: single) func located(_ body: String) throws { let plain = try PureYAML.parseLocated(body) let marked = try PureYAML.parseLocated("\u{FEFF}" + body) #expect(marked == plain) } @Test("A stream of documents reads the same with the mark as without it", arguments: streams) func stream(_ body: String) throws { let plain = try PureYAML.parseStreamLocated(body) let marked = try PureYAML.parseStreamLocated("\u{FEFF}" + body) #expect(marked == plain) #expect(try PureYAML.parseStream("\u{FEFF}" + body).count == PureYAML.parseStream(body).count) } @Test("A mark inside a quoted string is the string's own character, written bare or as the escape", arguments: ["a: \"\u{FEFF}1\"\n", "a: \"1\u{FEFF}\"\n"]) func insideAString(_ body: String) throws { let escaped = body.replacingOccurrences(of: "\u{FEFF}", with: "\\uFEFF") #expect(try PureYAML.parse(body) == PureYAML.parse(escaped)) #expect(try PureYAML.parse("\u{FEFF}" + body) == PureYAML.parse(escaped), "and a mark that opens the stream does not change that") } }