-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonSamplesFood.swift
100 lines (91 loc) · 2.67 KB
/
JsonSamplesFood.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
98
99
100
import Testing
import Foundation
import Nutrition
struct JsonSamplesFoodTests {
@Test func printYourExampleHere() throws {
let data = try JSONEncoder.stable().encode(MacroFactorFood.yourExample())
let json = try #require(String(data: data, encoding: .utf8))
print(json)
}
@Test func parseMinimalistExample() throws {
let food = try JSONDecoder().decode(MacroFactorFood.self, from: Data(espressoMinimalJSON.utf8))
#expect(food.serving == .one)
}
@Test func parseDetailedExample() throws {
let food = try JSONDecoder().decode(MacroFactorFood.self, from: Data(espressoDetailedJSON.utf8))
#expect(food.serving == .one)
#expect(food.llmPrompt?.isEmpty == false)
}
@Test func parseCustomServingExample() throws {
let food = try JSONDecoder().decode(MacroFactorFood.self, from: Data(espressoCustomServingJSON.utf8))
#expect(food.serving == .custom(Serving.CustomServing(amount: 40.0, unit: .milliliters)))
}
}
private let espressoMinimalJSON = """
{
"icon" : "coffeeEspresso",
"name" : "Morning Coffee",
"nutrients" : {
"caffeine" : 30,
"water" : 36,
},
"serving": "one",
"source" : "my-shot-logger"
}
"""
private let espressoDetailedJSON = """
{
"barcode" : "123456789",
"beverage" : "beverage",
"brand" : "Finca Monteblanco",
"icon" : "coffeeEspresso",
"llmPrompt" : "light roast turbo espresso shot",
"name" : "Purple Caturra Washed, Extractamundo Dos",
"notes" : "Not fruity enough. Loosen grind, charge roast hotter.",
"nutrients" : {
"caffeine" : 30,
"water" : 36,
},
"serving" : "one",
"source" : "my-shot-logger"
}
"""
private let espressoCustomServingJSON = """
{
"barcode" : "123456789",
"beverage" : "beverage",
"brand" : "Finca Monteblanco",
"icon" : "coffeeEspresso",
"llmPrompt" : "light roast turbo espresso shot",
"name" : "Purple Caturra Washed, Extractamundo Dos",
"notes" : "Not fruity enough. Loosen grind, charge roast hotter.",
"nutrients" : {
"caffeine" : 30,
"water" : 36,
},
"serving" : {
"amount" : 40,
"unit" : "milliliters"
},
"source" : "my-shot-logger"
}
"""
private extension MacroFactorFood {
static func yourExample() -> MacroFactorFood {
MacroFactorFood(
source: "my-shot-logger",
icon: .coffeeEspresso,
name: "Purple Caturra Washed, Extractamundo Dos",
nutrients: [
.caffeine: 50.0
],
serving: .custom(Serving.CustomServing(amount: 36.0, unit: .milliliters)),
llmPrompt: "light roast turbo espresso shot",
barcode: "123456789",
brand: "Finca Monteblanco",
beverage: .beverage,
notes: "Not fruity enough. Loosen grind, charge roast hotter.",
recipe: nil
)
}
}