Browse Source

⧫ Gave up wrapping functions. Added some tests ⧫

main
Unbewohnte 3 years ago
parent
commit
18206e5b5f
  1. 59
      id3ed.go
  2. 48
      id3ed_test.go
  3. 0
      testData/testWriteTags.mp3
  4. BIN
      testData/testwritev1.mp3
  5. 60
      util/read_test.go
  6. 32
      util/write_test.go

59
id3ed.go

@ -1,59 +0,0 @@
package id3ed
import (
"fmt"
"io"
"os"
v1 "github.com/Unbewohnte/id3ed/v1"
)
type Tagger interface {
WriteToFile(*os.File) error
Version() int
}
// Reads certain ID3 tags from io.ReadSeeker according to given version.
// Wrapper function to v1|v2 package functions
func ReadTags(rs io.ReadSeeker, version int) (Tagger, error) {
switch version {
case 10:
// ID3v1
t, err := v1.Getv1Tags(rs)
if err != nil {
return nil, err
}
return t, nil
case 11:
// ID3v1.1
t, err := v1.Getv11Tags(rs)
if err != nil {
return nil, err
}
return t, nil
case 23:
// ID3v2.3
return nil, fmt.Errorf("v2.3 is not supported")
case 24:
// ID3v2.4
return nil, fmt.Errorf("v2.4 is not supported")
}
return nil, fmt.Errorf("invalid version or not supported")
}
// DOESN`T work for some reason !? err is coming from f.Seek(), but it`s completely fine
// when run directly from v1.WriteToFile() function.
// Writes tags to file.
func WriteTags(dstf *os.File, t Tagger) error {
err := t.WriteToFile(dstf)
if err != nil {
return err
}
return nil
}

48
id3ed_test.go

@ -1,48 +0,0 @@
package id3ed
import (
"os"
"path/filepath"
"testing"
)
var TESTDATAPATH string = filepath.Join("testData")
func TestReadTags(t *testing.T) {
f, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testreadv1.mp3"), os.O_RDONLY, os.ModePerm)
if err != nil {
t.Errorf("%s", err)
}
_, err = ReadTags(f, 11)
if err != nil {
t.Errorf("ReadTags failed: %s", err)
}
_, err = ReadTags(f, 10)
if err != nil {
t.Errorf("ReadTags failed: %s", err)
}
}
// func TestWriteTags(t *testing.T) {
// f, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testreadv1.mp3"), os.O_RDONLY, os.ModePerm)
// if err != nil {
// t.Errorf("%s", err)
// }
// tagger, err := ReadTags(f, 11)
// if err != nil {
// t.Errorf("%s", err)
// }
// f2, err := os.OpenFile(filepath.Join(TESTDATAPATH, "testWriteTags.mp3"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, os.ModePerm)
// if err != nil {
// t.Errorf("%s", err)
// }
// err = WriteTags(f2, tagger)
// if err != nil {
// t.Errorf("WriteTags failed: %s", err)
// }
// }

0
testData/testWriteTags.mp3

BIN
testData/testwritev1.mp3

Binary file not shown.

60
util/read_test.go

@ -0,0 +1,60 @@
package util
import (
"bytes"
"testing"
)
func TestRead(t *testing.T) {
testBuffer := new(bytes.Buffer)
_, err := testBuffer.WriteString("Writing some data here. ᗜˬᗜ")
if err != nil {
t.Errorf("%s", err)
}
buffLen := testBuffer.Len()
buffBytes := testBuffer.Bytes()
read, err := Read(testBuffer, uint64(buffLen))
if err != nil {
t.Errorf("Read failed: %s", err)
}
if int(len(read)) != int(buffLen) {
t.Errorf("Read failed: expected length %d; got %d", buffLen, len(read))
}
for index, b := range read {
if buffBytes[index] != b {
t.Errorf("Read failed: expected byte %v; got %v at index %d", buffBytes[index], b, index)
}
}
}
func TestReadToString(t *testing.T) {
testBuffer := new(bytes.Buffer)
_, err := testBuffer.WriteString("This is literally the same as before. ᗜˬᗜ")
if err != nil {
t.Errorf("%s", err)
}
buffLen := testBuffer.Len()
buffBytes := testBuffer.Bytes()
read, err := Read(testBuffer, uint64(buffLen))
if err != nil {
t.Errorf("Read failed: %s", err)
}
if int(len(read)) != int(buffLen) {
t.Errorf("Read failed: expected length %d; got %d", buffLen, len(read))
}
for index, b := range read {
if buffBytes[index] != b {
t.Errorf("Read failed: expected byte %v; got %v at index %d", buffBytes[index], b, index)
}
}
}

32
util/write_test.go

@ -0,0 +1,32 @@
package util
import (
"bytes"
"testing"
)
func TestWriteToExtent(t *testing.T) {
testBuff := new(bytes.Buffer)
testData := []byte("some data here")
err := WriteToExtent(testBuff, testData, len(testData)+50)
if err != nil {
t.Errorf("WriteToExtent failed: %s", err)
}
if testBuff.Len() != len(testData)+50 {
t.Errorf("WriteToExtent failed: expected length %d; got %d", len(testData)+50, testBuff.Len())
}
nullByteCounter := 0
for _, b := range testBuff.Bytes() {
if b == 0 {
nullByteCounter++
}
}
if nullByteCounter != 50 {
t.Errorf("WriteToExtent failed: expected null bytes %d; got %d", 50, nullByteCounter)
}
}
Loading…
Cancel
Save