import Foundation import Testing @testable import Ch29Kit @Suite("问候与时钟") struct GreetingTests { @Test( arguments: [ (0, "Good night, Ada"), (5, "Good night, Ada"), (6, "Good morning, Ada"), (11, "Good morning, Ada"), (12, "Good afternoon, Ada"), (17, "Good afternoon, Ada"), (18, "Good evening, Ada"), (23, "Good evening, Ada"), ]) func greetingAtBoundaries(hour: Int, expected: String) { #expect(Greeting.text(for: "Ada", hour: hour) == expected) } @Test func injectedClockIsDeterministic() { #expect(Scheduler.greeting(name: "Ada", time: FixedTime(hour: 9)) == "Good morning, Ada") #expect(Scheduler.greeting(name: "Ada", time: FixedTime(hour: 21)) == "Good evening, Ada") } @Test func parseAcceptsOnlyPositiveNumbers() throws { let value = try #require(Parsing.positive("42")) #expect(value == 42) #expect(Parsing.positive("-1") == nil) #expect(Parsing.positive("abc") == nil) #expect(Parsing.positive("0") == nil) } @Test func parsingNilDoesNotThrow() { #expect(Parsing.positive("nope") == nil) } @Test @MainActor func greetingIsCallableOnMainActor() { #expect(Greeting.text(for: "Ada", hour: 12) == "Good afternoon, Ada") } } @Suite("快照") struct SnapshotTests { @Test func reportMatchesCommittedSnapshot() throws { let items = [ Item(name: "bob", score: 80), Item(name: "ada", score: 95), Item(name: "cy", score: 88), ] let rendered = Report.render(items) let testFile = URL(fileURLWithPath: #filePath) let root = testFile .deletingLastPathComponent() .deletingLastPathComponent() .deletingLastPathComponent() let expected = try String( contentsOf: root.appendingPathComponent("expected/snapshot.txt"), encoding: .utf8 ) #expect(rendered == expected.trimmingCharacters(in: .newlines)) } @Test func reportIsSortedByName() { let rendered = Report.render([ Item(name: "z", score: 1), Item(name: "a", score: 2), ]) #expect(rendered == "a: 2\nz: 1") } }