示例工程 10-protocols
10-protocols/Tests/Ch10KitTests/DispatchTests.swift
import Testing
@testable import Ch10Kit
@Suite("默认实现与派发")
struct DispatchTests {
@Test func defaultImplementationUsedWhenNotOverridden() {
#expect(DispatchLab.greetThroughProtocol(PlainPerson()) == "default")
#expect(DispatchLab.greetThroughGeneric(PlainPerson()) == "default")
}
@Test func concreteImplementationWinsThroughProtocol() {
#expect(DispatchLab.greetThroughProtocol(CustomPerson()) == "custom")
#expect(DispatchLab.greetThroughGeneric(CustomPerson()) == "custom")
}
@Test func extensionOnlyMethodIsStaticallyDispatched() {
#expect(DispatchLab.workConcrete() == "concrete")
#expect(DispatchLab.workThroughProtocol() == "extension")
}
}