DayByDayTests/SortRoundTests.swift
62 lines · 2357 bytes
8 symbols in this file
1import Testing
2@testable import DayByDay
3
4struct SortRoundTests {
5 func round(_ seed: UInt64 = 91) -> SortRound {
6 var rng = SeededGenerator(seed: seed)
7 return SortRound(using: &rng)
8 }
9
10 @Test func threeOfEach() {
11 for seed in 0..<50 {
12 let r = round(UInt64(seed))
13 #expect(r.foods.filter(\.isFruit).count == 3)
14 #expect(r.foods.filter { !$0.isFruit }.count == 3)
15 #expect(Set(r.foods).count == 6)
16 }
17 }
18
19 @Test func correctBinAdvances() {
20 var r = round()
21 let first = r.current!
22 #expect(r.place(isFruit: first.isFruit) == .correct)
23 #expect((first.isFruit ? r.fruitBin : r.vegetableBin) == [first])
24 #expect(r.current == r.foods[1])
25 }
26
27 @Test func wrongBinWaits() {
28 var r = round()
29 let first = r.current!
30 #expect(r.place(isFruit: !first.isFruit) == .incorrect(correctName: first.name))
31 #expect(r.place(isFruit: first.isFruit) == .ignored)
32 r.resume()
33 #expect(r.place(isFruit: first.isFruit) == .correct)
34 }
35
36 @Test func doneIgnoresInput() {
37 var r = round()
38 while let food = r.current { _ = r.place(isFruit: food.isFruit) }
39 #expect(r.isDone)
40 #expect(r.place(isFruit: true) == .ignored)
41 #expect(r.fruitBin.count + r.vegetableBin.count == 6)
42 }
43
44 @Test func promptAndCorrection() {
45 var r = round()
46 let food = r.current!
47 #expect(r.prompt == SortRound.question(for: food))
48 _ = r.place(isFruit: !food.isFruit)
49 #expect(r.correction().hasSuffix("is a \(food.isFruit ? "fruit" : "vegetable"). Try again!"))
50 #expect(SortRound.fact(for: .apple) == "An apple is a fruit.")
51 #expect(SortRound.fact(for: .carrot) == "A carrot is a vegetable.")
52 }
53
54 @Test func grammarForEveryFood() {
55 #expect(SortRound.fact(for: .grapes) == "Grapes are a fruit.")
56 #expect(SortRound.fact(for: .corn) == "Corn is a vegetable.")
57 #expect(SortRound.fact(for: .broccoli) == "Broccoli is a vegetable.")
58 #expect(SortRound.question(for: .grapes) == "Are grapes a fruit or a vegetable?")
59 #expect(SortRound.question(for: .corn) == "Is corn a fruit or a vegetable?")
60 #expect(SortRound.question(for: .apple) == "Is the apple a fruit or a vegetable?")
61 }
62}