-
Notifications
You must be signed in to change notification settings - Fork 20
/
taglib_test.go
295 lines (238 loc) · 6.94 KB
/
taglib_test.go
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
package taglib
import (
"io"
"io/ioutil"
"os"
"path"
"strconv"
"testing"
"time"
)
func TestReadNothing(t *testing.T) {
file, err := Read("doesnotexist.mp3")
if file != nil {
t.Fatal("Returned non nil file struct.")
}
if err == nil {
t.Fatal("Returned nil err.")
}
if err != ErrInvalid {
t.Fatal("Didn't return ErrInvalid")
}
}
func TestReadDirectory(t *testing.T) {
file, err := Read("/")
if file != nil {
t.Fatal("Returned non nil file struct.")
}
if err == nil {
t.Fatal("Returned nil err.")
}
if err != ErrInvalid {
t.Fatal("Didn't return ErrInvalid")
}
}
func TestTagLib(t *testing.T) {
file, err := Read("test.mp3")
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
defer file.Close()
// Test the Tags
if title := file.Title(); title != "The Title" {
t.Errorf("Got wrong title: %s", title)
}
if artist := file.Artist(); artist != "The Artist" {
t.Errorf("Got wrong artist: %s", artist)
}
if album := file.Album(); album != "The Album" {
t.Errorf("Got wrong album: %s", album)
}
if comment := file.Comment(); comment != "A Comment" {
t.Errorf("Got wrong comment: %s", comment)
}
if genre := file.Genre(); genre != "Booty Bass" {
t.Errorf("Got wrong genre: %s", genre)
}
if year := file.Year(); year != 1942 {
t.Errorf("Got wrong year: %d", year)
}
if track := file.Track(); track != 42 {
t.Errorf("Got wrong track: %d", track)
}
// Test the properties
if length := file.Length(); length != 42*time.Second {
t.Errorf("Got wrong length: %s", length)
}
if bitrate := file.Bitrate(); bitrate != 128 {
t.Errorf("Got wrong bitrate: %d", bitrate)
}
if samplerate := file.Samplerate(); samplerate != 44100 {
t.Errorf("Got wrong samplerate: %d", samplerate)
}
if channels := file.Channels(); channels != 2 {
t.Errorf("Got wrong channels: %d", channels)
}
}
func TestWriteTagLib(t *testing.T) {
fileName := "test.mp3"
file, err := Read(fileName)
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
tempDir, err := ioutil.TempDir("", "go-taglib-test")
if err != nil {
panic(err)
t.Fatalf("Cannot create temporary file for writing tests: %s", err)
}
tempFileName := path.Join(tempDir, "go-taglib-test.mp3")
defer file.Close()
defer os.RemoveAll(tempDir)
err = cp(tempFileName, fileName)
if err != nil {
panic(err)
t.Fatalf("Cannot copy file for writing tests: %s", err)
}
modifiedFile, err := Read(tempFileName)
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
modifiedFile.SetAlbum(getModifiedString(file.Album()))
modifiedFile.SetComment(getModifiedString(file.Comment()))
modifiedFile.SetGenre(getModifiedString(file.Genre()))
modifiedFile.SetTrack(file.Track() + 1)
modifiedFile.SetYear(file.Year() + 1)
modifiedFile.SetArtist(getModifiedString(file.Artist()))
modifiedFile.SetTitle(getModifiedString(file.Title()))
err = modifiedFile.Save()
if err != nil {
panic(err)
t.Fatalf("Cannot save file : %s", err)
}
modifiedFile.Close()
//Re-open the modified file
modifiedFile, err = Read(tempFileName)
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
// Test the Tags
if title := modifiedFile.Title(); title != getModifiedString("The Title") {
t.Errorf("Got wrong modified title: %s", title)
}
if artist := modifiedFile.Artist(); artist != getModifiedString("The Artist") {
t.Errorf("Got wrong modified artist: %s", artist)
}
if album := modifiedFile.Album(); album != getModifiedString("The Album") {
t.Errorf("Got wrong modified album: %s", album)
}
if comment := modifiedFile.Comment(); comment != getModifiedString("A Comment") {
t.Errorf("Got wrong modified comment: %s", comment)
}
if genre := modifiedFile.Genre(); genre != getModifiedString("Booty Bass") {
t.Errorf("Got wrong modified genre: %s", genre)
}
if year := modifiedFile.Year(); year != getModifiedInt(1942) {
t.Errorf("Got wrong modified year: %d", year)
}
if track := modifiedFile.Track(); track != getModifiedInt(42) {
t.Errorf("Got wrong modified track: %d", track)
}
}
func TestGenericWriteTagLib(t *testing.T) {
fileName := "test.mp3"
file, err := Read(fileName)
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
tempDir, err := ioutil.TempDir("", "go-taglib-test")
if err != nil {
panic(err)
t.Fatalf("Cannot create temporary file for writing tests: %s", err)
}
tempFileName := path.Join(tempDir, "go-taglib-test.mp3")
defer file.Close()
defer os.RemoveAll(tempDir)
err = cp(tempFileName, fileName)
if err != nil {
panic(err)
t.Fatalf("Cannot copy file for writing tests: %s", err)
}
modifiedFile, err := Read(tempFileName)
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
modifiedFile.SetTag(Album, getModifiedString(file.Album()))
modifiedFile.SetTag(Comments, getModifiedString(file.Comment()))
modifiedFile.SetTag(Genre, getModifiedString(file.Genre()))
modifiedFile.SetTag(Track, strconv.Itoa(file.Track()+1))
modifiedFile.SetTag(Year, strconv.Itoa(file.Year()+1))
modifiedFile.SetTag(Artist, getModifiedString(file.Artist()))
modifiedFile.SetTag(Title, getModifiedString(file.Title()))
err = modifiedFile.Save()
if err != nil {
panic(err)
t.Fatalf("Cannot save file : %s", err)
}
modifiedFile.Close()
//Re-open the modified file
modifiedFile, err = Read(tempFileName)
if err != nil {
panic(err)
t.Fatalf("Read returned error: %s", err)
}
// Test the Tags
if title := modifiedFile.Tag(Title); title != getModifiedString("The Title") {
t.Errorf("Got wrong modified title: %s", title)
}
if artist := modifiedFile.Tag(Artist); artist != getModifiedString("The Artist") {
t.Errorf("Got wrong modified artist: %s", artist)
}
if album := modifiedFile.Tag(Album); album != getModifiedString("The Album") {
t.Errorf("Got wrong modified album: %s", album)
}
if comment := modifiedFile.Tag(Comments); comment != getModifiedString("A Comment") {
t.Errorf("Got wrong modified comment: %s", comment)
}
if genre := modifiedFile.Tag(Genre); genre != getModifiedString("Booty Bass") {
t.Errorf("Got wrong modified genre: %s", genre)
}
if year := modifiedFile.Tag(Year); year != strconv.Itoa(getModifiedInt((1942))) {
t.Errorf("Got wrong modified year: %s", year)
}
if track := modifiedFile.Tag(Track); track != strconv.Itoa(getModifiedInt((42))) {
t.Errorf("Got wrong modified track: %s", track)
}
}
func checkModified(original string, modified string) bool {
return modified == getModifiedString(original)
}
func getModifiedString(s string) string {
return s + " MODIFIED"
}
func getModifiedInt(i int) int {
return i + 1
}
func cp(dst, src string) error {
s, err := os.Open(src)
if err != nil {
return err
}
// no need to check errors on read only file, we already got everything
// we need from the filesystem, so nothing can go wrong now.
defer s.Close()
d, err := os.Create(dst)
if err != nil {
return err
}
if _, err := io.Copy(d, s); err != nil {
d.Close()
return err
}
return d.Close()
}