gomu/utils_test.go

159 lines
3.4 KiB
Go
Raw Permalink Normal View History

2020-07-03 11:17:50 +08:00
package main
import (
"fmt"
"os"
2020-07-03 11:17:50 +08:00
"testing"
"time"
2021-02-24 13:32:30 +08:00
"github.com/issadarkthing/gomu/lyric"
2021-02-24 13:32:30 +08:00
"github.com/stretchr/testify/assert"
"github.com/tramhao/id3v2"
2020-07-03 11:17:50 +08:00
)
func TestFmtDuration(t *testing.T) {
samples := map[time.Duration]string{
time.Second * 5: "00:05",
time.Hour * 2: "02:00:00",
time.Minute*4 + time.Second*15: "04:15",
time.Minute * 0: "00:00",
time.Millisecond * 5: "00:00",
}
for k, v := range samples {
got := fmtDuration(k)
if got != v {
t.Errorf("fmtDuration(%s); Expected %s got %s", k, v, got)
}
}
}
func TestGetName(t *testing.T) {
samples := map[string]string{
2020-07-10 22:05:36 +08:00
"hello.mp3": "hello",
"~/music/fl.mp3": "fl",
"/home/terra/Music/pop/hola na.mp3": "hola na",
2020-07-03 11:17:50 +08:00
"~/macklemary - (ft jello) extreme!! .mp3": "macklemary - (ft jello) extreme!! ",
}
for k, v := range samples {
2020-07-23 15:15:39 +08:00
got := getName(k)
2020-07-03 11:17:50 +08:00
if got != v {
t.Errorf("GetName(%s); Expected %s got %s", k, v, got)
}
}
}
func TestDownloadedFilePath(t *testing.T) {
sample := `[youtube] jJPMnTXl63E: Downloading webpage
[download] Destination: /tmp/Powfu - death bed (coffee for your head) (Official Video) ft. beabadoobee.webm
[download] 100%% of 2.54MiB in 00:0213MiB/s ETA 00:002
[ffmpeg] Destination: /tmp/Powfu - death bed (coffee for your head) (Official Video) ft. beabadoobee.mp3
Deleting original file /tmp/Powfu - death bed (coffee for your head) (Official Video) ft. beabadoobee.webm (pass -k to keep)`
result := "/tmp/Powfu - death bed (coffee for your head) (Official Video) ft. beabadoobee.mp3"
2020-07-12 17:04:33 +08:00
got := extractFilePath([]byte(sample), "/tmp")
2020-07-03 11:17:50 +08:00
if got != result {
t.Errorf("downloadedFilePath(%s); expected %s got %s", sample, result, got)
}
}
func TestEscapeBackSlash(t *testing.T) {
sample := map[string]string{
2020-07-10 22:05:36 +08:00
"/home/terra": "\\/home\\/terra",
2020-07-03 11:17:50 +08:00
"~/Documents/memes": "~\\/Documents\\/memes",
}
for k, v := range sample {
got := escapeBackSlash(k)
if got != v {
t.Errorf("escapeBackSlash(%s); expected %s, got %s", k, v, got)
}
}
}
2020-07-04 17:12:57 +08:00
func TestExpandTilde(t *testing.T) {
homeDir, err := os.UserHomeDir()
if err != nil {
t.Errorf("Unable to get home dir: %e", err)
}
2020-07-04 17:12:57 +08:00
sample := map[string]string{
2020-08-11 22:16:55 +08:00
"~/music": homeDir + "/music",
homeDir + "/Music": homeDir + "/Music",
2020-07-04 17:12:57 +08:00
}
for k, v := range sample {
got := expandTilde(k)
if got != v {
t.Errorf("expected %s; got %s", v, got)
}
}
}
2021-02-24 13:32:30 +08:00
func TestEmbedLyric(t *testing.T) {
testFile := "./test/sample"
lyricString := "[offset:1000]\n[00:12.000]Lyrics beginning ...\n[00:15.300]Some more lyrics ...\n"
2021-02-24 13:32:30 +08:00
descriptor := "en"
f, err := os.Create(testFile)
if err != nil {
t.Error(err)
}
f.Close()
2021-03-04 12:33:09 +08:00
defer func() {
2021-02-24 13:32:30 +08:00
err := os.Remove(testFile)
if err != nil {
t.Error(err)
}
}()
2021-03-26 00:48:01 +08:00
var lyric lyric.Lyric
err = lyric.NewFromLRC(lyricString)
if err != nil {
t.Error(err)
}
fmt.Println(lyric)
lyric.LangExt = descriptor
2021-02-24 13:32:30 +08:00
err = embedLyric(testFile, &lyric, false)
2021-02-24 13:32:30 +08:00
if err != nil {
t.Error(err)
}
tag, err := id3v2.Open(testFile, id3v2.Options{Parse: true})
if err != nil {
t.Error(err)
} else if tag == nil {
t.Error("unable to read tag")
}
usltFrames := tag.GetFrames(tag.CommonID("Unsynchronised lyrics/text transcription"))
frame, ok := usltFrames[0].(id3v2.UnsynchronisedLyricsFrame)
if !ok {
t.Error("invalid type")
}
assert.Equal(t, lyricString, frame.Lyrics)
2021-02-24 13:32:30 +08:00
assert.Equal(t, descriptor, frame.ContentDescriptor)
}