-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata_test.go
More file actions
351 lines (326 loc) · 9.56 KB
/
metadata_test.go
File metadata and controls
351 lines (326 loc) · 9.56 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package gometadata
import (
"math"
"testing"
"time"
"github.com/FlavioCFOliveira/GoMetadata/exif"
"github.com/FlavioCFOliveira/GoMetadata/iptc"
"github.com/FlavioCFOliveira/GoMetadata/xmp"
)
// newTestMetadata builds a Metadata whose three components are all non-nil
// and ready for writing. EXIF is initialised from a minimal parse so that
// IFD0 is valid.
func newTestMetadata(t *testing.T) *Metadata {
t.Helper()
// Build a minimal LE TIFF so EXIF.IFD0 is valid.
const ifdOff = uint32(8)
buf := make([]byte, 8+2+12+4) // header + 1 IFD entry + next-ptr
buf[0], buf[1] = 'I', 'I'
buf[2], buf[3] = 0x2A, 0x00
buf[4] = byte(ifdOff)
// 1 entry
buf[8], buf[9] = 0x01, 0x00
// entry: tag=0x0100 (ImageWidth), type=4 (Long), count=1, value=1
buf[10], buf[11] = 0x00, 0x01 // tag LE
buf[12], buf[13] = 0x04, 0x00 // type LE
buf[14], buf[15], buf[16], buf[17] = 0x01, 0x00, 0x00, 0x00 // count LE
buf[18], buf[19], buf[20], buf[21] = 0x01, 0x00, 0x00, 0x00 // value LE
// next IFD offset = 0
buf[22], buf[23], buf[24], buf[25] = 0x00, 0x00, 0x00, 0x00
e, err := exif.Parse(buf)
if err != nil {
t.Fatalf("newTestMetadata: exif.Parse: %v", err)
}
i := new(iptc.IPTC)
x := &xmp.XMP{Properties: make(map[string]map[string]string)}
return &Metadata{EXIF: e, IPTC: i, XMP: x}
}
// testSetCaption is a helper for TestMetadataSetters/SetCaption.
func testSetCaption(t *testing.T, m *Metadata) {
t.Helper()
m.SetCaption("golden hour")
if got := m.EXIF.Caption(); got != "golden hour" {
t.Errorf("EXIF.Caption = %q, want %q", got, "golden hour")
}
if got := m.IPTC.Caption(); got != "golden hour" {
t.Errorf("IPTC.Caption = %q, want %q", got, "golden hour")
}
if got := m.XMP.Caption(); got != "golden hour" {
t.Errorf("XMP.Caption = %q, want %q", got, "golden hour")
}
// Metadata getter returns XMP value (highest priority).
if got := m.Caption(); got != "golden hour" {
t.Errorf("m.Caption() = %q, want %q", got, "golden hour")
}
}
// testSetCopyright is a helper for TestMetadataSetters/SetCopyright.
func testSetCopyright(t *testing.T, m *Metadata) {
t.Helper()
m.SetCopyright("(c) 2024 Alice")
if got := m.EXIF.Copyright(); got != "(c) 2024 Alice" {
t.Errorf("EXIF.Copyright = %q", got)
}
if got := m.IPTC.Copyright(); got != "(c) 2024 Alice" {
t.Errorf("IPTC.Copyright = %q", got)
}
if got := m.XMP.Copyright(); got != "(c) 2024 Alice" {
t.Errorf("XMP.Copyright = %q", got)
}
}
// testSetCreator is a helper for TestMetadataSetters/SetCreator.
func testSetCreator(t *testing.T, m *Metadata) {
t.Helper()
m.SetCreator("Bob")
if got := m.EXIF.Creator(); got != "Bob" {
t.Errorf("EXIF.Creator = %q", got)
}
if got := m.IPTC.Creator(); got != "Bob" {
t.Errorf("IPTC.Creator = %q", got)
}
if got := m.XMP.Creator(); got != "Bob" {
t.Errorf("XMP.Creator = %q", got)
}
}
// testSetCameraModel is a helper for TestMetadataSetters/SetCameraModel.
func testSetCameraModel(t *testing.T, m *Metadata) {
t.Helper()
m.SetCameraModel("Canon EOS R5")
if got := m.EXIF.CameraModel(); got != "Canon EOS R5" {
t.Errorf("EXIF.CameraModel = %q", got)
}
if got := m.XMP.CameraModel(); got != "Canon EOS R5" {
t.Errorf("XMP.CameraModel = %q", got)
}
}
// testSetGPS is a helper for TestMetadataSetters/SetGPS.
func testSetGPS(t *testing.T, m *Metadata) {
t.Helper()
m.SetGPS(51.5074, -0.1278)
lat, lon, ok := m.EXIF.GPS()
if !ok {
t.Fatal("EXIF.GPS() ok=false after SetGPS")
}
if math.Abs(lat-51.5074) > 0.001 {
t.Errorf("EXIF lat = %f, want ~51.5074", lat)
}
if math.Abs(lon-(-0.1278)) > 0.001 {
t.Errorf("EXIF lon = %f, want ~-0.1278", lon)
}
xlat, xlon, xok := m.XMP.GPS()
if !xok {
t.Fatal("XMP.GPS() ok=false after SetGPS")
}
if math.Abs(xlat-51.5074) > 0.001 {
t.Errorf("XMP lat = %f, want ~51.5074", xlat)
}
if math.Abs(xlon-(-0.1278)) > 0.001 {
t.Errorf("XMP lon = %f, want ~-0.1278", xlon)
}
}
// testSetKeywords is a helper for TestMetadataSetters/SetKeywords.
func testSetKeywords(t *testing.T, m *Metadata) {
t.Helper()
m.SetKeywords([]string{"travel", "street"})
if kws := m.IPTC.Keywords(); len(kws) != 2 {
t.Errorf("IPTC.Keywords count = %d, want 2", len(kws))
}
if kws := m.XMP.Keywords(); len(kws) != 2 {
t.Errorf("XMP.Keywords count = %d, want 2", len(kws))
}
}
// testSetLensModel is a helper for TestMetadataSetters/SetLensModel.
func testSetLensModel(t *testing.T, m *Metadata) {
t.Helper()
m.SetLensModel("EF 50mm f/1.2L")
if got := m.EXIF.LensModel(); got != "EF 50mm f/1.2L" {
t.Errorf("EXIF.LensModel = %q", got)
}
if got := m.XMP.LensModel(); got != "EF 50mm f/1.2L" {
t.Errorf("XMP.LensModel = %q", got)
}
}
// testSetMake is a helper for TestMetadataSetters/SetMake.
func testSetMake(t *testing.T, m *Metadata) {
t.Helper()
m.SetMake("Canon")
if got := m.Make(); got != "Canon" {
t.Errorf("Make = %q, want Canon", got)
}
}
// testSetDateTimeOriginal is a helper for TestMetadataSetters/SetDateTimeOriginal.
func testSetDateTimeOriginal(t *testing.T, m *Metadata) {
t.Helper()
ts := time.Date(2024, 6, 21, 12, 0, 0, 0, time.UTC)
m.SetDateTimeOriginal(ts)
got, ok := m.EXIF.DateTimeOriginal()
if !ok {
t.Fatal("EXIF.DateTimeOriginal missing after set")
}
if !got.Equal(ts) {
t.Errorf("EXIF.DateTimeOriginal = %v, want %v", got, ts)
}
xmpStr := m.XMP.DateTimeOriginal()
if xmpStr == "" {
t.Error("XMP.DateTimeOriginal empty after set")
}
}
// testSetExposureTime is a helper for TestMetadataSetters/SetExposureTime.
func testSetExposureTime(t *testing.T, m *Metadata) {
t.Helper()
m.SetExposureTime(1, 250)
num, den, ok := m.ExposureTime()
if !ok {
t.Fatal("ExposureTime missing after set")
}
if num != 1 || den != 250 {
t.Errorf("ExposureTime = %d/%d, want 1/250", num, den)
}
}
// testSetFNumber is a helper for TestMetadataSetters/SetFNumber.
func testSetFNumber(t *testing.T, m *Metadata) {
t.Helper()
m.SetFNumber(4.0)
f, ok := m.FNumber()
if !ok {
t.Fatal("FNumber missing after set")
}
if math.Abs(f-4.0) > 0.001 {
t.Errorf("FNumber = %f, want 4.0", f)
}
}
// testSetISO is a helper for TestMetadataSetters/SetISO.
func testSetISO(t *testing.T, m *Metadata) {
t.Helper()
m.SetISO(800)
iso, ok := m.ISO()
if !ok {
t.Fatal("ISO missing after set")
}
if iso != 800 {
t.Errorf("ISO = %d, want 800", iso)
}
}
// testSetFocalLength is a helper for TestMetadataSetters/SetFocalLength.
func testSetFocalLength(t *testing.T, m *Metadata) {
t.Helper()
m.SetFocalLength(85.0)
fl, ok := m.FocalLength()
if !ok {
t.Fatal("FocalLength missing after set")
}
if math.Abs(fl-85.0) > 0.001 {
t.Errorf("FocalLength = %f, want 85.0", fl)
}
}
// testSetOrientation is a helper for TestMetadataSetters/SetOrientation.
func testSetOrientation(t *testing.T, m *Metadata) {
t.Helper()
m.SetOrientation(6)
v, ok := m.Orientation()
if !ok {
t.Fatal("Orientation missing after set")
}
if v != 6 {
t.Errorf("Orientation = %d, want 6", v)
}
}
// testSetImageSize is a helper for TestMetadataSetters/SetImageSize.
func testSetImageSize(t *testing.T, m *Metadata) {
t.Helper()
m.SetImageSize(1920, 1080)
w, h, ok := m.ImageSize()
if !ok {
t.Fatal("ImageSize missing after set")
}
if w != 1920 || h != 1080 {
t.Errorf("ImageSize = %dx%d, want 1920x1080", w, h)
}
}
// TestMetadataSetters verifies that every Metadata setter writes through to
// the underlying components and that getters return the expected values.
func TestMetadataSetters(t *testing.T) {
t.Parallel()
t.Run("SetCaption", func(t *testing.T) {
t.Parallel()
testSetCaption(t, newTestMetadata(t))
})
t.Run("SetCopyright", func(t *testing.T) {
t.Parallel()
testSetCopyright(t, newTestMetadata(t))
})
t.Run("SetCreator", func(t *testing.T) {
t.Parallel()
testSetCreator(t, newTestMetadata(t))
})
t.Run("SetCameraModel", func(t *testing.T) {
t.Parallel()
testSetCameraModel(t, newTestMetadata(t))
})
t.Run("SetGPS", func(t *testing.T) {
t.Parallel()
testSetGPS(t, newTestMetadata(t))
})
t.Run("SetKeywords", func(t *testing.T) {
t.Parallel()
testSetKeywords(t, newTestMetadata(t))
})
t.Run("SetLensModel", func(t *testing.T) {
t.Parallel()
testSetLensModel(t, newTestMetadata(t))
})
t.Run("SetMake", func(t *testing.T) {
t.Parallel()
testSetMake(t, newTestMetadata(t))
})
t.Run("SetDateTimeOriginal", func(t *testing.T) {
t.Parallel()
testSetDateTimeOriginal(t, newTestMetadata(t))
})
t.Run("SetExposureTime", func(t *testing.T) {
t.Parallel()
testSetExposureTime(t, newTestMetadata(t))
})
t.Run("SetFNumber", func(t *testing.T) {
t.Parallel()
testSetFNumber(t, newTestMetadata(t))
})
t.Run("SetISO", func(t *testing.T) {
t.Parallel()
testSetISO(t, newTestMetadata(t))
})
t.Run("SetFocalLength", func(t *testing.T) {
t.Parallel()
testSetFocalLength(t, newTestMetadata(t))
})
t.Run("SetOrientation", func(t *testing.T) {
t.Parallel()
testSetOrientation(t, newTestMetadata(t))
})
t.Run("SetImageSize", func(t *testing.T) {
t.Parallel()
testSetImageSize(t, newTestMetadata(t))
})
}
// TestMetadataSettersNilComponents verifies that every Metadata setter is a
// no-op (and does not panic) when all component pointers are nil.
func TestMetadataSettersNilComponents(t *testing.T) {
t.Parallel()
m := &Metadata{} // all components nil
ts := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
m.SetCaption("x")
m.SetCopyright("x")
m.SetCreator("x")
m.SetCameraModel("x")
m.SetGPS(0, 0)
m.SetKeywords([]string{"a"})
m.SetLensModel("x")
m.SetMake("x")
m.SetDateTimeOriginal(ts)
m.SetExposureTime(1, 100)
m.SetFNumber(1.4)
m.SetISO(100)
m.SetFocalLength(50)
m.SetOrientation(1)
m.SetImageSize(100, 100)
// reaching here without panic is the pass condition
}