← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Tests/Unit/QuestionEditorTests.swift (4853B)

1import Foundation
2import Testing
3
4@testable import Reportage
5
6@MainActor
7@Suite("QuestionEditor")
8struct QuestionEditorTests {
9
10    private static func question(
11        _ kind: QuestionKind, options: [String] = [], rule: NarrativeRule? = nil
12    ) -> Question {
13        Question(
14            id: "Q", prompt: "Prompt?", kind: kind, options: options, narrativeRule: rule, askOn: .random,
15            order: 0, modifiedAt: Date(timeIntervalSince1970: 0))
16    }
17
18    @Test("Answers renamed in their field are renamed by old text; blank and repeated answers are dropped")
19    func renames() {
20        let editor = QuestionEditor(question: Self.question(.choice, options: ["Great", "Okay", "Bad"]))
21        editor.options[1].text = " Fine "
22        editor.options[2].text = ""
23        editor.options.append(.init(text: "Great"))
24        editor.options.append(.init(text: "Awful"))
25
26        let (question, renames) = editor.drafted()
27        #expect(question.options == ["Great", "Fine", "Awful"])
28        #expect(renames == ["Okay": "Fine"])
29
30        editor.saved(question)
31        #expect(editor.options.map(\.text) == ["Great", "Fine", "Awful"])
32        #expect(editor.drafted().renames.isEmpty)
33    }
34
35    @Test("A question with no rule keeps none until one is written, and a written rule can be cleared")
36    func rules() {
37        let editor = QuestionEditor(question: Self.question(.yesNo))
38        editor.prompt = "Raining?"
39        #expect(editor.drafted().question.narrativeRule == nil)
40
41        editor.yesPhrase = "Raining"
42        #expect(editor.drafted().question.narrativeRule == .phrases(yes: "Raining", no: ""))
43
44        let cleared = QuestionEditor(question: Self.question(.number, rule: .count(singular: "cup", plural: "cups")))
45        (cleared.singular, cleared.plural) = ("", "")
46        #expect(cleared.drafted().question.narrativeRule == .count(singular: "", plural: ""))
47    }
48
49    @Test("A list question opens the sentence or reads as its own, and new ones read as their own")
50    func listSentence() {
51        let existing = QuestionEditor(question: Self.question(.tokens))
52        #expect(existing.opensSentence)
53        #expect(existing.openedSentence)
54        #expect(existing.drafted().question.narrativeRule == nil)
55
56        existing.opensSentence = false
57        #expect(existing.drafted().question.narrativeRule == .template(format: ""))
58        existing.template = "Feeling %@"
59        let question = existing.drafted().question
60        #expect(question.narrativeRule == .template(format: "Feeling %@"))
61        let sample = NarrativeExample.samples(for: .tokens, options: [])[0]
62        #expect(NarrativeExample.narrative(of: sample, rule: question.narrativeRule) == "Feeling ….")
63
64        let reopened = QuestionEditor(question: question)
65        #expect(!reopened.opensSentence)
66        #expect(!reopened.openedSentence)
67        reopened.opensSentence = true
68        #expect(reopened.drafted().question.narrativeRule == .activities)
69
70        let added = QuestionEditor(adding: .tokens)
71        added.prompt = "Feeling?"
72        #expect(!added.opensSentence)
73        #expect(!added.openedSentence)
74        #expect(added.drafted().question.narrativeRule == .template(format: ""))
75    }
76
77    @Test("A blank prompt keeps the saved one, and a choice needs an answer to be added")
78    func completeness() {
79        let editor = QuestionEditor(adding: .choice)
80        editor.prompt = "Weather?"
81        #expect(!editor.isComplete)
82        editor.options.append(.init(text: "Sunny"))
83        #expect(editor.isComplete)
84
85        let existing = QuestionEditor(question: Self.question(.note))
86        existing.prompt = "  "
87        #expect(existing.drafted().question.prompt == "Prompt?")
88    }
89
90    @Test("Examples read as the rule writes, or not at all when it's blank")
91    func examples() {
92        let rule = NarrativeRule.count(singular: "cup", plural: "cups")
93        let samples = NarrativeExample.samples(for: .number, options: [])
94        #expect(samples.compactMap { NarrativeExample.narrative(of: $0, rule: rule) } == ["1 cup.", "2 cups."])
95        #expect(NarrativeExample.narrative(of: .number(1), rule: .count(singular: "", plural: "")) == nil)
96    }
97
98    @Test("A new question's type can be chosen, and only a choice keeps answers")
99    func newKind() {
100        let editor = QuestionEditor(adding: .tokens)
101        #expect(editor.isNew)
102        editor.prompt = "Weather?"
103        editor.kind = .choice
104        editor.options.append(.init(text: "Sunny"))
105        #expect(editor.drafted().question.kind == .choice)
106        #expect(editor.drafted().question.options == ["Sunny"])
107        editor.kind = .yesNo
108        #expect(editor.drafted().question.options.isEmpty)
109        #expect(editor.drafted().question.narrativeRule == nil)
110        #expect(!QuestionEditor(question: editor.drafted().question).isNew)
111    }
112}