-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonSamplesTodaySummary.swift
97 lines (89 loc) · 2.21 KB
/
JsonSamplesTodaySummary.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import Testing
import Foundation
import Nutrition
struct JsonSamplesTodaySummaryTests {
@Test func printYourExampleHere() throws {
let data = try JSONEncoder.stable().encode(MacroFactorTodaySummary.yourExample())
let json = try #require(String(data: data, encoding: .utf8))
print(json)
}
@Test func writeNoConsumptionNoGoalsMet() throws {
let expectedJson = """
{
"consumed" : {
},
"remaining" : {
"water" : {
"maximum" : 3500,
"minimum" : 2000,
"target" : 2500
}
}
}
"""
let data = try JSONEncoder.stable().encode(MacroFactorTodaySummary.noGoalsMet())
let json = try #require(String(data: data, encoding: .utf8))
#expect(json == expectedJson)
}
@Test func writeConsumptionMetOrExceededSomeGoals() throws {
let expectedJson = """
{
"consumed" : {
"water" : 2500
},
"remaining" : {
"water" : {
"maximum" : 1000,
"minimum" : -500,
"target" : 0
}
}
}
"""
let data = try JSONEncoder.stable().encode(MacroFactorTodaySummary.someGoalsMetOrExceeded())
let json = try #require(String(data: data, encoding: .utf8))
#expect(json == expectedJson)
}
}
private extension MacroFactorTodaySummary {
static func yourExample() -> MacroFactorTodaySummary {
MacroFactorTodaySummary(
consumed: [
.water: 2000
],
remaining: [
.water: GoalConsumptionRemaining(
minimum: 0, // Minimum goal was 2000 mL
target: nil,
maximum: nil
)
]
)
}
static func noGoalsMet() -> MacroFactorTodaySummary {
MacroFactorTodaySummary(
consumed: [:],
remaining: [
.water: GoalConsumptionRemaining(
minimum: 2000, // Minimum goal was 2000 mL
target: 2500, // Target was 2500 mL
maximum: 3500 // Ceiling was 3500 mL
)
]
)
}
static func someGoalsMetOrExceeded() -> MacroFactorTodaySummary {
MacroFactorTodaySummary(
consumed: [
.water: 2500
],
remaining: [
.water: GoalConsumptionRemaining(
minimum: -500, // Minimum goal was 2000 mL
target: 0, // Target was 2500 mL
maximum: 1000 // Ceiling was 3500 mL
)
]
)
}
}