diff --git a/tag_test.go b/tag_test.go new file mode 100644 index 0000000..b66f65b --- /dev/null +++ b/tag_test.go @@ -0,0 +1,118 @@ +// Copyright 2015, David Howden +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package tag + +import ( + "os" + "testing" +) + +type testMetadata struct { + Album string + AlbumArtist string + Artist string + Comment string + Composer string + Disc int + DiscTotal int + Genre string + Lyrics string + Title string + Track int + TrackTotal int + Year int +} + +var emptyMetadata = testMetadata{} +var fullMetadata = testMetadata{ + Album: "Test Album", + AlbumArtist: "Test AlbumArtist", + Artist: "Test Artist", + Composer: "Test Composer", + Disc: 2, + DiscTotal: 0, + Genre: "Jazz", + Lyrics: "", + Title: "Test Title", + Track: 3, + TrackTotal: 6, + Year: 2000, +} +var mp3id3v11Metadata = testMetadata{ + Album: "Test Album", + Artist: "Test Artist", + Genre: "Jazz", + Lyrics: "", + Title: "Test Title", + Track: 3, + Year: 2000, +} + +func TestReadFrom(t *testing.T) { + testdata := map[string]testMetadata{ + "with_tags/sample.flac": fullMetadata, + "with_tags/sample.id3v11.mp3": mp3id3v11Metadata, + "with_tags/sample.id3v22.mp3": fullMetadata, + "with_tags/sample.id3v23.mp3": fullMetadata, + "with_tags/sample.id3v24.mp3": fullMetadata, + "with_tags/sample.m4a": fullMetadata, + "with_tags/sample.mp4": fullMetadata, + "without_tags/sample.flac": emptyMetadata, + "without_tags/sample.m4a": emptyMetadata, + "without_tags/sample.mp3": emptyMetadata, + "without_tags/sample.mp4": emptyMetadata, + } + + for path, metadata := range testdata { + if err := test(t, path, metadata); err != nil { + + // mp3 id3v11 returns an err if it doesn't find any tags + if err != ErrNoTagsFound && path != "without_tags/sample.mp3" { + t.Error(err) + } + + } + } +} + +func test(t *testing.T, path string, metadata testMetadata) error { + f, err := os.Open("testdata/" + path) + if err != nil { + return err + } + defer f.Close() + + m, err := ReadFrom(f) + if err != nil { + return err + } + compareMetadata(t, m, metadata) + return nil +} + +func compareMetadata(t *testing.T, m Metadata, tt testMetadata) { + testValue(t, tt.Album, m.Album()) + testValue(t, tt.AlbumArtist, m.AlbumArtist()) + testValue(t, tt.Artist, m.Artist()) + testValue(t, tt.Composer, m.Composer()) + testValue(t, tt.Genre, m.Genre()) + testValue(t, tt.Lyrics, m.Lyrics()) + testValue(t, tt.Title, m.Title()) + testValue(t, tt.Year, m.Year()) + + disc, discTotal := m.Disc() + testValue(t, tt.Disc, disc) + testValue(t, tt.DiscTotal, discTotal) + + track, trackTotal := m.Track() + testValue(t, tt.Track, track) + testValue(t, tt.TrackTotal, trackTotal) +} + +func testValue(t *testing.T, expected interface{}, found interface{}) { + if expected != found { + t.Errorf("expected '%v', found '%v'", expected, found) + } +} diff --git a/testdata/README.md b/testdata/README.md new file mode 100644 index 0000000..501afbc --- /dev/null +++ b/testdata/README.md @@ -0,0 +1,3 @@ +# testdata + +Samples files come from [here](http://techslides.com/sample-files-for-development) diff --git a/testdata/with_tags/sample.flac b/testdata/with_tags/sample.flac new file mode 100644 index 0000000..db237f5 Binary files /dev/null and b/testdata/with_tags/sample.flac differ diff --git a/testdata/with_tags/sample.id3v11.mp3 b/testdata/with_tags/sample.id3v11.mp3 new file mode 100644 index 0000000..cd85493 Binary files /dev/null and b/testdata/with_tags/sample.id3v11.mp3 differ diff --git a/testdata/with_tags/sample.id3v22.mp3 b/testdata/with_tags/sample.id3v22.mp3 new file mode 100644 index 0000000..9d6850d Binary files /dev/null and b/testdata/with_tags/sample.id3v22.mp3 differ diff --git a/testdata/with_tags/sample.id3v23.mp3 b/testdata/with_tags/sample.id3v23.mp3 new file mode 100644 index 0000000..9d6850d Binary files /dev/null and b/testdata/with_tags/sample.id3v23.mp3 differ diff --git a/testdata/with_tags/sample.id3v24.mp3 b/testdata/with_tags/sample.id3v24.mp3 new file mode 100644 index 0000000..768545f Binary files /dev/null and b/testdata/with_tags/sample.id3v24.mp3 differ diff --git a/testdata/with_tags/sample.m4a b/testdata/with_tags/sample.m4a new file mode 100644 index 0000000..3f6cbac Binary files /dev/null and b/testdata/with_tags/sample.m4a differ diff --git a/testdata/with_tags/sample.mp4 b/testdata/with_tags/sample.mp4 new file mode 100644 index 0000000..c2bdac0 Binary files /dev/null and b/testdata/with_tags/sample.mp4 differ diff --git a/testdata/without_tags/sample.flac b/testdata/without_tags/sample.flac new file mode 100644 index 0000000..46ccf2e Binary files /dev/null and b/testdata/without_tags/sample.flac differ diff --git a/testdata/without_tags/sample.m4a b/testdata/without_tags/sample.m4a new file mode 100644 index 0000000..1c717db Binary files /dev/null and b/testdata/without_tags/sample.m4a differ diff --git a/testdata/without_tags/sample.mp3 b/testdata/without_tags/sample.mp3 new file mode 100644 index 0000000..8f29068 Binary files /dev/null and b/testdata/without_tags/sample.mp3 differ diff --git a/testdata/without_tags/sample.mp4 b/testdata/without_tags/sample.mp4 new file mode 100644 index 0000000..151cd53 Binary files /dev/null and b/testdata/without_tags/sample.mp4 differ