forked from issadarkthing/gomu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist_test.go
122 lines (87 loc) · 1.94 KB
/
playlist_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"os"
"path"
"path/filepath"
"testing"
"github.com/rivo/tview"
)
// Prepares for test
func prepareTest() *Gomu {
gomu := newGomu()
gomu.player = &Player{}
gomu.queue = newQueue()
gomu.playlist = &Playlist{
TreeView: tview.NewTreeView(),
}
gomu.app = tview.NewApplication()
rootDir, err := filepath.Abs("./test")
if err != nil {
panic(err)
}
root := tview.NewTreeNode("music")
rootAudioFile := &AudioFile{
name: root.GetText(),
path: rootDir,
}
root.SetReference(rootAudioFile)
populate(root, rootDir)
gomu.playlist.SetRoot(root)
return gomu
}
func TestPopulate(t *testing.T) {
gomu = newGomu()
rootDir, err := filepath.Abs("./test")
if err != nil {
panic(err)
}
expected := 0
walkFn := func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
expected++
return nil
}
f, e := os.Open(path)
if e != nil {
return e
}
defer f.Close()
expected++
return nil
}
// calculate the amount of mp3 files and directories
filepath.Walk(rootDir, walkFn)
root := tview.NewTreeNode(path.Base(rootDir))
root.SetReference(&AudioFile{
name: "Music",
isAudioFile: false,
})
populate(root, rootDir)
gotItems := 1
root.Walk(func(node, _ *tview.TreeNode) bool {
gotItems++
return true
})
if gotItems != expected {
t.Errorf("Invalid amount of file; expected %d got %d", expected, gotItems)
}
}
func TestAddAllToQueue(t *testing.T) {
gomu = prepareTest()
var songs []*tview.TreeNode
gomu.playlist.GetRoot().Walk(func(node, parent *tview.TreeNode) bool {
if node.GetReference().(*AudioFile).name == "rap" {
gomu.playlist.addAllToQueue(node)
}
return true
})
queue := gomu.queue.getItems()
for i, song := range songs {
audioFile := song.GetReference().(*AudioFile)
// strips the path of the song in the queue
s := filepath.Base(queue[i])
if audioFile.name != s {
t.Errorf("Expected \"%s\", got \"%s\"", audioFile.name, s)
}
}
}