@testable import PureYAML import Testing @Suite("%YAML directive validation") struct YAMLDirectiveTests { @Test("A malformed or repeated %YAML directive is rejected", arguments: [ "%YAML 1.2 foo\n---\nx\n", // an extra parameter after the version "%YAML 1.2\n%YAML 1.2\n---\nx\n", // a second %YAML directive "%YAML 2.0\n---\nx\n", // an incompatible major version ]) func test_invalidYAMLDirectiveIsRejected(_ yaml: String) throws { #expect(throws: PureYAML.Parsing.ParseError.self) { try PureYAML.parse(yaml) } } @Test("A higher minor version is accepted, since the spec calls for processing it with a warning") func test_higherMinorVersionAccepted() throws { #expect(try PureYAML.parse("%YAML 1.3\n---\nx\n") == .string("x")) } @Test("A well-formed %YAML directive, with an optional trailing comment, is accepted") func test_validYAMLDirectiveAccepted() throws { let expected = PureYAML.Model.Value.string("x") #expect(try PureYAML.parse("%YAML 1.2\n---\nx\n") == expected) #expect(try PureYAML.parse("%YAML 1.2 # version\n---\nx\n") == expected) } @Test("A directive with no following document is rejected", arguments: [ "%YAML 1.2\n", // directive then end of stream "%YAML 1.2\n...\n", // directive then a document-end marker "%TAG !e! tag:example,0:\n", // any directive, not just %YAML ]) func test_directiveWithoutDocumentRejected(_ yaml: String) throws { #expect(throws: PureYAML.Parsing.ParseError.self) { try PureYAML.parse(yaml) } } @Test("A directive followed by an explicit document start is accepted") func test_directiveWithDocumentAccepted() throws { #expect(try PureYAML.parse("%YAML 1.2\n---\n") == .null) #expect(try PureYAML.parse("%TAG !e! tag:example,0:\n---\nx\n") == .string("x")) } }