示例工程 07-optionals
07-optionals/Tests/Ch07KitTests/OptionalTransformTests.swift
import Testing
@testable import Ch07Kit
@Suite("map 与 flatMap")
struct OptionalTransformTests {
@Test func mapKeepsContainerShape() {
#expect(OptionalTransforms.increment(nil) == nil)
#expect(OptionalTransforms.increment(1) == 2)
}
@Test func mapOnOptionalCanNest() {
let nested = OptionalTransforms.nestedParse("12")
#expect(nested != nil)
#expect(nested! == 12)
}
@Test func flatMapFlattensOneLevel() {
#expect(OptionalTransforms.flattenedParse("12") == 12)
#expect(OptionalTransforms.flattenedParse("x") == nil)
#expect(OptionalTransforms.flattenedParse(nil) == nil)
}
@Test func compactMapDropsFailures() {
#expect(OptionalTransforms.parseAll(["1", "x", "3"]) == [1, 3])
#expect(OptionalTransforms.rawParse(["1", "x", "3"]).count == 3)
#expect(OptionalTransforms.rawParse(["1", "x", "3"])[1] == nil)
}
}