diff --git a/CHANGELOG.md b/CHANGELOG.md index c11975b..0595a59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Schema conformance fixtures, completing XSD and RELAX NG validation (#2). A + cross-cutting suite validates realistic schemas (a patterned, bounded, + ID-keyed XSD purchase order and a RELAX NG contact card using interleave, + attribute value choices, and optional/repeatable elements) against conforming + and non-conforming instances, plus datatype boundary fixtures. - RELAX NG validation by derivatives (toward #2). `PureXML.Schema.RelaxNG` parses a RELAX NG schema in the XML syntax into a pattern grammar (`element`, `attribute`, `text`, `empty`, `notAllowed`, `group`, `choice`, `interleave`, diff --git a/README.md b/README.md index 05dc594..91bf6c9 100644 --- a/README.md +++ b/README.md @@ -81,22 +81,6 @@ flowchart TB Core --> Chars ``` -Validation: - -```mermaid -flowchart TB - classDef done fill:#ddf9e4,stroke:#34c759,color:#111827 - classDef review fill:#fff7d6,stroke:#ffcc00,color:#111827 - classDef epic fill:#f2e5ff,stroke:#af52de,color:#111827 - classDef todo fill:#f2f4f7,stroke:#8e8e93,color:#111827 - Querying["Querying: #21 XPath, #22 xmlPattern, #23 XPointer (shipped)"]:::done - Regexp["#30 Regular expression engine"]:::done - Xsd["#2 XSD / RELAX NG validation"]:::epic - Schematron["#25 Schematron validation"]:::done - Regexp --> Xsd - Querying --> Schematron -``` - Transformation and integrity: ```mermaid @@ -105,6 +89,7 @@ flowchart TB classDef review fill:#fff7d6,stroke:#ffcc00,color:#111827 classDef epic fill:#f2e5ff,stroke:#af52de,color:#111827 classDef todo fill:#f2f4f7,stroke:#8e8e93,color:#111827 + Validation["Validation shipped: #21 #22 #23 query, #30 regex, #2 XSD/RELAX NG, #25 Schematron"]:::done Xslt["#3 XSLT 1.0"]:::epic XInclude["#24 XInclude, URI, xml:base"]:::done C14n["#26 Canonical XML"]:::done diff --git a/Tests/SchemaConformanceTests.swift b/Tests/SchemaConformanceTests.swift new file mode 100644 index 0000000..7aa299b --- /dev/null +++ b/Tests/SchemaConformanceTests.swift @@ -0,0 +1,134 @@ +@testable import PureXML +import Testing + +/// Cross-cutting conformance fixtures for both schema languages: realistic +/// schemas validated against valid and invalid instances. +@Suite("Schema conformance") +struct SchemaConformanceTests { + // MARK: A realistic XSD: a purchase order + + private let purchaseOrderXSD = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + """ + + @Test("Purchase order: a fully conforming document") + func test_orderValid() throws { + let schema = try PureXML.Schema.Document(purchaseOrderXSD) + let xml = """ + 123-AB5\ + 999-ZZ999 + """ + #expect(try schema.validate(xml).isEmpty) + } + + @Test("Purchase order: each constraint is enforced") + func test_orderViolations() throws { + let schema = try PureXML.Schema.Document(purchaseOrderXSD) + // Bad SKU pattern. + #expect(try !schema.validate("bad5").isEmpty) + // Quantity above the maxInclusive. + #expect(try !schema.validate("123-AB1000").isEmpty) + // Quantity not positive. + #expect(try !schema.validate("123-AB0").isEmpty) + // Missing the required id attribute. + #expect(try !schema.validate("123-AB5").isEmpty) + // At least one item is required. + #expect(try !schema.validate("").isEmpty) + } + + // MARK: A realistic RELAX NG: a contact card with attributes and interleave + + private let contactRNG = """ + + + + + personcompany + + + + + + + + + """ + + @Test("Contact: valid documents in varying order") + func test_contactValid() throws { + let schema = try PureXML.Schema.RelaxNG(contactRNG) + #expect(try schema.validate("Ada")) + #expect(try schema.validate(""" + 1Acme\ + a@b.c2 + """)) + } + + @Test("Contact: invalid documents are rejected") + func test_contactInvalid() throws { + let schema = try PureXML.Schema.RelaxNG(contactRNG) + // Missing the required kind attribute. + #expect(try !schema.validate("Ada")) + // kind not one of the allowed values. + #expect(try !schema.validate("Ada")) + // Missing the required name element. + #expect(try !schema.validate("a@b.c")) + // Two names (name is not repeatable). + #expect(try !schema.validate("AB")) + } + + // MARK: Datatype boundary fixtures + + private struct DatatypeCase { + let value: String + let type: PureXML.Schema.BuiltinType + let expected: Bool + } + + @Test("Datatype boundaries across the type library") + func test_datatypeBoundaries() { + let cases: [DatatypeCase] = [ + .init(value: "2024-02-29", type: .date, expected: true), + .init(value: "2025-02-29", type: .date, expected: false), + .init(value: "9223372036854775807", type: .long, expected: true), + .init(value: "9223372036854775808", type: .long, expected: false), + .init(value: "P1Y", type: .duration, expected: true), + .init(value: "1Y", type: .duration, expected: false), + .init(value: "0.1", type: .decimal, expected: true), + .init(value: "1.0e3", type: .decimal, expected: false), + .init(value: "true", type: .boolean, expected: true), + .init(value: "urn:example:x", type: .anyURI, expected: true), + ] + for testCase in cases { + #expect( + PureXML.Schema.isValid(testCase.value, type: testCase.type) == testCase.expected, + "\(testCase.value) as \(testCase.type.rawValue)", + ) + } + } +}