PureXML/Tests/RegexCategoryTests.swift
Mihaela Mihaljevic be8a39394d fix(schema): advance XSTS valid-instances-rejected to 4 (#146)
Consolidates the in-progress #146 work; valid-instances-rejected reaches 4
on the 2006-11-06 XSTS archive (buckets now 1/262/4/147).

Regex: match Unicode scalars rather than grapheme clusters; the empty
pattern matches only the empty string; pin the category repertoire to
Unicode 3.2, the version the corpus is consistent at, so code points added
in 3.2 (Tagalog, Cyrillic Komi) are word characters while later additions
(the Tamil digit zero) stay unassigned and match \D. New XSDCategory and a
3.2 assignment table derived from DerivedAge, replacing the old Unicode31
files.

Content model: assess each child against the particle the content automaton
matched, not a by-name lookup (#180). Two same-named particles keep their
own fixed values, and an element matching a skip wildcard is no longer
validated against its global declaration. Selection is made reproducible
when an ambiguous schema slips past the UPA check.

XSTS runner: compile and merge every schemaDocument in a schemaTest, not
just the first; this also surfaced one previously-masked invalid-instance
acceptance (recorded in the baselines).

Also folds in attribute-group anyAttribute inheritance, extension wildcard
union, namespace-qualified type tables for multi-document
xsi:schemaLocation, per-container elementFormDefault, strict and lax
attribute wildcard validation, built-in type resolution, and xs:redefine
simpleType overrides.
2026-06-16 22:22:05 +02:00

84 lines
2.9 KiB
Swift

@testable import PureXML
import Testing
@Suite("Regex Unicode categories and class subtraction")
struct RegexCategoryTests {
private func matches(_ pattern: String, _ string: String) throws -> Bool {
try PureXML.Regex.matches(pattern, string)
}
@Test("\\p{L} matches a letter group and rejects non-letters")
func test_letterGroup() throws {
#expect(try matches("\\p{L}+", "abcÅ"))
#expect(try !matches("\\p{L}+", "ab1"))
}
@Test("\\p{Lu} and \\p{Ll} distinguish case")
func test_caseCategories() throws {
#expect(try matches("\\p{Lu}\\p{Ll}+", "Hello"))
#expect(try !matches("\\p{Lu}\\p{Ll}+", "hello"))
}
@Test("\\p{Nd} matches decimal digits")
func test_decimalNumber() throws {
#expect(try matches("\\p{Nd}+", "2026"))
#expect(try !matches("\\p{Nd}+", "12a"))
}
@Test("\\P{...} negates a category")
func test_negatedCategory() throws {
#expect(try matches("\\P{L}+", "123 !"))
#expect(try !matches("\\P{L}+", "12a"))
}
@Test("A category inside a character class composes with ranges")
func test_categoryInClass() throws {
// A letter or an underscore.
#expect(try matches("[\\p{L}_]+", "a_b"))
#expect(try !matches("[\\p{L}_]+", "a-b"))
}
@Test("\\p{IsBasicLatin} tests a Unicode block")
func test_block() throws {
#expect(try matches("\\p{IsBasicLatin}+", "abc"))
#expect(try !matches("\\p{IsBasicLatin}+", "abç"))
}
@Test("\\p{IsArabic} tests the Arabic block")
func test_arabicBlock() throws {
#expect(try matches("\\p{IsArabic}+", "\u{0627}\u{0628}"))
// Adjacent Arabic block scalars that Swift fuses into one grapheme cluster.
#expect(try matches("\\p{IsArabic}+", "\u{0600}\u{0601}"))
// Base letter plus combining mark: two XSD characters, one grapheme.
#expect(try matches("\\p{IsArabic}+", "\u{0627}\u{064B}"))
#expect(try !matches("\\p{IsArabic}+", "abc"))
}
@Test("An unknown category name is a compile error")
func test_unknownCategory() {
#expect(throws: PureXML.Regex.RegexError.self) {
_ = try PureXML.Regex.Pattern("\\p{Zz}")
}
}
@Test("Character-class subtraction removes the subtrahend")
func test_subtraction() throws {
#expect(try matches("[a-z-[aeiou]]+", "bcdfg"))
#expect(try !matches("[a-z-[aeiou]]+", "bce"))
}
@Test("Subtraction nests")
func test_nestedSubtraction() throws {
// a-z, minus (b-d minus c): so b and d are removed, c stays.
#expect(try matches("[a-z-[b-d-[c]]]+", "acez"))
#expect(try !matches("[a-z-[b-d-[c]]]+", "abz"))
}
@Test("Subtraction composes with a category subtrahend")
func test_subtractCategory() throws {
// Any letter that is not lowercase.
#expect(try matches("[\\p{L}-[\\p{Ll}]]+", "ABC"))
#expect(try !matches("[\\p{L}-[\\p{Ll}]]+", "ABc"))
}
}