krz/daybyday

iOS learning games for young children: days, months, seasons, numbers, letters, animals and more. education ios kids swift https://krz.sh/apps/daybyday/

DayByDayTests/MixColorsTests.swift

062549ad26607ce9e973ed2a9a7ef6f853885f83
daybyday/DayByDayTests/MixColorsTests.swift history · blame · raw

44 lines · 1754 bytes

6 symbols in this file
 1import Testing
 2@testable import DayByDay
 3
 4struct MixColorsTests {
 5    typealias Topic = DayByDay.Category
 6
 7    func color(_ item: QuizItem) -> LearnColor { item.value(LearnColor.self)! }
 8
 9    @Test func everyMixIsRight() {
10        let table: [Set<LearnColor>: LearnColor] = [
11            [.red, .yellow]: .orange, [.blue, .yellow]: .green, [.red, .blue]: .purple, [.red, .white]: .pink,
12        ]
13        var rng = SeededGenerator(seed: 61)
14        var seen = Set<LearnColor>()
15        for i in 0..<300 {
16            let q = MixColors().next(correctCount: i % 5, excluding: nil, using: &rng)
17            #expect(q.shown.count == 2)
18            #expect(table[Set(q.shown.map(color))] == color(q.answer))
19            seen.insert(color(q.answer))
20        }
21        #expect(seen == [.orange, .green, .purple, .pink])
22    }
23
24    @Test func mixNeverOffersIngredients() {
25        var rng = SeededGenerator(seed: 62)
26        var previous: QuizItem?
27        for i in 0..<300 {
28            let q = MixColors().next(correctCount: i % 5, excluding: previous, using: &rng)
29            #expect(Set(q.choices).isDisjoint(with: q.shown))
30            #expect(q.choices.contains(q.answer))
31            #expect(q.answer != previous)
32            previous = q.answer
33        }
34    }
35
36    @Test func mixPromptAndCorrection() {
37        var rng = SeededGenerator(seed: 63)
38        let q = MixColors().next(correctCount: 0, excluding: nil, using: &rng)
39        let (a, b) = (q.shown[0].name, q.shown[1].name)
40        #expect(q.prompt == "What do \(a.lowercased()) and \(b.lowercased()) make?")
41        let wrong = q.choices.first { $0 != q.answer }!
42        #expect(q.correction(for: wrong) == "\(a) and \(b.lowercased()) make \(q.answer.name.lowercased()). Try again!")
43    }
44}