forked from kkdai/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_list_test.go
146 lines (130 loc) · 2.42 KB
/
format_list_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package youtube
import (
"testing"
"github.com/stretchr/testify/assert"
)
type filter struct {
Quality string
ItagNo int
MimeType string
Language string
AudioChannels int
}
func (list FormatList) Filter(filter filter) FormatList {
if filter.ItagNo > 0 {
list = list.Itag(filter.ItagNo)
}
if filter.AudioChannels > 0 {
list = list.AudioChannels(filter.AudioChannels)
}
if filter.Quality != "" {
list = list.Quality(filter.Quality)
}
if filter.MimeType != "" {
list = list.Type(filter.MimeType)
}
if filter.Language != "" {
list = list.Language(filter.Language)
}
return list
}
func TestFormatList_Filter(t *testing.T) {
t.Parallel()
format1 := Format{
ItagNo: 1,
Quality: "medium",
QualityLabel: "360p",
}
format2 := Format{
ItagNo: 2,
Quality: "large",
QualityLabel: "480p",
MimeType: `video/mp4; codecs="avc1.42001E, mp4a.40.2"`,
AudioChannels: 1,
}
formatStereo := Format{
ItagNo: 3,
URL: "stereo",
AudioChannels: 2,
}
list := FormatList{
format1,
format2,
formatStereo,
}
tests := []struct {
name string
args filter
want []Format
}{
{
name: "empty list with quality small",
args: filter{
Quality: "small",
},
},
{
name: "empty list with other itagNo",
args: filter{
ItagNo: 99,
},
},
{
name: "empty list with other mimeType",
args: filter{
MimeType: "other",
},
},
{
name: "empty list with other audioChannels",
args: filter{
AudioChannels: 7,
},
},
{
name: "audioChannels stereo",
args: filter{
AudioChannels: formatStereo.AudioChannels,
},
want: []Format{formatStereo},
},
{
name: "find by medium quality",
args: filter{
Quality: "medium",
},
want: []Format{format1},
},
{
name: "find by 480p",
args: filter{
Quality: "480p",
},
want: []Format{format2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
formats := list.Filter(tt.args)
if tt.want == nil {
assert.Empty(t, formats)
} else {
assert.Equal(t, tt.want, []Format(formats))
}
})
}
}
func TestFormatList_Sort(t *testing.T) {
t.Parallel()
list := FormatList{
{Width: 512},
{Width: 768, MimeType: "mp4"},
{Width: 768, MimeType: "opus"},
}
list.Sort()
assert.Equal(t, FormatList{
{Width: 768, MimeType: "mp4"},
{Width: 768, MimeType: "opus"},
{Width: 512},
}, list)
}