rss/rss_2.0_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
package rss import ( "fmt" "io/ioutil" "path/filepath" "testing" ) func TestParseItemLen(t *testing.T) { tests := map[string]int{ "rss_2.0": 2, "rss_2.0_content_encoded": 1, "rss_2.0_enclosure": 1, "rss_2.0-1": 4, "rss_2.0-1_enclosure": 1, } for test, want := range tests { name := filepath.Join("testdata", test) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } if len(feed.Items) != want { t.Errorf("%s: got %d, want %d", name, len(feed.Items), want) } } } func TestParseContent(t *testing.T) { tests := map[string]string{ "rss_2.0_content_encoded": "<p><a href=\"https://example.com/\">Example.com</a> is an example site.</p>", } for test, want := range tests { name := filepath.Join("testdata", test) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } if feed.Items[0].Content != want { t.Errorf("%s: got %s, want %s", name, feed.Items[0].Content, want) } } } func TestParseItemDateOK(t *testing.T) { tests := map[string]string{ "rss_2.0": "2009-09-06 16:45:00 +0000 UTC", "rss_2.0_content_encoded": "2009-09-06 16:45:00 +0000 UTC", "rss_2.0_enclosure": "2009-09-06 16:45:00 +0000 UTC", "rss_2.0-1": "2003-06-03 09:39:21 +0000 UTC", "rss_2.0-1_enclosure": "2016-05-14 15:39:34 +0000 UTC", } for test, want := range tests { name := filepath.Join("testdata", test) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } if !feed.Items[0].DateValid { t.Errorf("%s: date %q invalid!", name, feed.Items[0].Date) } else if got := feed.Items[0].Date.UTC().String(); got != want { t.Errorf("%s: got %q, want %q", name, got, want) } } } func TestParseItemDateFailure(t *testing.T) { tests := map[string]string{ "rss_2.0": "0001-01-01 00:00:00 +0000 UTC", } for test, want := range tests { name := filepath.Join("testdata", test) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } if fmt.Sprintf("%s", feed.Items[1].Date) != want { t.Errorf("%s: got %q, want %q", name, feed.Items[1].Date, want) } if feed.Items[1].DateValid { t.Errorf("%s: got unexpected valid date", name) } } } func TestParseCategories(t *testing.T) { tests := map[string]int{ "rss_2.0-1_enclosure": 2, "rss_2.0_enclosure": 0, } for test, want := range tests { name := filepath.Join("testdata", test) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } if len(feed.Items[0].Categories) != want { t.Errorf("%s: got %q, want %q", name, feed.Items[0].Categories, want) } } } func TestParseChannelCategories(t *testing.T) { tests := map[string]int{ "rss_2.0-1_enclosure": 2, "rss_2.0_enclosure": 1, } for test, want := range tests { name := filepath.Join("testdata", test) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } if len(feed.Categories) != want { t.Errorf("%s: got %q, want %q", name, feed.Items[0].Categories, want) } } } func TestChannelProperties(t *testing.T) { tests := []struct { name string testdata string verify func(t *testing.T, feed *Feed) }{{ name: "normal case", testdata: "rss_2.0_content_encoded", verify: func(t *testing.T, feed *Feed) { assertEqual("en", feed.Language, t) assertEqual("someone", feed.Author, t) }, }} for i := range tests { tt := tests[i] t.Run(tt.name, func(t *testing.T) { name := filepath.Join("testdata", tt.testdata) data, err := ioutil.ReadFile(name) if err != nil { t.Fatalf("Reading %s: %v", name, err) } feed, err := Parse(data) if err != nil { t.Fatalf("Parsing %s: %v", name, err) } tt.verify(t, feed) }) } } func assertEqual(expected, got string, t *testing.T) { if expected != got { t.Errorf("expect '%s', got '%s'", expected, got) } }