import Testing
@testable import PureXML
/// Schema compile findings carry validation coding paths that resolve to source
/// spans on a ranged schema tree, the IDE underline flow for #169.
@Suite("Schema located compile diagnostics")
struct SchemaLocatedDiagnosticsTests {
private func inconsistentFindings(in xsd: String) throws -> [PureXML.Validation.ValidationError] {
do {
_ = try PureXML.Schema.Document(xsd)
Issue.record("expected schema compilation to fail")
return []
} catch let error as PureXML.Schema.SchemaError {
guard case let .inconsistent(findings) = error else {
Issue.record("expected .inconsistent, got \(error)")
return []
}
return findings
}
}
@Test("A duplicate xs:ID is located on the offending component")
func test_duplicateIdLocated() throws {
let xsd = """
"""
let findings = try inconsistentFindings(in: xsd)
let duplicate = try #require(findings.first { $0.reason.contains("duplicate id") })
#expect(duplicate.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
#expect(duplicate.codingPath.last?.intValue == 2)
let (tree, _) = PureXML.readTree(xsd)
let range = tree.sourceRange(at: duplicate.codingPath)
#expect(range?.start.line == 3)
#expect(range?.start.column == 3)
}
@Test("An invalid xs:ID value is located on its declaring component")
func test_invalidIdLocated() throws {
let xsd = """
"""
let findings = try inconsistentFindings(in: xsd)
let invalid = try #require(findings.first { $0.reason.contains("not a valid NCName") })
#expect(invalid.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
let (tree, _) = PureXML.readTree(xsd)
let range = tree.sourceRange(at: invalid.codingPath)
#expect(range?.start.line == 2)
#expect(range?.start.column == 3)
}
@Test("An ID-typed default/fixed value constraint is located on its declaring component")
func test_idValueConstraintLocated() throws {
let xsd = """
"""
let findings = try inconsistentFindings(in: xsd)
let located = try #require(findings.first { $0.reason.contains("must not have a default or fixed value") })
#expect(located.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
let (tree, _) = PureXML.readTree(xsd)
let range = tree.sourceRange(at: located.codingPath)
#expect(range?.start.line == 2)
#expect(range?.start.column == 3)
}
@Test("A bad value constraint against a named user type is located on its declaring element")
func test_userTypeValueConstraintLocated() throws {
let xsd = """
"""
let findings = try inconsistentFindings(in: xsd)
let located = try #require(findings.first { $0.reason.contains("is not valid for type 't:count'") })
#expect(located.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
let (tree, _) = PureXML.readTree(xsd)
let range = tree.sourceRange(at: located.codingPath)
#expect(range?.start.line == 3)
#expect(range?.start.column == 3)
}
@Test("A bad value constraint against an inline type is located on its declaring element")
func test_inlineTypeValueConstraintLocated() throws {
let xsd = """
"""
let findings = try inconsistentFindings(in: xsd)
let located = try #require(findings.first { $0.reason.contains("is not valid for the declared inline type") })
#expect(located.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
let (tree, _) = PureXML.readTree(xsd)
let range = tree.sourceRange(at: located.codingPath)
#expect(range?.start.line == 2)
#expect(range?.start.column == 3)
}
@Test("A bad value constraint against a built-in type is located on its declaring element")
func test_builtinValueConstraintLocated() throws {
let xsd = """
"""
let findings = try inconsistentFindings(in: xsd)
let located = try #require(findings.first { $0.reason.contains("is not valid for type 'xs:integer'") })
#expect(located.codingPath.map(\.stringValue) == ["xs:schema", "xs:element"])
let (tree, _) = PureXML.readTree(xsd)
let range = tree.sourceRange(at: located.codingPath)
#expect(range?.start.line == 2)
#expect(range?.start.column == 3)
}
@Test("An xs:include targetNamespace mismatch is located on the include directive")
func test_includeMismatchLocated() throws {
let included = """
"""
let main = """
"""
let findings: [PureXML.Validation.ValidationError]
do {
_ = try PureXML.Schema.Document(main, schemaLoader: { $0 == "other.xsd" ? included : nil })
Issue.record("expected include mismatch to be rejected")
return
} catch let error as PureXML.Schema.SchemaError {
guard case let .inconsistent(found) = error else {
Issue.record("expected .inconsistent, got \(error)")
return
}
findings = found
}
let mismatch = try #require(findings.first { $0.reason.contains("included schema targetNamespace") })
#expect(mismatch.codingPath.map(\.stringValue) == ["xs:schema", "xs:include"])
let (tree, _) = PureXML.readTree(main)
let range = tree.sourceRange(at: mismatch.codingPath)
#expect(range?.start.line == 2)
#expect(range?.start.column == 3)
}
@Test("SchemaError.inconsistentFindings exposes the validation errors")
func test_inconsistentFindingsAccessor() throws {
let xsd = """
"""
do {
_ = try PureXML.Schema.Document(xsd)
Issue.record("expected schema compilation to fail")
} catch let error as PureXML.Schema.SchemaError {
let findings = try #require(error.inconsistentFindings)
#expect(findings.contains { $0.reason.contains("not a valid NCName") })
}
}
}