@testable import PureXML import Testing @Suite("Schematron key()/document()/current() in tests") struct SchematronFunctionTests { private func validate(_ schema: String, _ xml: String, loader: @escaping (String) -> String? = { _ in nil }) throws -> [PureXML.Validation.ValidationError] { try PureXML.Validation.Schematron(schema: schema).validate(xml, documentLoader: loader) } private let sch = "http://purl.oclc.org/dsdl/schematron" private let xsl = "http://www.w3.org/1999/XSL/Transform" @Test("key() resolves a node by an xsl:key index") func test_key() throws { let schema = """ no item with that id """ // ref/@to=a resolves to item id=a; ref/@to=z resolves to nothing -> one failure. let xml = "" let errors = try validate(schema, xml) #expect(errors.count == 1) #expect(errors.first?.reason.contains("no item") == true) } @Test("document() loads an external file for cross-document checks") func test_document() throws { let schema = """ id is not in the allowed list """ let allowed = "ab" let xml = "" let errors = try validate(schema, xml) { $0 == "allowed.xml" ? allowed : nil } #expect(errors.count == 1) #expect(errors.first?.reason.contains("not in the allowed") == true) } @Test("current() refers to the rule's context node inside a predicate") func test_current() throws { let schema = """ the referenced item must exist """ // item ref=b resolves (item id=b exists); item ref=z does not -> one failure. let xml = "" let errors = try validate(schema, xml) #expect(errors.count == 1) } @Test("A schema without these functions still validates") func test_plain() throws { let schema = """ too big """ #expect(try validate(schema, "5").isEmpty) #expect(try !validate(schema, "50").isEmpty) } }