forked from ushis/m3u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3u_test.go
124 lines (107 loc) · 3.33 KB
/
m3u_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
123
124
// The MIT License (MIT)
//
// Copyright (c) 2013 ushi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package m3u
import (
"bytes"
"io"
"os"
"testing"
)
var extended = Playlist{
Track{
Path: "Alternative\\everclear_SMFTA.mp3",
Title: "Everclear - So Much For The Afterglow",
Time: 233,
},
Track{
Path: "Comedy/Weird_Al_Everything_You_Know_Is_Wrong.mp3",
Title: "",
Time: 227,
},
Track{
Path: "Weird_Al_This_Is_The_Life.mp3",
Title: "Weird Al Yankovic - This is the Life",
Time: 187,
},
Track{
Path: "http://www.site.com/~user/gump.mp3",
Title: "Weird Al: Bad Hair Day - Gump",
Time: 129,
},
Track{
Path: "http://www.site.com:8000/listen.pls",
Title: "My Cool Stream",
Time: -1,
},
}
var simple = Playlist{
Track{Time: -1, Title: "", Path: "Alternative\\everclear_SMFTA.mp3"},
Track{Time: -1, Title: "", Path: "Comedy/Weird_Al_Everything_You_Know_Is_Wrong.mp3"},
Track{Time: -1, Title: "", Path: "Weird_Al_This_Is_The_Life.mp3"},
Track{Time: -1, Title: "", Path: "http://www.site.com/~user/gump.mp3"},
Track{Time: -1, Title: "", Path: "http://www.site.com:8000/listen.pls"},
}
func assertPlaylist(t *testing.T, a, b Playlist) {
if len(a) != len(b) {
t.Fatalf("Result: %v\nExpected: %v\n", a, b)
}
for i, _ := range a {
if a[i].Path != b[i].Path || a[i].Title != b[i].Title || a[i].Time != b[i].Time {
t.Fatalf("\nResult: %v\nExpected: %v\n", a, b)
}
}
}
func parse(t *testing.T, path string) Playlist {
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
pl, err := Parse(f)
if err != nil {
t.Fatal(err)
}
return pl
}
func writeAndParse(t *testing.T, w func(io.Writer) (int, error)) Playlist {
var buf bytes.Buffer
if _, err := w(&buf); err != nil {
t.Fatal(err)
}
pl, err := Parse(&buf)
if err != nil {
t.Fatal(err)
}
return pl
}
func TestParse(t *testing.T) {
assertPlaylist(t, parse(t, "testdata/extended.m3u"), extended)
}
func TestParseSimple(t *testing.T) {
assertPlaylist(t, parse(t, "testdata/simple.m3u"), simple)
}
func TestWriteTo(t *testing.T) {
assertPlaylist(t, writeAndParse(t, extended.WriteTo), extended)
}
func TestWriteSimpleTo(t *testing.T) {
assertPlaylist(t, writeAndParse(t, extended.WriteSimpleTo), simple)
}