@testable import PureXML
import Testing
@Suite("XSD schema documents")
struct XSDSchemaTests {
private func validate(_ xsd: String, _ xml: String) throws -> [PureXML.Validation.ValidationError] {
try PureXML.Schema.Document(xsd).validate(xml)
}
@Test("A nested type violation is located by coding path")
func test_violationCarriesPath() throws {
let xsd = """
"""
let found = try validate(xsd, "lots")
#expect(found.count == 1)
#expect(String(describing: found[0]).hasSuffix("at path: order/qty"))
}
@Test("A simple element with a built-in type")
func test_simpleElement() throws {
let xsd = """
"""
#expect(try validate(xsd, "30").isEmpty)
#expect(try !validate(xsd, "-5").isEmpty)
#expect(try !validate(xsd, "x").isEmpty)
}
@Test("A named simpleType with facets")
func test_namedSimpleType() throws {
let xsd = """
"""
#expect(try validate(xsd, "ABC-1234").isEmpty)
#expect(try !validate(xsd, "abc-1234").isEmpty)
}
@Test("A complex type with a sequence and attributes")
func test_complexType() throws {
let xsd = """
"""
#expect(try validate(xsd, "T2026").isEmpty)
#expect(try validate(xsd, "T").isEmpty)
#expect(try !validate(xsd, "T").isEmpty) // missing isbn
#expect(try !validate(xsd, "2026").isEmpty) // missing title
#expect(try !validate(xsd, "Tnope").isEmpty)
}
@Test("A named complex type referenced by elements")
func test_namedComplexType() throws {
let xsd = """
"""
#expect(try validate(xsd, "1.52.5").isEmpty)
#expect(try !validate(xsd, "1.5").isEmpty)
}
@Test("Recursive types validate to any depth")
func test_recursiveType() throws {
let xsd = """
"""
let nested = "123"
#expect(try validate(xsd, nested).isEmpty)
let bad = "1x"
#expect(try !validate(xsd, bad).isEmpty)
}
@Test("Simple content restricts the text and keeps attributes")
func test_simpleContent() throws {
let xsd = """
"""
#expect(try validate(xsd, "9.99").isEmpty)
#expect(try !validate(xsd, "nope").isEmpty)
}
@Test("An unknown root element is reported")
func test_unknownRoot() throws {
let xsd = """
"""
#expect(try !validate(xsd, "").isEmpty)
}
}