-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader_test.go
More file actions
142 lines (132 loc) · 3.54 KB
/
header_test.go
File metadata and controls
142 lines (132 loc) · 3.54 KB
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
package ultrastar
import (
"errors"
"slices"
"strconv"
"testing"
)
func TestCanonicalHeaderKey(t *testing.T) {
tests := []struct{ key, expected string }{
{"TITLE", "TITLE"},
{"artist", "ARTIST"},
{"gEnRe", "GENRE"},
{"FOO-bar", "FOO-BAR"},
{"inval:d", ""},
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
actual := CanonicalHeaderKey(tt.key)
if actual != tt.expected {
t.Errorf("CanonicalHeaderKey(%q) = %q, expected %q", tt.key, actual, tt.expected)
}
})
}
}
func TestHeader_Add(t *testing.T) {
h := make(Header)
h.Add("title", "hello world")
if len(h[HeaderTitle]) != 1 || h[HeaderTitle][0] != "hello world" {
t.Errorf("h.Add(%q, %q) did not append the expected value", "title", "hello world")
}
}
func TestHeader_Set(t *testing.T) {
h := Header{
HeaderGenre: []string{"Rock", "Pop"},
}
h.Set(HeaderGenre, "Soul")
if vs, ok := h[HeaderGenre]; !ok || len(vs) != 1 || vs[0] != "Soul" {
t.Errorf("h.Set(%q, %q) did not replace the previous value", HeaderGenre, "Soul")
}
}
func TestHeader_SetValues(t *testing.T) {
h := make(Header)
h.SetValues("test", []string{"foo", "", "bar"})
if len(h["TEST"]) != 2 {
t.Errorf("h.SetValues(...) did not remove empty values, expected no empty values")
}
}
func TestHeader_Get(t *testing.T) {
h := Header{
HeaderAudio: []string{"Hello", "World"},
}
if h.Get("audio") == "" {
t.Errorf("h.Get(...) = \"\", expected non-empty string")
}
}
func TestHeader_Del(t *testing.T) {
h := Header{
HeaderArtist: []string{"Hello", "World"},
}
h.Del("foobar")
if len(h) != 1 {
t.Errorf("h.Del(...) deleted an element when it should not")
}
h.Del("artist")
if len(h) != 0 {
t.Errorf("h.Del(...) did not delete an element when it should have")
}
}
func TestHeader_Len(t *testing.T) {
h := Header{
HeaderArtist: []string{"", "Hello", ""},
"artist": []string{"World"},
"foo:bar": []string{"removed"},
"": []string{"removed"},
}
h.Clean()
if len(h) != 1 {
t.Errorf("h.Clean() left %d elements, expected 1", len(h))
}
if len(h[HeaderArtist]) != 2 {
t.Errorf("h.Clean() left %v, expected [Hello World]", h[HeaderArtist])
}
}
func TestHeader_Unique(t *testing.T) {
tests := []struct {
expected string
err error
values []string
}{
{"foo", nil, []string{"foo"}},
{"bar", nil, []string{"bar", "bar", ""}},
{"foo", nil, []string{"", "foo", ""}},
{"", nil, []string{}},
{"", nil, []string{"", ""}},
{"", ErrMultipleValues, []string{"foo", "bar"}},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
h := Header{
HeaderTitle: tt.values,
}
actual, err := h.GetUnique(HeaderTitle)
if tt.err != nil && !errors.Is(err, tt.err) {
t.Errorf("h.GetUnique(...) = _, %q, expected %q", err, tt.err)
} else if tt.err == nil && actual != tt.expected {
t.Errorf("h.GetUnique(...) = %q, _, expected %q", actual, tt.expected)
}
})
}
}
func TestHeader_GetMultiValued(t *testing.T) {
tests := []struct {
raw []string
expected []string
}{
{[]string{"foo,bar", "foobar"}, []string{"foo", "bar", "foobar"}},
{[]string{"foo,,,bar,"}, []string{"foo,", "bar"}},
{[]string{" bar , foo , , ,", ",foo,"}, []string{"bar", "foo", "foo"}},
{[]string{"Foo", ""}, []string{"Foo"}},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
h := Header{
HeaderGenre: tt.raw,
}
actual := slices.Collect(h.GetMultiValued(HeaderGenre))
if !slices.Equal(actual, tt.expected) {
t.Errorf("h.GetMultiValued(...) = %v, expected %v", actual, tt.expected)
}
})
}
}