@testable import PureXML
import Testing
/// Schema-driven completions: the exact follow-set of allowed next elements,
/// whether content may end, and attribute required/present status. The basis for
/// editor autocomplete and "what's missing".
@Suite("XSD completions")
struct XSDCompletionTests2 {
private let xsd = """
"""
private func completions(_ xml: String) throws -> PureXML.Schema.Completions? {
let schema = try PureXML.Schema.Document(xsd)
let (tree, _) = PureXML.readTree(xml)
return schema.completions(at: [.element("order")], in: tree)
}
@Test("The follow-set is the exact next allowed element")
func test_followSet() throws {
#expect(try completions("")?.elements == ["name"])
#expect(try completions("n")?.elements == ["qty"])
#expect(try completions("n1")?.elements == ["note"])
}
@Test("complete reflects whether a required child is still expected")
func test_complete() throws {
#expect(try completions("n")?.complete == false)
#expect(try completions("n1")?.complete == true)
}
@Test("Attribute completions carry required and present status")
func test_attributes() throws {
let attrs = try #require(try completions("")?.attributes)
#expect(attrs == [.init(name: "id", required: true, present: false)])
let present = try #require(try completions("")?.attributes)
#expect(present == [.init(name: "id", required: true, present: true)])
}
@Test("Completions resolve for a nested element by its coding path")
func test_nested() throws {
let nestedXsd = """
"""
let schema = try PureXML.Schema.Document(nestedXsd)
let (tree, _) = PureXML.readTree(" ")
let here = schema.completions(at: [.element("root"), .element("item")], in: tree)
#expect(here?.elements == ["a"])
#expect(here?.complete == false)
}
}