DayByDayTests/OrderSourcesTests.swift
90 lines · 3991 bytes
11 symbols in this file
1import Testing
2@testable import DayByDay
3
4struct OrderSourcesTests {
5 typealias Topic = DayByDay.Category
6
7 func asked(_ q: Question) -> String {
8 let after = q.prompt.components(separatedBy: "after ").last!
9 return String(after.dropLast())
10 }
11
12 @Test func monthSeasons() {
13 #expect([MonthOfYear.december, .january, .february].allSatisfy { $0.season == .winter })
14 #expect(MonthOfYear.march.season == .spring)
15 #expect(MonthOfYear.june.season == .summer)
16 #expect([MonthOfYear.september, .november].allSatisfy { $0.season == .fall })
17 }
18
19 @Test func comesNextWraps() {
20 var rng = SeededGenerator(seed: 31)
21 let days = ComesNext(items: Topic.days.quizItems, noun: "day")
22 let months = ComesNext(items: Topic.months.quizItems, noun: "month")
23 var sawSaturday = false, sawDecember = false
24 for i in 0..<300 {
25 let d = days.next(correctCount: i % 5, excluding: nil, using: &rng)
26 if asked(d) == "Saturday" { sawSaturday = true; #expect(d.answer.name == "Sunday") }
27 let m = months.next(correctCount: i % 5, excluding: nil, using: &rng)
28 if asked(m) == "December" { sawDecember = true; #expect(m.answer.name == "January") }
29 }
30 #expect(sawSaturday && sawDecember)
31 }
32
33 @Test func comesNextPrompt() {
34 var rng = SeededGenerator(seed: 32)
35 let q = ComesNext(items: Topic.days.quizItems, noun: "day").next(correctCount: 0, excluding: nil, using: &rng)
36 #expect(q.prompt == "What day comes after \(asked(q))?")
37 }
38
39 @Test func comesNextNeverOffersAskedItem() {
40 var rng = SeededGenerator(seed: 33)
41 let source = ComesNext(items: Topic.days.quizItems, noun: "day")
42 var previous: QuizItem?
43 for i in 0..<300 {
44 let q = source.next(correctCount: i % 5, excluding: previous, using: &rng)
45 #expect(!q.choices.map(\.name).contains(asked(q)))
46 #expect(q.answer != previous)
47 previous = q.answer
48 }
49 }
50
51 @Test func comesNextCorrection() {
52 var rng = SeededGenerator(seed: 34)
53 let q = ComesNext(items: Topic.days.quizItems, noun: "day").next(correctCount: 0, excluding: nil, using: &rng)
54 let wrong = q.choices.first { $0 != q.answer }!
55 #expect(q.correction(for: wrong) == "\(q.answer.name) comes after \(asked(q)). Try again!")
56 }
57
58 @Test func whichSeasonPromptMatchesAnswer() {
59 var rng = SeededGenerator(seed: 35)
60 for i in 0..<300 {
61 let q = WhichSeason().next(correctCount: i % 5, excluding: nil, using: &rng)
62 let monthName = q.prompt.replacingOccurrences(of: "Which season is ", with: "")
63 .replacingOccurrences(of: " in?", with: "")
64 let month = MonthOfYear.allCases.first { $0.name == monthName }!
65 #expect(Topic.seasons.item(month.season) == q.answer)
66 let wrong = q.choices.first { $0 != q.answer }!
67 #expect(q.correction(for: wrong) == "\(monthName) is in \(q.answer.name). Try again!")
68 }
69 }
70
71 @Test func mostAnswerIsLargest() {
72 var rng = SeededGenerator(seed: 36)
73 var previous: QuizItem?
74 for i in 0..<300 {
75 let q = Most().next(correctCount: i % 5, excluding: previous, using: &rng)
76 let answer = q.answer.value(LearnNumber.self)!.numeral
77 #expect(q.choices.filter { $0 != q.answer }.allSatisfy { $0.value(LearnNumber.self)!.numeral < answer })
78 #expect(Set(q.choices).count == q.choices.count)
79 #expect(q.answer != previous)
80 previous = q.answer
81 }
82 }
83
84 @Test func mostFacts() {
85 var rng = SeededGenerator(seed: 37)
86 let q = Most().next(correctCount: 0, excluding: nil, using: &rng)
87 let wrong = q.choices.first { $0 != q.answer }!
88 #expect(q.correction(for: wrong) == "That one has \(wrong.value(LearnNumber.self)!.numeral). Try again!")
89 }
90}