gomu/playlist_test.go

138 lines
2.4 KiB
Go
Raw Permalink Normal View History

2020-07-04 16:17:04 +08:00
package main
import (
"os"
"path"
"path/filepath"
"testing"
2021-02-26 11:18:58 +08:00
"github.com/issadarkthing/gomu/player"
2021-03-04 12:33:09 +08:00
"github.com/rivo/tview"
2020-07-04 16:17:04 +08:00
)
2020-07-06 18:58:27 +08:00
// Prepares for test
2020-07-17 15:34:50 +08:00
func prepareTest() *Gomu {
2020-07-04 16:17:04 +08:00
2020-07-23 15:15:39 +08:00
gomu := newGomu()
2021-02-26 11:18:58 +08:00
gomu.player = player.New(0)
2020-07-23 15:15:39 +08:00
gomu.queue = newQueue()
gomu.playlist = &Playlist{
2020-07-24 22:27:08 +08:00
TreeView: tview.NewTreeView(),
2020-07-04 16:17:04 +08:00
}
2020-07-23 15:15:39 +08:00
gomu.app = tview.NewApplication()
2020-07-04 16:17:04 +08:00
2021-02-15 20:32:50 +08:00
err := execConfig(expandFilePath(testConfigPath))
2021-02-13 22:49:26 +08:00
if err != nil {
panic(err)
}
gomu.colors = newColor()
2020-07-17 15:34:50 +08:00
rootDir, err := filepath.Abs("./test")
2020-07-04 16:17:04 +08:00
if err != nil {
panic(err)
}
root := tview.NewTreeNode("music")
2021-04-30 16:08:16 +08:00
rootAudioFile := new(player.AudioFile)
rootAudioFile.SetName(root.GetText())
rootAudioFile.SetPath(rootDir)
2020-07-04 16:17:04 +08:00
root.SetReference(rootAudioFile)
2021-03-14 21:24:57 +08:00
populate(root, rootDir, false)
2020-07-23 15:15:39 +08:00
gomu.playlist.SetRoot(root)
2020-07-04 16:17:04 +08:00
return gomu
}
func TestPopulate(t *testing.T) {
2020-07-23 15:15:39 +08:00
gomu = newGomu()
2021-02-15 20:32:50 +08:00
err := execConfig(expandFilePath(testConfigPath))
2021-02-13 22:49:26 +08:00
if err != nil {
t.Error(err)
}
gomu.colors = newColor()
2020-07-13 20:35:03 +08:00
rootDir, err := filepath.Abs("./test")
2020-07-04 16:17:04 +08:00
if err != nil {
panic(err)
}
2020-07-21 01:21:52 +08:00
expected := 0
walkFn := func(path string, info os.FileInfo, err error) error {
2020-07-06 17:02:59 +08:00
2020-07-04 16:17:04 +08:00
if info.IsDir() {
2020-07-21 01:21:52 +08:00
expected++
2020-07-04 16:17:04 +08:00
return nil
}
2020-07-06 17:02:59 +08:00
f, e := os.Open(path)
if e != nil {
return e
2020-07-04 16:17:04 +08:00
}
defer f.Close()
2020-07-22 21:01:13 +08:00
expected++
2020-07-04 16:17:04 +08:00
return nil
2020-07-21 01:21:52 +08:00
}
// calculate the amount of mp3 files and directories
filepath.Walk(rootDir, walkFn)
2020-07-04 16:17:04 +08:00
root := tview.NewTreeNode(path.Base(rootDir))
2021-04-30 16:08:16 +08:00
rootAudioFile := new(player.AudioFile)
rootAudioFile.SetName("Music")
rootAudioFile.SetIsAudioFile(false)
2020-07-13 14:35:12 +08:00
2021-03-14 21:24:57 +08:00
populate(root, rootDir, false)
2021-02-13 22:49:26 +08:00
gotItems := 0
2020-07-13 14:35:12 +08:00
root.Walk(func(node, _ *tview.TreeNode) bool {
2020-07-13 20:35:03 +08:00
gotItems++
2020-07-04 16:17:04 +08:00
return true
})
2021-02-15 20:32:50 +08:00
// ignore config, config.test and arbitrary_file.txt
gotItems += 3
2021-02-13 22:49:26 +08:00
2020-07-22 21:01:13 +08:00
if gotItems != expected {
t.Errorf("Invalid amount of file; expected %d got %d", expected, gotItems)
}
2020-07-04 16:17:04 +08:00
}
func TestAddAllToQueue(t *testing.T) {
2020-07-17 15:34:50 +08:00
gomu = prepareTest()
2020-07-06 18:58:27 +08:00
var songs []*tview.TreeNode
2020-07-04 16:17:04 +08:00
2020-07-23 15:15:39 +08:00
gomu.playlist.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
2020-07-04 16:17:04 +08:00
2021-04-30 16:08:16 +08:00
if node.GetReference().(*player.AudioFile).Name() == "rap" {
2020-07-23 15:15:39 +08:00
gomu.playlist.addAllToQueue(node)
2020-07-04 16:17:04 +08:00
}
return true
})
2020-07-23 15:15:39 +08:00
queue := gomu.queue.getItems()
2020-07-04 16:17:04 +08:00
2020-07-06 18:58:27 +08:00
for i, song := range songs {
2021-04-30 16:08:16 +08:00
audioFile := song.GetReference().(*player.AudioFile)
2020-07-06 18:58:27 +08:00
// strips the path of the song in the queue
s := filepath.Base(queue[i])
2021-04-30 16:08:16 +08:00
if audioFile.Name() != s {
t.Errorf("Expected \"%s\", got \"%s\"", audioFile.Name(), s)
2020-07-06 18:58:27 +08:00
}
2020-07-04 16:17:04 +08:00
}
2020-07-06 18:58:27 +08:00
2020-07-04 16:17:04 +08:00
}