import SwiftUI import Testing @testable import Ch37Kit @Suite("弹簧曲线") struct SpringTests { private let probe = SpringProbe(response: 0.5, dampingRatio: 0.8) @Test func startsAtZero() { #expect(abs(probe.progress(at: 0)) < 0.0001) } @Test func settlesAtOne() { #expect(abs(probe.progress(at: 10) - 1) < 0.0001) } @Test func isMonotonicEarlyOn() { var previous = -1.0 for step in 0...10 { let value = probe.progress(at: Double(step) * 0.02) #expect(value >= previous) previous = value } } @Test func underdampedSpringOvershoots() { #expect(probe.overshoots(at: 0.5)) #expect(!SpringProbe(dampingRatio: 1.0).overshoots(at: 0.5)) } @Test func settlingDurationIsPositive() { #expect(probe.settlingDuration > 0) #expect(probe.settlingDuration < 2) } @Test func probeIsSmall() { #expect(MemoryLayout.size <= 32) } } @Suite("相位") struct PhaseTests { @Test func cyclesBackToIdle() { #expect(Phase.idle.next == .loading) #expect(Phase.loading.next == .done) #expect(Phase.done.next == .idle) } @Test func allCasesAreUnique() { #expect(Set(Phase.allCases).count == Phase.allCases.count) } } @Suite("视图") struct AnimationViewTests { @Test @MainActor func boardRendersAtFixedSize() throws { let image = try #require(ImageRenderer(content: AnimationBoard()).cgImage) #expect(image.width == 360) #expect(image.height > 180) } @Test @MainActor func explodeIsOffByDefault() throws { let image = try #require(ImageRenderer(content: ExpandingCard()).cgImage) #expect(image.width >= 300) #expect(image.height > 40) } } @Suite("截图") struct ScreenshotTests { @MainActor private func writeShot( _ view: some View, name: String, expectedWidth: Int, minHeight: Int ) throws { let renderer = ImageRenderer(content: view) renderer.scale = 2 let image = try #require(renderer.cgImage) #expect(image.width == expectedWidth) #expect(image.height >= minHeight) let url = URL(fileURLWithPath: #filePath) .deletingLastPathComponent() .deletingLastPathComponent() .deletingLastPathComponent() .appendingPathComponent("shots/\(name)") guard !FileManager.default.fileExists(atPath: url.path) else { return } let data = try #require( NSBitmapImageRep(cgImage: image).representation(using: .png, properties: [:]) ) try FileManager.default.createDirectory( at: url.deletingLastPathComponent(), withIntermediateDirectories: true ) try data.write(to: url) } @Test @MainActor func boardShot() throws { try writeShot( AnimationBoard(), name: "animation-board.png", expectedWidth: 720, minHeight: 300) } @Test @MainActor func expandShot() throws { try writeShot( ExpandedCardBoard(), name: "animation-expand.png", expectedWidth: 640, minHeight: 150) } @Test @MainActor func progressShot() throws { try writeShot( ProgressBar(progress: 0.6).padding(16).frame(width: 320), name: "animation-progress.png", expectedWidth: 640, minHeight: 60 ) } @Test @MainActor func springShot() throws { try writeShot( SpringCurve().frame(width: 320, height: 110), name: "animation-spring.png", expectedWidth: 640, minHeight: 200 ) } }