← All repositories

reportage

A life-logging app for iPhone

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


Tests/Unit/ModelCodingTests.swift (1628B)

1import Foundation
2import Testing
3
4@testable import Reportage
5
6@Suite("ModelCoding")
7struct ModelCodingTests {
8
9    @Test("Response values encode as single-key objects")
10    func responseValueShape() throws {
11        let data = try ModelCoding.encode(ResponseValue.tokens(["coffee", "work"]))
12        #expect(String(decoding: data, as: UTF8.self) == #"{"tokens":["coffee","work"]}"#)
13    }
14
15    @Test(
16        "Response values round-trip",
17        arguments: [
18            ResponseValue.options(["Yes"]),
19            .tokens(["a", "b"]),
20            .number(Decimal(string: "7.25")!),
21            .text("Note\nwith a line break"),
22            .place(
23                PlaceAnswer(
24                    tokenID: "T1",
25                    coordinate: Coordinate(latitude: 1.5, longitude: -2.25),
26                    accuracy: 10
27                )
28            ),
29        ]
30    )
31    func responseValueRoundTrip(value: ResponseValue) throws {
32        #expect(
33            try ModelCoding.decode(ResponseValue.self, from: ModelCoding.encode(value)) == value)
34    }
35
36    @Test("A response value with no key or several keys is rejected")
37    func malformedResponseValue() {
38        for json in ["{}", #"{"text":"a","tokens":[]}"#] {
39            #expect(throws: DecodingError.self) {
40                try ModelCoding.decode(ResponseValue.self, from: Data(json.utf8))
41            }
42        }
43    }
44
45    @Test("Survey kinds follow report kinds")
46    func surveyKinds() {
47        #expect(SurveyKinds(surveyFor: .manual) == .random)
48        #expect(SurveyKinds(surveyFor: .wake) == .wake)
49        #expect(SurveyKinds.all.rawValue == 7)
50    }
51}