diff --git a/feed_test.go b/feed_test.go new file mode 100644 index 0000000..ed96973 --- /dev/null +++ b/feed_test.go @@ -0,0 +1,48 @@ +package main + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" +) + +var feedSourceTests = []struct { + filename string + title string + itemText string +}{ + { + filename: "/xkcd.xml", + title: "xkcd.com", + itemText: "Occam", + }, +} + +func TestFeedSource(t *testing.T) { + server := httptest.NewServer(http.FileServer(http.Dir("testdata"))) + defer server.Close() + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + for _, tt := range feedSourceTests { + src := NewFeed(server.URL + tt.filename) + src.update(ctx) + if err := src.Error; err != nil { + t.Errorf("%v: Error = %v, expected nil", tt.filename, err) + continue + } + if src.Title != tt.title { + t.Errorf("%v: Title = %v expected %v", tt.filename, src.Title, tt.title) + } + items := src.GetItems() + if len(items) == 0 { + t.Errorf("%v: GetItems() = %v expected len > 0", tt.filename, items) + } else { + x := items[0] + if x.Text != tt.itemText { + t.Errorf("%v: item 0 Text = %q expected %q", tt.filename, x.Text, tt.itemText) + } + } + } +} diff --git a/testdata/xkcd.xml b/testdata/xkcd.xml new file mode 100644 index 0000000..e98c7c4 --- /dev/null +++ b/testdata/xkcd.xml @@ -0,0 +1,2 @@ + +xkcd.comhttps://xkcd.com/2021-11-12T00:00:00ZOccam2021-11-12T00:00:00Zhttps://xkcd.com/2541/<img src="https://imgs.xkcd.com/comics/occam.png" title="Oh no, Murphy just picked up the razor." alt="Oh no, Murphy just picked up the razor." />TTSLTSWBD2021-11-10T00:00:00Zhttps://xkcd.com/2540/<img src="https://imgs.xkcd.com/comics/ttsltswbd.png" title="Tomorrow's sessions will be entirely devoted to sewing machine rotary hooks." alt="Tomorrow's sessions will be entirely devoted to sewing machine rotary hooks." />Flinch2021-11-08T00:00:00Zhttps://xkcd.com/2539/<img src="https://imgs.xkcd.com/comics/flinch.png" title="Premed: &quot;Does this count for a physics credit? Can we shorten the string so I can get it done faster? And can we do one where it hits me in the face? I gotta do a thing for first aid training right after.&quot;" alt="Premed: &quot;Does this count for a physics credit? Can we shorten the string so I can get it done faster? And can we do one where it hits me in the face? I gotta do a thing for first aid training right after.&quot;" />Snack2021-11-05T00:00:00Zhttps://xkcd.com/2538/<img src="https://imgs.xkcd.com/comics/snack.png" title="Although grad students, suddenly reminded that food exists, tend to just grab and devour both without further discussion." alt="Although grad students, suddenly reminded that food exists, tend to just grab and devour both without further discussion." /> \ No newline at end of file