示例工程 10-protocols
10-protocols/Tests/Ch10KitTests/ContainerTests.swift
import Testing
@testable import Ch10Kit
@Suite("关联类型与组合")
struct ContainerTests {
@Test func associatedTypeIsInferred() {
#expect(ContainerLab.count(IntBox([1, 2, 3])) == 3)
#expect(ContainerLab.count(StringBox(["a"])) == 1)
}
@Test func whereClauseConstrainsAssociatedType() {
#expect(ContainerLab.join(StringBox(["a", "b"])) == "a,b")
}
@Test func compositionRequiresBothProtocols() {
let person = Person(name: "ada", age: 36)
#expect(CompositionLab.describe(person) == "ada(36)")
#expect(CompositionLab.names([person]) == ["ada"])
}
}