DayByDayTests/GameTests.swift
175 lines · 8383 bytes
16 symbols in this file
1import Testing
2import UIKit
3@testable import DayByDay
4
5struct GameTests {
6 typealias Topic = DayByDay.Category
7
8 @Test func catalog() {
9 for c in Topic.gridCases {
10 #expect(c.games.first == .findIt)
11 }
12 #expect(Topic.gridCases.filter { $0.games.contains(.oddOneOut) } == [.shapes, .animals, .food])
13 #expect(Topic.gridCases.filter { $0.games.contains(.listen) } == [.numbers, .animals, .alphabet, .weather])
14 #expect(Topic.days.games == [.findIt, .comesNext, .weekTrain, .yesterdayTomorrow])
15 #expect(Topic.months.games == [.findIt, .comesNext, .yearWheel])
16 #expect(Topic.seasons.games == [.findIt, .whichSeason])
17 #expect(Topic.colors.games == [.findIt, .mixColors])
18 #expect(Topic.shapes.games == [.findIt, .oddOneOut, .trace])
19 #expect(Topic.alphabet.games == [.findIt, .listen, .trace])
20 #expect(Topic.weather.games == [.findIt, .listen, .dressUp])
21 #expect(Topic.food.games == [.findIt, .oddOneOut, .sortFood])
22 #expect(Topic.bodyParts.games == [.findIt, .moves])
23 #expect(Topic.numbers.games == [.findIt, .listen, .counting, .most])
24 }
25
26 @Test(arguments: Topic.gridCases) func everyGameMakesValidQuestions(_ c: Topic) {
27 var rng = SeededGenerator(seed: 21)
28 for game in c.games where game.usesRound {
29 let source = c.questionSource(for: game)
30 var previous: QuizItem?
31 for i in 0..<300 {
32 let q = source.next(correctCount: i % 5, excluding: previous, using: &rng)
33 #expect(q.choices.contains(q.answer), "\(c) \(game)")
34 #expect(Set(q.choices).count == q.choices.count, "\(c) \(game)")
35 #expect((3...4).contains(q.choices.count), "\(c) \(game)")
36 #expect(q.answer != previous, "\(c) \(game)")
37 #expect(q.choices.allSatisfy { $0.category == c }, "\(c) \(game)")
38 #expect(!q.prompt.isEmpty)
39 previous = q.answer
40 }
41 }
42 }
43
44 static func group(_ item: QuizItem) -> Bool {
45 if let a = item.value(LearnAnimal.self) { return a.canFly }
46 if let f = item.value(LearnFood.self) { return f.isFruit }
47 return item.value(LearnShape.self)!.hasCorners!
48 }
49
50 @Test(arguments: [Topic.shapes, .animals, .food]) func oddOneOutHasExactlyOneOddChoice(_ c: Topic) {
51 var rng = SeededGenerator(seed: 22)
52 let source = c.questionSource(for: .oddOneOut)
53 for i in 0..<300 {
54 let q = source.next(correctCount: i % 5, excluding: nil, using: &rng)
55 let odd = Self.group(q.answer)
56 #expect(q.choices.filter { Self.group($0) == odd } == [q.answer], "\(c)")
57 #expect(q.facts.count == q.choices.count)
58 }
59 }
60
61 @Test func oddOneOutLeavesOutHeart() {
62 var rng = SeededGenerator(seed: 23)
63 let heart = Topic.shapes.item(LearnShape.heart)
64 for _ in 0..<300 {
65 #expect(!Topic.shapes.questionSource(for: .oddOneOut)
66 .next(correctCount: 4, excluding: nil, using: &rng).choices.contains(heart))
67 }
68 }
69
70 @Test func oddOneOutFacts() {
71 var rng = SeededGenerator(seed: 24)
72 let q = Topic.food.questionSource(for: .oddOneOut).next(correctCount: 0, excluding: nil, using: &rng)
73 for item in q.choices {
74 #expect(q.facts[item] == SortRound.fact(for: item.value(LearnFood.self)!))
75 }
76 }
77
78 func questions(_ c: Topic, _ game: Game, count: Int = 300) -> [Question] {
79 var rng = SeededGenerator(seed: 25)
80 let source = c.questionSource(for: game)
81 return (0..<count).map { source.next(correctCount: $0 % 5, excluding: nil, using: &rng) }
82 }
83
84 @Test func listenCues() {
85 let animals = questions(.animals, .listen)
86 #expect(animals.allSatisfy { $0.sound != nil && $0.prompt == "Who says this?" })
87 #expect(Set(animals.map(\.answer)).count == 7)
88 let cow = animals.first { $0.answer.value(LearnAnimal.self) == .cow }!
89 #expect(cow.sound == .moo)
90
91 let weather = questions(.weather, .listen)
92 let sounds: [LearnWeather: SoundEffect] = [.rainy: .rain, .windy: .wind, .stormy: .thunder]
93 #expect(weather.allSatisfy { sounds[$0.answer.value(LearnWeather.self)!] == $0.sound })
94 #expect(weather.allSatisfy { $0.prompt == "What weather sounds like this?" })
95
96 let numbers = questions(.numbers, .listen)
97 #expect(numbers.allSatisfy { $0.sound == .beeps($0.answer.value(LearnNumber.self)!.numeral) })
98 #expect(numbers.allSatisfy { $0.answer.value(LearnNumber.self)!.numeral <= 5 && $0.prompt == "How many beeps?" })
99
100 let letters = questions(.alphabet, .listen)
101 let excluded: Set<LearnLetter> = [.k, .q, .x]
102 #expect(letters.allSatisfy { q in q.sound == nil && q.choices.allSatisfy { !excluded.contains($0.value(LearnLetter.self)!) } })
103 let b = letters.first { $0.answer.value(LearnLetter.self) == .b }!
104 #expect(b.prompt == "Which letter does ball start with?")
105 }
106
107 @Test func findItPromptCopy() {
108 func prompt(_ c: Topic, _ value: some CaseIterable) -> String {
109 var rng = SeededGenerator(seed: 26)
110 let target = c.item(value)
111 let source = c.questionSource(for: .findIt)
112 while true {
113 let q = source.next(correctCount: 0, excluding: nil, using: &rng)
114 if q.answer == target { return q.prompt }
115 }
116 }
117 #expect(prompt(.alphabet, LearnLetter.b) == "Can you find the letter b?")
118 #expect(prompt(.numbers, LearnNumber.three) == "Can you find the number three?")
119 #expect(prompt(.animals, LearnAnimal.cow) == "Can you find the cow?")
120 #expect(prompt(.bodyParts, LearnBodyPart.arms) == "Can you find the arms?")
121 #expect(prompt(.days, DayOfWeek.tuesday) == "Can you find Tuesday?")
122 #expect(prompt(.colors, LearnColor.red) == "Can you find Red?")
123 }
124
125 @Test func titles() {
126 #expect(Game.allCases.map(\.title) == ["Find it!", "Odd one out", "Listen",
127 "What comes next?", "Week train", "Yesterday and tomorrow",
128 "Year wheel", "Which season?", "Count with me", "Which has the most?",
129 "Mix colors", "Trace it", "Dress for the weather", "Fruit or vegetable?", "Move your body"])
130 }
131
132 /// The prompt names the rule, so only one choice fits it.
133 @Test func oddOneOutPromptsNameTheRule() {
134 let expected: [(Topic, (QuizItem) -> String)] = [
135 (.animals, { $0.value(LearnAnimal.self)!.canFly ? "Which one can fly?" : "Which one can't fly?" }),
136 (.food, { $0.value(LearnFood.self)!.isFruit ? "Which one is a fruit?" : "Which one is a vegetable?" }),
137 (.shapes, { $0.value(LearnShape.self)!.hasCorners! ? "Which one has corners?" : "Which one is round?" }),
138 ]
139 for (c, prompt) in expected {
140 for q in questions(c, .oddOneOut) {
141 #expect(q.prompt == prompt(q.answer), "\(c)")
142 }
143 }
144 }
145
146 /// A storm is rain and wind to a child, so it is never offered beside those cues.
147 @Test func stormyNotOfferedForRainOrWind() {
148 let stormy = Topic.weather.item(LearnWeather.stormy)
149 for q in questions(.weather, .listen) where q.answer != stormy {
150 #expect(!q.choices.contains(stormy))
151 }
152 }
153}
154
155struct GameSymbolTests {
156 @Test(arguments: Game.allCases) func symbolExists(_ game: Game) {
157 #expect(UIImage(systemName: game.symbol) != nil)
158 }
159}
160
161struct OddOneOutFoodFactTests {
162 /// Food facts use the same grammar as the sorting game ("Grapes are a fruit.").
163 @Test func foodFactsAreGrammatical() {
164 var rng = SeededGenerator(seed: 77)
165 let grapes = DayByDay.Category.food.item(LearnFood.grapes)
166 var seen = false
167 for i in 0..<500 {
168 let q = DayByDay.Category.food.questionSource(for: .oddOneOut).next(correctCount: i % 5, excluding: nil, using: &rng)
169 guard q.choices.contains(grapes), q.answer != grapes else { continue }
170 #expect(q.correction(for: grapes) == "Grapes are a fruit. Try again!")
171 seen = true
172 }
173 #expect(seen)
174 }
175}