Stage 3 foundation of the faithful libyaml scanner port. Translates the IS_* character-class macros from yaml_private.h (IS_Z, IS_SPACE, IS_TAB, IS_BLANK, IS_BREAK, IS_BREAKZ, IS_SPACEZ, IS_BLANKZ, IS_DIGIT, IS_ALPHA, IS_HEX, IS_ASCII, IS_PRINTABLE) into PureYAML.Parsing.LibYAML.CharacterClass. libyaml inspects UTF-8 bytes; the Swift port operates on Unicode.Scalar, where the multi-byte line breaks NEL (U+0085), LS (U+2028), and PS (U+2029) are single scalars, and end of input is nil, libyaml's \0 sentinel (so isZ(nil) is true and the others are false). These predicates are what the token fetchers dispatch on. Unit-tested for end-of-input, blanks, the full break set, the alpha/digit/hex sets, and the ascii/printable boundaries.
66 lines
2.2 KiB
Swift
66 lines
2.2 KiB
Swift
@testable import PureYAML
|
|
import Testing
|
|
|
|
/// Verifies the ported character-class predicates match libyaml's `IS_*` macros,
|
|
/// including end-of-input as `nil` and the multi-byte line breaks.
|
|
@Suite("LibYAML Character Class")
|
|
struct LibYAMLCharacterClassTests {
|
|
private typealias Classify = PureYAML.Parsing.LibYAML.CharacterClass
|
|
|
|
@Test("end of input is Z and a break, nothing else")
|
|
func test_endOfInput() {
|
|
#expect(Classify.isZ(nil))
|
|
#expect(Classify.isBreakZ(nil))
|
|
#expect(Classify.isBlankZ(nil))
|
|
#expect(!Classify.isBreak(nil))
|
|
#expect(!Classify.isBlank(nil))
|
|
#expect(!Classify.isAlpha(nil))
|
|
}
|
|
|
|
@Test("blanks are space and tab only")
|
|
func test_blank() {
|
|
#expect(Classify.isSpace(" "))
|
|
#expect(Classify.isTab("\t"))
|
|
#expect(Classify.isBlank(" "))
|
|
#expect(Classify.isBlank("\t"))
|
|
#expect(!Classify.isBlank("\n"))
|
|
#expect(!Classify.isBlank("x"))
|
|
}
|
|
|
|
@Test("line breaks include CR, LF, NEL, LS, PS")
|
|
func test_break() {
|
|
for scalar in ["\r", "\n", "\u{85}", "\u{2028}", "\u{2029}"] as [Unicode.Scalar] {
|
|
#expect(Classify.isBreak(scalar))
|
|
#expect(Classify.isBreakZ(scalar))
|
|
#expect(Classify.isBlankZ(scalar))
|
|
}
|
|
#expect(!Classify.isBreak(" "))
|
|
#expect(!Classify.isBreak("x"))
|
|
}
|
|
|
|
@Test("alpha is alnum plus underscore and hyphen")
|
|
func test_alpha() {
|
|
for scalar in ["0", "9", "A", "Z", "a", "z", "_", "-"] as [Unicode.Scalar] {
|
|
#expect(Classify.isAlpha(scalar))
|
|
}
|
|
#expect(!Classify.isAlpha("."))
|
|
#expect(!Classify.isAlpha(" "))
|
|
#expect(Classify.isDigit("5"))
|
|
#expect(!Classify.isDigit("a"))
|
|
#expect(Classify.isHex("f"))
|
|
#expect(Classify.isHex("9"))
|
|
#expect(!Classify.isHex("g"))
|
|
}
|
|
|
|
@Test("ascii and printable boundaries")
|
|
func test_asciiPrintable() {
|
|
#expect(Classify.isASCII("\u{7F}"))
|
|
#expect(!Classify.isASCII("\u{80}"))
|
|
#expect(Classify.isPrintable("\t"))
|
|
#expect(Classify.isPrintable("A"))
|
|
#expect(Classify.isPrintable("\u{85}"))
|
|
#expect(!Classify.isPrintable("\u{0}"))
|
|
#expect(!Classify.isPrintable("\u{1F}"))
|
|
}
|
|
}
|