示例工程 09-classes-inheritance-init

09-classes-inheritance-init/Tests/Ch09KitTests/InitTests.swift

swift · 0.9 KB

import Testing
 
@testable import Ch09Kit
 
@Suite("初始化链")
struct InitTests {
    @Test func designatedInitRunsBaseFirst() {
        let log = LifecycleLog()
        _ = Car(brand: "A", log: log)
        #expect(log.entries == ["Vehicle.init(wheels:4)", "Car.init(brand:A)"])
    }
 
    @Test func convenienceInitDelegatesToDesignated() {
        let log = LifecycleLog()
        let car = Car(brand: "B")
        #expect(car.brand == "B")
        #expect(car.wheels == 4)
        #expect(log.entries.isEmpty)
    }
 
    @Test func subclassOverridesDescription() {
        #expect(Vehicle(wheels: 2).description == "Vehicle(wheels: 2)")
        #expect(Car(brand: "C").description == "Car(brand: C, wheels: 4)")
    }
 
    @Test func protocolExtensionUsesDynamicDescription() {
        let car: Vehicle = Car(brand: "D")
        #expect(car.summary == "summary: Car(brand: D, wheels: 4)")
    }
}