示例工程 07-optionals

07-optionals/Tests/Ch07KitTests/ErrorLossTests.swift

swift · 1.2 KB

import Testing
 
@testable import Ch07Kit
 
@Suite("错误与嵌套")
struct ErrorLossTests {
    @Test func tryQuestionLosesReason() {
        #expect(ErrorLoss.withTryQuestion("") == nil)
        #expect(ErrorLoss.withTryQuestion("x") == nil)
        #expect(ErrorLoss.withTryQuestion("3") == 3)
    }
 
    @Test func doCatchKeepsReason() {
        #expect(ErrorLoss.reason("") == "empty")
        #expect(ErrorLoss.reason("x") == "notANumber(x)")
        #expect(ErrorLoss.reason("3") == "ok")
    }
 
    @Test func nestedOptionalDistinguishesTwoLevels() {
        #expect(NestedOptional.describe(NestedOptional.someNil) == "outer.some(inner.none)")
        #expect(NestedOptional.describe(NestedOptional.none) == "outer.none")
        #expect(NestedOptional.describe(.some(.some(7))) == "outer.some(inner.some(7))")
    }
 
    @Test func flattenKeepsInnerNil() {
        #expect(NestedOptional.flattenOneLevel(NestedOptional.someNil) == nil)
        #expect(NestedOptional.flattenOneLevel(.some(.some(7))) == 7)
        #expect(NestedOptional.flattenOneLevel(.none) == nil)
    }
 
    @Test func dictionaryDoubleOptionalIsFlattened() {
        #expect(NestedOptional.dictionaryLookup() == "1/nil/nil")
    }
}