@testable import PureYAML import Testing @Suite("Comments must be preceded by whitespace") struct CommentWhitespaceTests { @Test("A comment glued to earlier content on its line is rejected", arguments: [ "key: \"value\"# comment\n", // after a quoted scalar "[ a, b ]#comment\n", // after a flow sequence close "{ a: b }#comment\n", // after a flow mapping close "block: >#comment\n x\n", // after a block scalar header "[ a,#comment\n]\n", // after a flow entry separator ]) func test_gluedCommentIsRejected(_ yaml: String) throws { #expect(throws: PureYAML.Parsing.ParseError.self) { try PureYAML.parse(yaml) } } @Test("A comment preceded by whitespace or opening a line is accepted", arguments: [ "key: \"value\" # comment\n", // a space before the comment "# comment\nkey: v\n", // a comment opening the document "a: 1\n # comment\nb: 2\n", // an indented comment line "[ a,\n # comment\n b ]\n", // a comment line inside a flow collection ]) func test_wellSpacedCommentIsAccepted(_ yaml: String) throws { _ = try PureYAML.parse(yaml) } @Test("A '#' inside a plain scalar stays literal content, not a comment") func test_hashInsidePlainScalar() throws { #expect(try PureYAML.parse("key: a#b\n") == .mapping(.init([ .init(key: "key", value: .string("a#b")), ]))) } }