-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathm3uplus_test.go
107 lines (96 loc) · 2.42 KB
/
m3uplus_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
package proxytv
import (
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
type mockHandler struct {
playlistStartCalled bool
tracks []Track
playlistEndCalled bool
}
func (m *mockHandler) OnPlaylistStart() {
m.playlistStartCalled = true
}
func (m *mockHandler) OnTrack(t *Track) {
m.tracks = append(m.tracks, *t)
}
func (m *mockHandler) OnPlaylistEnd() {
m.playlistEndCalled = true
}
func TestDecodeM3u(t *testing.T) {
tests := []struct {
name string
input string
expected mockHandler
wantErr bool
}{
{
name: "Valid M3U",
input: `#EXTM3U
#EXTINF:-1 tvg-id="id1" tvg-name="name1",Channel 1
http://example.com/channel1
#EXTINF:-1 tvg-id="id2" tvg-name="name2",Channel 2
http://example.com/channel2`,
expected: mockHandler{
playlistStartCalled: true,
tracks: []Track{
{
Name: "Channel 1",
Length: 0,
URI: mustParseURL("http://example.com/channel1"),
Tags: map[string]string{"tvg-id": "id1", "tvg-name": "name1"},
},
{
Name: "Channel 2",
Length: 0,
URI: mustParseURL("http://example.com/channel2"),
Tags: map[string]string{"tvg-id": "id2", "tvg-name": "name2"},
},
},
playlistEndCalled: true,
},
wantErr: false,
},
{
name: "Invalid M3U (missing #EXTM3U)",
input: "Invalid content",
expected: mockHandler{},
wantErr: true,
},
{
name: "Missing EXTINF",
input: `#EXTM3U
http://example.com/channel1`,
expected: mockHandler{
playlistStartCalled: true,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := &mockHandler{}
err := loadM3u(strings.NewReader(tt.input), handler)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected.playlistStartCalled, handler.playlistStartCalled)
assert.Equal(t, tt.expected.playlistEndCalled, handler.playlistEndCalled)
assert.Equal(t, len(tt.expected.tracks), len(handler.tracks))
for i, expectedTrack := range tt.expected.tracks {
assert.Equal(t, expectedTrack.Name, handler.tracks[i].Name)
assert.Equal(t, expectedTrack.Length, handler.tracks[i].Length)
assert.Equal(t, expectedTrack.URI, handler.tracks[i].URI)
assert.Equal(t, expectedTrack.Tags, handler.tracks[i].Tags)
}
}
})
}
}
func mustParseURL(s string) *url.URL {
u, _ := url.Parse(s)
return u
}