Browse Source

Testing and minor improvements

main
Unbewohnte 3 years ago
parent
commit
d39974bf1e
  1. 21
      README.md
  2. 2
      id3v10.go
  3. 23
      id3v10_test.go
  4. 6
      id3v11.go
  5. 27
      id3v11_test.go
  6. 17
      id3v1_test.go
  7. 0
      testData/testmp3id3v1.mp3
  8. 14
      util.go

21
README.md

@ -23,20 +23,35 @@ func main() {
panic(err) panic(err)
} }
// extract ID3v1.1 tags
mp3tags, err := GetID3v11Tags(mp3file) mp3tags, err := GetID3v11Tags(mp3file)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// printing all tags // print all tags
fmt.Printf("%+v",mp3tags) fmt.Printf("%+v",mp3tags)
// getting certain tag // get a certain tag from "getter" function
songname := mp3tags.GetSongName() songname := mp3tags.GetSongName()
// get a certain tag from struct field
genre := mp3tags.Genre
// etc. // etc.
} }
``` ```
# Testing
```
go test
```
or
```
go test -v
```
to get a verbose output
# Under construction ! # Under construction !
## Bugs are a possibility rn in this state ## Bugs are a possibility rn in this state, the package is still not tested properly

2
id3v10.go

@ -35,7 +35,7 @@ func GetID3v1Tags(rs io.ReadSeeker) (*ID3v1Tags, error) {
if !bytes.Equal(tag, []byte("TAG")) { if !bytes.Equal(tag, []byte("TAG")) {
// no TAG, given file does not use ID3v1 // 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) songname, err := readToString(rs, 30)

23
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)
}
}

6
id3v11.go

@ -32,7 +32,7 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
if !bytes.Equal(tag, []byte("TAG")) { if !bytes.Equal(tag, []byte("TAG")) {
// no TAG, given file does not use ID3v1 // 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) songname, err := readToString(rs, 30)
@ -50,11 +50,11 @@ func GetID3v11Tags(rs io.ReadSeeker) (*ID3v11Tags, error) {
return nil, err return nil, err
} }
yearBytes, err := read(rs, 4) yearStr, err := readToString(rs, 4)
if err != nil { if err != nil {
return nil, err return nil, err
} }
year, err := strconv.Atoi(string(yearBytes)) year, err := strconv.Atoi(yearStr)
if err != nil { if err != nil {
return nil, fmt.Errorf("could not convert yearbytes into int: %s", err) return nil, fmt.Errorf("could not convert yearbytes into int: %s", err)
} }

27
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())
}
}

17
id3v1_test.go

@ -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)
}
}

0
testData/id3v1.txt → testData/testmp3id3v1.mp3

14
util.go

@ -16,12 +16,22 @@ func read(rs io.Reader, n int) ([]byte, error) {
return read, nil 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) { func readToString(rs io.Reader, n int) (string, error) {
read := make([]byte, n) read := make([]byte, n)
_, err := rs.Read(read) _, err := rs.Read(read)
if err != nil { if err != nil {
return "", fmt.Errorf("could not read from reader: %s", err) 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
} }

Loading…
Cancel
Save