-
Notifications
You must be signed in to change notification settings - Fork 2
/
golyrics_test.go
269 lines (260 loc) · 5.96 KB
/
golyrics_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package golyrics
import (
"reflect"
"testing"
)
func Test_breakToNewLine(t *testing.T) {
type args struct {
HTML string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test should work for breaks in the start of string",
args: args{
"<br/>Hello World",
},
want: "\nHello World",
},
{
name: "test should work for breaks in the middle of string",
args: args{
"Hello<br/>World",
},
want: "Hello\nWorld",
},
{
name: "test should work for breaks in the end of string",
args: args{
"Hello World<br/>",
},
want: "Hello World\n",
},
}
for _, tt := range tests {
if got := breakToNewLine(tt.args.HTML); got != tt.want {
t.Errorf("%q. breakToNewLine() = %v, want %v", tt.name, got, tt.want)
}
}
}
func Test_stripeHTMLTags(t *testing.T) {
type args struct {
HTML string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test should work for different kinds of HTML tags",
args: args{
"<p>Hello World<br/>This is a TEST! :D</p>",
},
want: "Hello WorldThis is a TEST! :D",
},
}
for _, tt := range tests {
got := stripeHTMLTags(tt.args.HTML)
if got != tt.want {
t.Errorf("%q. stripeHTMLTags() = %v, want %v", tt.name, got, tt.want)
}
}
}
func Test_fixApostrophesAndQuotes(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test should work for apostrophes and quotes in the strings",
args: args{
"I'm not strong enough to stay away\nCan't run from "you"...",
},
want: "I'm not strong enough to stay away\nCan't run from \"you\"...",
},
}
for _, tt := range tests {
if got := fixApostrophesAndQuotes(tt.args.text); got != tt.want {
t.Errorf("%q. fixApostrophes() = %v, want %v", tt.name, got, tt.want)
}
}
}
func Test_getSearchURI(t *testing.T) {
type args struct {
query string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test should return right URI",
args: args{
"blackfield:pain",
},
want: "http://lyrics.wikia.com/index.php?action=ajax&rs=getLinkSuggest&format=json&query=blackfield%3Apain",
},
}
for _, tt := range tests {
if got := getSearchURI(tt.args.query); got != tt.want {
t.Errorf("%q. getSearchURI() = %v, want %v", tt.name, got, tt.want)
}
}
}
func Test_getFormattedLyrics(t *testing.T) {
type args struct {
text string
}
tests := []struct {
name string
args args
want string
}{
{
name: "test should return a correct formatted string from HTML lyrics",
args: args{
"<div class='lyricbox'>The shortest song in the universe<br/>Really isn't much fun<br/>It only has one puny verse<br/>. . . and then it's done!<div class='lyricsbreak'></div>\n</div>",
},
want: "The shortest song in the universe\nReally isn't much fun\nIt only has one puny verse\n. . . and then it's done!\n",
},
}
for _, tt := range tests {
got := getFormattedLyrics(tt.args.text)
if got != tt.want {
t.Errorf("%q. getFormattedLyrics() = %v, want %v", tt.name, got, tt.want)
}
}
}
func TestSearchTrack(t *testing.T) {
type args struct {
query string
}
tests := []struct {
name string
args args
want []Track
wantErr bool
}{
{
name: "test should return some results for some famous tracks",
args: args{
"metallica:unforgiven",
},
want: []Track{{
Artist: "Metallica",
Name: "Unforgiven",
}, {
Artist: "Metallica",
Name: "The Unforgiven II",
}},
wantErr: false,
},
{
name: "test should return no results for wrong input query",
args: args{
"sadasdasfdsfkdsjfgkrjferjkgnf,gfdngirjdgfmv",
},
want: []Track{},
wantErr: false,
},
}
for _, tt := range tests {
got, err := SearchTrack(tt.args.query)
if (err != nil) != tt.wantErr {
t.Errorf("%q. SearchLyrics() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("%q. SearchLyrics() = %v, want %v", tt.name, got, tt.want)
}
}
}
func TestSearchTrackByArtistAndName(t *testing.T) {
type args struct {
artist string
name string
}
tests := []struct {
name string
args args
want []Track
wantErr bool
}{
{
name: "test should return some results for some good real tracks",
args: args{
"blackfield",
"end of the world",
},
want: []Track{{
Artist: "Blackfield",
Name: "End Of The World",
}},
wantErr: false,
},
{
name: "test should return no results for unreal tracks",
args: args{
"kjskjajkdjkaskjdjka",
"kjjasdkjakjdjkajk",
},
want: []Track{},
wantErr: false,
},
}
for _, tt := range tests {
got, err := SearchTrackByArtistAndName(tt.args.artist, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("%q. SearchTrackByArtistAndName() error = %v, wantErr %v", tt.name, err, tt.wantErr)
continue
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("%q. SearchTrackByArtistAndName() = %v, want %v", tt.name, got, tt.want)
}
}
}
func TestTrack_FetchLyrics(t *testing.T) {
type fields struct {
Artist string
Name string
Lyrics string
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "should fetch lyrics for the track and sets it on it",
fields: fields{
Artist: "Sandra_Boynton",
Name: "The_Shortest_Song_In_The_Universe",
Lyrics: "The shortest song in the universe\nReally isn't much fun\nIt only has one puny verse\n... And then it's done!\n",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
track := &Track{
Artist: tt.fields.Artist,
Name: tt.fields.Name,
Lyrics: tt.fields.Lyrics,
}
if err := track.FetchLyrics(); (err != nil) != tt.wantErr {
t.Errorf("Track.FetchLyrics() error = %v, wantErr %v", err, tt.wantErr)
}
if track.Lyrics != tt.fields.Lyrics {
t.Errorf("%q. Track.FetchLyrics() = %v, want %v", tt.name, track.Lyrics, tt.fields.Lyrics)
}
})
}
}