@testable import PureXML
import Testing
/// The `id` attribute on any XSD component is of type `xs:ID`: its value must be
/// a valid NCName and unique within the schema document (XSD Structures). Such a
/// schema is invalid and must be rejected at compile time; previously the `id`
/// value was never checked and the schema accepted (XSTS invalid-schema idA-idK,
/// attgA, attB, ctA, and related cases).
@Suite("Schema id attribute validity")
struct SchemaIDAttributeTests {
private func compile(_ body: String) throws {
_ = try PureXML.Schema.Document("""
\(body)
""")
}
private func rejects(_ body: String) -> Bool {
do { try compile(body)
return false
} catch { return true }
}
@Test("valid, unique id attributes compile")
func test_valid() throws {
try compile(#""#)
try compile(#"""
"""#)
}
@Test("an id value that is not a valid NCName is rejected")
func test_idNotNCName() {
#expect(rejects(#""#))
#expect(rejects(#""#))
#expect(rejects(#""#))
#expect(rejects(#""#))
}
@Test("a duplicated id value in the schema document is rejected")
func test_duplicateId() {
#expect(rejects(#""#))
// A duplicate across different component kinds (element and a constraint).
#expect(rejects(#"""
"""#))
}
@Test("foreign id attributes are not treated as xs:ID")
func test_foreignIdsIgnored() throws {
// Foreign content inside xs:documentation carries its own non-xs:ID `id`
// attributes; repeated or non-NCName values there must not reject the schema.
try compile(#"""
"""#)
// A prefixed xml:id is a different attribute, not the schema-component id.
try compile(#""#)
}
@Test("the rejection message names the id problem")
func test_message() {
do {
try compile(#""#)
Issue.record("expected a malformed id to be rejected")
} catch {
#expect(String(describing: error).contains("id attribute"))
}
}
}