示例工程 36-lists-and-navigation

36-lists-and-navigation/Tests/Ch36KitTests/NavigationTests.swift

swift · 3.5 KB

import Foundation
import SwiftUI
import Testing
 
@testable import Ch36Kit
 
@Suite("导航路径")
struct NavigationTests {
    @Test func pushAndPop() {
        var model = NavigationModel()
        model.push(.library)
        model.push(.settings)
        #expect(model.path.count == 2)
        model.pop()
        #expect(model.path == [.library])
        model.popToRoot()
        #expect(model.path.isEmpty)
    }
 
    @Test func popOnEmptyPathIsSafe() {
        var model = NavigationModel()
        model.pop()
        #expect(model.path.isEmpty)
    }
 
    @Test func jsonRoundTripRestoresPath() throws {
        var model = NavigationModel()
        model.push(.library)
        model.push(.book(SampleData.books[0].id))
        let data = try model.encoded()
        let restored = try NavigationModel.restored(from: data)
        #expect(restored == model)
    }
 
    @Test func decodingGarbageThrows() {
        #expect(throws: (any Error).self) {
            _ = try NavigationModel.restored(from: Data("{}".utf8))
        }
    }
}
 
@Suite("列表身份")
struct IdentityTests {
    @Test func renamingKeepsTheStableID() {
        var book = SampleData.books[0]
        let id = book.id
        book.title = "新标题"
        #expect(book.id == id)
    }
 
    @Test func titleAsIdentityChangesOnRename() {
        let original = SampleData.books[0].title
        var book = SampleData.books[0]
        book.title = "新标题"
        #expect(book.title != original)
    }
 
    @Test @MainActor func staticListRendersAtFixedWidth() throws {
        let image = try #require(ImageRenderer(content: StaticBookList()).cgImage)
        #expect(image.width == 320)
        #expect(image.height > 100)
    }
}
 
@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 bookListShot() throws {
        try writeShot(StaticBookList(), name: "book-list.png", expectedWidth: 640, minHeight: 200)
    }
 
    @Test @MainActor func stackShot() throws {
        try writeShot(
            NavigationStylePreview(style: .stack), name: "navigation-stack.png",
            expectedWidth: 680, minHeight: 260)
    }
 
    @Test @MainActor func splitShot() throws {
        try writeShot(
            NavigationStylePreview(style: .split), name: "navigation-split.png",
            expectedWidth: 680, minHeight: 260)
    }
 
    @Test @MainActor func columnsShot() throws {
        try writeShot(
            NavigationStylePreview(style: .columns), name: "navigation-columns.png",
            expectedWidth: 680, minHeight: 260)
    }
}