From d39974bf1ec9713acd711b0f31b6d26110262745 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Thu, 24 Jun 2021 14:48:08 +0300 Subject: [PATCH] Testing and minor improvements --- README.md | 21 +++++++++++++++--- id3v10.go | 2 +- id3v10_test.go | 23 +++++++++++++++++++ id3v11.go | 6 ++--- id3v11_test.go | 27 +++++++++++++++++++++++ id3v1_test.go | 17 -------------- testData/{id3v1.txt => testmp3id3v1.mp3} | Bin util.go | 14 ++++++++++-- 8 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 id3v10_test.go create mode 100644 id3v11_test.go delete mode 100644 id3v1_test.go rename testData/{id3v1.txt => testmp3id3v1.mp3} (100%) diff --git a/README.md b/README.md index ec3b72f..78672eb 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,35 @@ func main() { panic(err) } + // extract ID3v1.1 tags mp3tags, err := GetID3v11Tags(mp3file) if err != nil { panic(err) } - // printing all tags + // print all tags fmt.Printf("%+v",mp3tags) - // getting certain tag + // get a certain tag from "getter" function songname := mp3tags.GetSongName() + + // get a certain tag from struct field + genre := mp3tags.Genre // etc. } ``` +# Testing + +``` +go test +``` +or +``` +go test -v +``` +to get a verbose output + # Under construction ! -## Bugs are a possibility rn in this state \ No newline at end of file +## Bugs are a possibility rn in this state, the package is still not tested properly \ No newline at end of file diff --git a/id3v10.go b/id3v10.go index 83998af..bdc98fb 100644 --- a/id3v10.go +++ b/id3v10.go @@ -35,7 +35,7 @@ func GetID3v1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) { if !bytes.Equal(tag, []byte("TAG")) { // no TAG, given file does not use ID3v1 - return nil, fmt.Errorf("does not use ID3v1") + return nil, fmt.Errorf("does not use ID3v1: expected %s; got %s", "TAG", tag) } songname, err := readToString(rs, 30) diff --git a/id3v10_test.go b/id3v10_test.go new file mode 100644 index 0000000..f822605 --- /dev/null +++ b/id3v10_test.go @@ -0,0 +1,23 @@ +package id3ed + +import ( + "os" + "testing" +) + +func TestGetID3v1Tags(t *testing.T) { + testfile, err := os.Open("./testData/testmp3id3v1.mp3") + if err != nil { + t.Errorf("could not open file for testing: %s", err) + } + tags, err := GetID3v1Tags(testfile) + if err != nil { + t.Errorf("GetID3v1Tags failed: %s", err) + } + + comment := tags.GetComment() + + if comment != "Comment here " { + t.Errorf("GetID3v1Tags failed: expected %s; got %s", "Comment here ", comment) + } +} diff --git a/id3v11.go b/id3v11.go index 3da3e98..c264c67 100644 --- a/id3v11.go +++ b/id3v11.go @@ -32,7 +32,7 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { if !bytes.Equal(tag, []byte("TAG")) { // no TAG, given file does not use ID3v1 - return nil, fmt.Errorf("does not use ID3v1") + return nil, fmt.Errorf("does not use ID3v1: expected %s; got %s", "TAG", tag) } songname, err := readToString(rs, 30) @@ -50,11 +50,11 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) { return nil, err } - yearBytes, err := read(rs, 4) + yearStr, err := readToString(rs, 4) if err != nil { return nil, err } - year, err := strconv.Atoi(string(yearBytes)) + year, err := strconv.Atoi(yearStr) if err != nil { return nil, fmt.Errorf("could not convert yearbytes into int: %s", err) } diff --git a/id3v11_test.go b/id3v11_test.go new file mode 100644 index 0000000..0dc3202 --- /dev/null +++ b/id3v11_test.go @@ -0,0 +1,27 @@ +package id3ed + +import ( + "fmt" + "os" + "testing" +) + +func TestGetID3v11Tags(t *testing.T) { + testfile, err := os.Open("./testData/testmp3id3v1.mp3") + if err != nil { + t.Errorf("could not open file for testing: %s", err) + } + mp3tags, err := GetID3v11Tags(testfile) + if err != nil { + t.Errorf("GetID3v1Tags failed: %s", err) + } + + if mp3tags.GetArtist() != "Artist" { + fmt.Printf("%v", mp3tags.Artist) + t.Errorf("GetID3v1Tags has failed: expected %v; got %v", "Artist", mp3tags.GetArtist()) + } + + if mp3tags.GetTrack() != 8 { + t.Errorf("GetID3v1Tags has failed: expected %v; got %v", 8, mp3tags.GetTrack()) + } +} diff --git a/id3v1_test.go b/id3v1_test.go deleted file mode 100644 index 962ad32..0000000 --- a/id3v1_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package id3ed - -import ( - "os" - "testing" -) - -func TestGetID3v1Tags(t *testing.T) { - testfile, err := os.Open("./testData/id3v1.txt") - if err != nil { - t.Errorf("could not open file for testing: %s", err) - } - _, err = GetID3v1Tags(testfile) - if err != nil { - t.Errorf("GetID3v1Tags failed: %s", err) - } -} diff --git a/testData/id3v1.txt b/testData/testmp3id3v1.mp3 similarity index 100% rename from testData/id3v1.txt rename to testData/testmp3id3v1.mp3 diff --git a/util.go b/util.go index 21cd30e..e23c1cc 100644 --- a/util.go +++ b/util.go @@ -16,12 +16,22 @@ func read(rs io.Reader, n int) ([]byte, error) { return read, nil } -// Shortcut function to read n bytes and convert them into string +// Shortcut function to read n bytes and convert them into string. +// If encountered zero-byte - converts to string only previously read bytes func readToString(rs io.Reader, n int) (string, error) { read := make([]byte, n) _, err := rs.Read(read) if err != nil { return "", fmt.Errorf("could not read from reader: %s", err) } - return string(read), nil + + var readString string + for _, b := range read { + if b == 0 { + break + } + readString += string(b) + } + + return readString, nil }