2020-07-04 16:17:04 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/rivo/tview"
|
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
gomu.player = &Player{}
|
|
|
|
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
|
|
|
|
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")
|
|
|
|
rootAudioFile := &AudioFile{
|
2020-07-23 15:15:39 +08:00
|
|
|
name: root.GetText(),
|
|
|
|
path: rootDir,
|
2020-07-04 16:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
root.SetReference(rootAudioFile)
|
|
|
|
populate(root, rootDir)
|
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()
|
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))
|
|
|
|
|
2020-07-13 14:35:12 +08:00
|
|
|
root.SetReference(&AudioFile{
|
2020-07-23 15:15:39 +08:00
|
|
|
name: "Music",
|
|
|
|
isAudioFile: false,
|
2020-07-13 14:35:12 +08:00
|
|
|
})
|
|
|
|
|
2020-07-04 16:17:04 +08:00
|
|
|
populate(root, rootDir)
|
2020-07-22 21:01:13 +08:00
|
|
|
gotItems := 1
|
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
|
|
|
|
})
|
|
|
|
|
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
|
|
|
|
2020-07-23 15:15:39 +08:00
|
|
|
if node.GetReference().(*AudioFile).name == "rap" {
|
|
|
|
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 {
|
|
|
|
|
|
|
|
audioFile := song.GetReference().(*AudioFile)
|
|
|
|
|
|
|
|
// strips the path of the song in the queue
|
|
|
|
s := filepath.Base(queue[i])
|
|
|
|
|
2020-07-23 15:15:39 +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
|
|
|
}
|