import Testing
@testable import PureXML
/// Quick-fixes derived from the structured schema completions, with precise
/// placement from content spans. The strongest check: applying a fix yields XML
/// that the same schema then accepts.
@Suite("Quick fixes")
struct QuickFixTests {
private let xsd = """
"""
private func apply(_ edit: PureXML.TextEdit, to xml: String) -> String {
var characters = Array(xml)
let start = edit.range.start.offset
let end = edit.range.end.offset
characters.replaceSubrange(start ..< end, with: Array(edit.replacement))
return String(characters)
}
@Test("A missing required attribute becomes an insertion before the start tag")
func test_addAttribute() throws {
let schema = try PureXML.Schema.Document(xsd)
let (tree, _) = PureXML.readTree("1")
let fixes = schema.quickFixes(at: [.element("order")], in: tree)
let fix = try #require(fixes.first { $0.title.contains("attribute 'id'") })
let fixed = apply(fix.edits[0], to: "1")
#expect(fixed == "1")
// The required-attribute error is gone after applying.
#expect(try schema.validate(fixed).contains { $0.reason.contains("required attribute 'id'") } == false)
}
@Test("A still-expected required child becomes an insertion before the end tag")
func test_insertChild() throws {
let schema = try PureXML.Schema.Document(xsd)
let (tree, _) = PureXML.readTree("")
let fixes = schema.quickFixes(at: [.element("order")], in: tree)
let fix = try #require(fixes.first { $0.title == "Insert " })
let fixed = apply(fix.edits[0], to: "")
#expect(fixed == "")
}
@Test("A complete, valid element offers no fixes")
func test_noFixes() throws {
let schema = try PureXML.Schema.Document(xsd)
let (tree, _) = PureXML.readTree("1")
#expect(schema.quickFixes(at: [.element("order")], in: tree).isEmpty)
}
}