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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
@testable import Grain
import XCTest
final class PhotoModelsTests: GrainTestCase {
// MARK: - Helpers
private func makeExif(
make: String? = nil,
model: String? = nil,
lensMake: String? = nil,
lensModel: String? = nil,
focalLength: String? = nil,
fNumber: String? = nil,
exposureTime: String? = nil,
iso: Int? = nil
) -> GrainExif {
GrainExif(
uri: "at://test/photo.exif/1",
cid: "cid",
photo: "at://test/photo/1",
createdAt: "2024-01-01T00:00:00Z",
exposureTime: exposureTime,
fNumber: fNumber,
focalLengthIn35mmFormat: focalLength,
iSO: iso,
lensMake: lensMake,
lensModel: lensModel,
make: make,
model: model
)
}
// MARK: - cameraName
func testCameraNameWithMakeAndModel() {
let exif = makeExif(make: "Sony", model: "A7III")
XCTAssertEqual(exif.cameraName, "Sony A7III")
}
func testCameraNameWithOnlyMake() {
let exif = makeExif(make: "Canon")
XCTAssertEqual(exif.cameraName, "Canon")
}
func testCameraNameWithOnlyModel() {
let exif = makeExif(model: "X100V")
XCTAssertEqual(exif.cameraName, "X100V")
}
func testCameraNameNilWhenBothMissing() {
let exif = makeExif()
XCTAssertNil(exif.cameraName)
}
func testCameraNameFiltersEmptyStrings() {
let exif = makeExif(make: "", model: "A7III")
XCTAssertEqual(exif.cameraName, "A7III")
}
// MARK: - lensName
func testLensNamePrefersLensModel() {
let exif = makeExif(lensMake: "Sigma", lensModel: "35mm f/1.4")
XCTAssertEqual(exif.lensName, "35mm f/1.4")
}
func testLensNameNilWhenMissing() {
let exif = makeExif()
XCTAssertNil(exif.lensName)
}
func testLensNameWithEmptyLensModel() {
let exif = makeExif(lensMake: "Sigma", lensModel: "")
// Empty lensModel → falls to joined path, but lensModel is empty so only lensMake
XCTAssertEqual(exif.lensName, "Sigma")
}
// MARK: - settingsLine
func testSettingsLineAllPresent() {
// settingsLine now routes focalLength + fNumber + exposureTime through their
// formatter helpers, which always emit "Nmm", "f/N", and "1/Ns" forms.
let exif = makeExif(focalLength: "35mm", fNumber: "f/1.4", exposureTime: "1/250", iso: 400)
XCTAssertEqual(exif.settingsLine, "35mm · f/1.4 · 1/250s · ISO 400")
}
func testSettingsLinePartial() {
let exif = makeExif(fNumber: "f/2.8", iso: 100)
XCTAssertEqual(exif.settingsLine, "f/2.8 · ISO 100")
}
func testSettingsLineNilWhenEmpty() {
let exif = makeExif()
XCTAssertNil(exif.settingsLine)
}
// MARK: - hasDisplayableData
func testHasDisplayableDataTrue() {
let exif = makeExif(make: "Sony")
XCTAssertTrue(exif.hasDisplayableData)
}
func testHasDisplayableDataFalse() {
let exif = makeExif()
XCTAssertFalse(exif.hasDisplayableData)
}
// MARK: - formatAperture
func testFormatApertureWholeNumberDropsZero() {
XCTAssertEqual(formatAperture(2.0), "f/2")
}
func testFormatApertureOneDecimal() {
XCTAssertEqual(formatAperture(2.5), "f/2.5")
}
func testFormatApertureTwoDecimals() {
XCTAssertEqual(formatAperture(2.83), "f/2.83")
}
func testFormatApertureRoundsToTwoDecimals() {
XCTAssertEqual(formatAperture(2.834), "f/2.83")
XCTAssertEqual(formatAperture(2.836), "f/2.84")
}
func testFormatApertureSmallValue() {
XCTAssertEqual(formatAperture(1.4), "f/1.4")
}
// MARK: - formatShutterSpeed
func testFormatShutterSpeedFastFraction() {
XCTAssertEqual(formatShutterSpeed(seconds: 0.002), "1/500s")
XCTAssertEqual(formatShutterSpeed(seconds: 0.001), "1/1000s")
}
func testFormatShutterSpeedHalfSecond() {
XCTAssertEqual(formatShutterSpeed(seconds: 0.5), "1/2s")
}
func testFormatShutterSpeedRoundsDenominator() {
// 1/249.something should round to 1/250
XCTAssertEqual(formatShutterSpeed(seconds: 1.0 / 249.7), "1/250s")
}
func testFormatShutterSpeedSlowWholeSecond() {
XCTAssertEqual(formatShutterSpeed(seconds: 1.0), "1s")
XCTAssertEqual(formatShutterSpeed(seconds: 2.0), "2s")
XCTAssertEqual(formatShutterSpeed(seconds: 30.0), "30s")
}
func testFormatShutterSpeedSlowPreservesHalfSecond() {
// Slow shutters keep one decimal of precision so 1.5s, 2.5s, etc. survive.
XCTAssertEqual(formatShutterSpeed(seconds: 1.5), "1.5s")
XCTAssertEqual(formatShutterSpeed(seconds: 2.5), "2.5s")
}
func testFormatShutterSpeedZeroOrNegative() {
XCTAssertEqual(formatShutterSpeed(seconds: 0), "0s")
XCTAssertEqual(formatShutterSpeed(seconds: -1), "0s")
}
// MARK: - formatFocalLength
func testFormatFocalLengthWholeNumber() {
XCTAssertEqual(formatFocalLength(mm: 35.0), "35mm")
}
func testFormatFocalLengthRoundsUp() {
XCTAssertEqual(formatFocalLength(mm: 50.5), "51mm")
}
func testFormatFocalLengthRoundsDown() {
XCTAssertEqual(formatFocalLength(mm: 24.4), "24mm")
}
// MARK: - formattedFNumber
func testFormattedFNumberStripsPrefix() {
let exif = makeExif(fNumber: "f/2.0")
XCTAssertEqual(exif.formattedFNumber, "f/2")
}
func testFormattedFNumberWithoutPrefix() {
let exif = makeExif(fNumber: "2.5")
XCTAssertEqual(exif.formattedFNumber, "f/2.5")
}
func testFormattedFNumberWithDecimals() {
let exif = makeExif(fNumber: "f/2.83")
XCTAssertEqual(exif.formattedFNumber, "f/2.83")
}
func testFormattedFNumberNilWhenMissing() {
let exif = makeExif()
XCTAssertNil(exif.formattedFNumber)
}
func testFormattedFNumberFallsBackOnGarbage() {
let exif = makeExif(fNumber: "bulb")
XCTAssertEqual(exif.formattedFNumber, "bulb")
}
// MARK: - formattedExposureTime
func testFormattedExposureTimeWithDecimalDenominator() {
let exif = makeExif(exposureTime: "1/500.0")
XCTAssertEqual(exif.formattedExposureTime, "1/500s")
}
func testFormattedExposureTimeWithSuffix() {
let exif = makeExif(exposureTime: "1/250s")
XCTAssertEqual(exif.formattedExposureTime, "1/250s")
}
func testFormattedExposureTimeAsDecimalSeconds() {
let exif = makeExif(exposureTime: "0.002")
XCTAssertEqual(exif.formattedExposureTime, "1/500s")
}
func testFormattedExposureTimeWholeSecond() {
let exif = makeExif(exposureTime: "2.0s")
XCTAssertEqual(exif.formattedExposureTime, "2s")
}
func testFormattedExposureTimeNilWhenMissing() {
let exif = makeExif()
XCTAssertNil(exif.formattedExposureTime)
}
func testFormattedExposureTimeRejectsZeroDenominator() {
let exif = makeExif(exposureTime: "1/0")
// 1/0 is invalid; we should pass through the original string rather than
// produce a divide-by-zero result.
XCTAssertEqual(exif.formattedExposureTime, "1/0")
}
func testFormattedExposureTimeFallsBackOnGarbage() {
let exif = makeExif(exposureTime: "bulb")
XCTAssertEqual(exif.formattedExposureTime, "bulb")
}
func testFormattedExposureTimePreservesSecAfterTrim() {
// The string "1/500 sec" should NOT get mangled into "1/500 ec" — only the
// single trailing "s" is stripped.
let exif = makeExif(exposureTime: "1/500 sec")
// With the safe trimming, "sec" doesn't end in just "s" cleanly — it does
// end in "c", so suffix-trim of "s" doesn't apply. The string falls through
// to the parser which can't parse "1/500 sec", so we return the original.
XCTAssertEqual(exif.formattedExposureTime, "1/500 sec")
}
// MARK: - formattedFocalLength
func testFormattedFocalLengthStripsSuffix() {
let exif = makeExif(focalLength: "35.0mm")
XCTAssertEqual(exif.formattedFocalLength, "35mm")
}
func testFormattedFocalLengthWithoutSuffix() {
let exif = makeExif(focalLength: "50.0")
XCTAssertEqual(exif.formattedFocalLength, "50mm")
}
func testFormattedFocalLengthRounds() {
let exif = makeExif(focalLength: "23.6mm")
XCTAssertEqual(exif.formattedFocalLength, "24mm")
}
func testFormattedFocalLengthNilWhenMissing() {
let exif = makeExif()
XCTAssertNil(exif.formattedFocalLength)
}
// MARK: - settingsLine uses formatted versions
func testSettingsLineUsesFormattedFocalLength() {
let exif = makeExif(focalLength: "35.0mm", iso: 100)
XCTAssertEqual(exif.settingsLine, "35mm · ISO 100")
}
func testSettingsLineUsesFormattedExposureTime() {
let exif = makeExif(exposureTime: "1/500.0", iso: 100)
XCTAssertEqual(exif.settingsLine, "1/500s · ISO 100")
}
func testSettingsLineFullyFormatted() {
let exif = makeExif(
focalLength: "35.0mm",
fNumber: "f/2.0",
exposureTime: "1/500.0",
iso: 400
)
XCTAssertEqual(exif.settingsLine, "35mm · f/2 · 1/500s · ISO 400")
}
}