From 18206e5b5f12a06b02414851f09b3add715351d9 Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Thu, 1 Jul 2021 19:56:38 +0300 Subject: [PATCH] =?UTF-8?q?=E2=A7=AB=20Gave=20up=20wrapping=20functions.?= =?UTF-8?q?=20Added=20some=20tests=20=E2=A7=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- id3ed.go | 59 ------------------------------------ id3ed_test.go | 48 ----------------------------- testData/testWriteTags.mp3 | 0 testData/testwritev1.mp3 | Bin 1439 -> 2207 bytes util/read_test.go | 60 +++++++++++++++++++++++++++++++++++++ util/write_test.go | 32 ++++++++++++++++++++ 6 files changed, 92 insertions(+), 107 deletions(-) delete mode 100644 id3ed.go delete mode 100644 id3ed_test.go delete mode 100755 testData/testWriteTags.mp3 create mode 100644 util/read_test.go create mode 100644 util/write_test.go diff --git a/id3ed.go b/id3ed.go deleted file mode 100644 index c3fadda..0000000 --- a/id3ed.go +++ /dev/null @@ -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 -} diff --git a/id3ed_test.go b/id3ed_test.go deleted file mode 100644 index 33aab00..0000000 --- a/id3ed_test.go +++ /dev/null @@ -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) -// } -// } diff --git a/testData/testWriteTags.mp3 b/testData/testWriteTags.mp3 deleted file mode 100755 index e69de29..0000000 diff --git a/testData/testwritev1.mp3 b/testData/testwritev1.mp3 index 5dfc8db24c6b32771cbeaf4b2e74c88eea024733..a5c531b6b562e7fdc42e50d108931f43cd49a13e 100644 GIT binary patch delta 11 ScmbQwJzsFceAdPD8JPhZegpLY delta 7 OcmbO)IG=mNd{zJpU;?iI diff --git a/util/read_test.go b/util/read_test.go new file mode 100644 index 0000000..28a8975 --- /dev/null +++ b/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) + } + } +} diff --git a/util/write_test.go b/util/write_test.go new file mode 100644 index 0000000..b80813a --- /dev/null +++ b/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) + } +}