Merge branch 'jooola-tag_test'

This commit is contained in:
David Howden 2018-09-21 19:02:25 +10:00
commit c555fff0b7
16 changed files with 151 additions and 7 deletions

View File

@ -82,10 +82,10 @@ func ReadID3v1Tags(r io.ReadSeeker) (Metadata, error) {
var comment string
var track int
if commentBytes[28] == 0 {
comment = strings.TrimSpace(string(commentBytes[:28]))
comment = trimString(string(commentBytes[:28]))
track = int(commentBytes[29])
} else {
comment = strings.TrimSpace(string(commentBytes))
comment = trimString(string(commentBytes))
}
var genre string
@ -98,17 +98,21 @@ func ReadID3v1Tags(r io.ReadSeeker) (Metadata, error) {
}
m := make(map[string]interface{})
m["title"] = strings.TrimSpace(title)
m["artist"] = strings.TrimSpace(artist)
m["album"] = strings.TrimSpace(album)
m["year"] = strings.TrimSpace(year)
m["comment"] = strings.TrimSpace(comment)
m["title"] = trimString(title)
m["artist"] = trimString(artist)
m["album"] = trimString(album)
m["year"] = trimString(year)
m["comment"] = trimString(comment)
m["track"] = track
m["genre"] = genre
return metadataID3v1(m), nil
}
func trimString(x string) string {
return strings.TrimSpace(strings.Trim(x, "\x00"))
}
// metadataID3v1 is the implementation of Metadata used for ID3v1 tags.
type metadataID3v1 map[string]interface{}

120
tag_test.go Normal file
View File

@ -0,0 +1,120 @@
// 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,
"with_tags/sample.ogg": fullMetadata,
"without_tags/sample.flac": emptyMetadata,
"without_tags/sample.m4a": emptyMetadata,
"without_tags/sample.mp3": emptyMetadata,
"without_tags/sample.mp4": emptyMetadata,
"without_tags/sample.ogg": 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)
}
}

20
testdata/README.md vendored Normal file
View File

@ -0,0 +1,20 @@
# testdata
Samples files come from [here](http://techslides.com/sample-files-for-development)
To write tags to files you can use `lltag`:
```sh
lltag sample.* \
-a "Test Artist" \
-t "Test Title" \
-A "Test Album" \
-n "3" \
-g "Jazz" \
-d "2000" \
-c "Test Comment" \
--tag ALBUMARTIST="Test AlbumArtist" \
--tag COMPOSER="Test Composer"\
--tag DISCNUMBER="02" \
--tag TRACKTOTAL="06"
```

BIN
testdata/with_tags/sample.flac vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.id3v11.mp3 vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.id3v22.mp3 vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.id3v23.mp3 vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.id3v24.mp3 vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.m4a vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.mp4 vendored Normal file

Binary file not shown.

BIN
testdata/with_tags/sample.ogg vendored Normal file

Binary file not shown.

BIN
testdata/without_tags/sample.flac vendored Normal file

Binary file not shown.

BIN
testdata/without_tags/sample.m4a vendored Normal file

Binary file not shown.

BIN
testdata/without_tags/sample.mp3 vendored Normal file

Binary file not shown.

BIN
testdata/without_tags/sample.mp4 vendored Normal file

Binary file not shown.

BIN
testdata/without_tags/sample.ogg vendored Normal file

Binary file not shown.