add a test for FeedSource

master
magical 2022-01-01 01:00:34 +00:00
parent ad7a1edaf1
commit ddb93ee852
2 changed files with 50 additions and 0 deletions

48
feed_test.go 100644
View File

@ -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)
}
}
}
}

2
testdata/xkcd.xml vendored 100644
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title>xkcd.com</title><link href="https://xkcd.com/" rel="alternate"></link><id>https://xkcd.com/</id><updated>2021-11-12T00:00:00Z</updated><entry><title>Occam</title><link href="https://xkcd.com/2541/" rel="alternate"></link><updated>2021-11-12T00:00:00Z</updated><id>https://xkcd.com/2541/</id><summary type="html">&lt;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." /&gt;</summary></entry><entry><title>TTSLTSWBD</title><link href="https://xkcd.com/2540/" rel="alternate"></link><updated>2021-11-10T00:00:00Z</updated><id>https://xkcd.com/2540/</id><summary type="html">&lt;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." /&gt;</summary></entry><entry><title>Flinch</title><link href="https://xkcd.com/2539/" rel="alternate"></link><updated>2021-11-08T00:00:00Z</updated><id>https://xkcd.com/2539/</id><summary type="html">&lt;img src="https://imgs.xkcd.com/comics/flinch.png" title="Premed: &amp;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.&amp;quot;" alt="Premed: &amp;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.&amp;quot;" /&gt;</summary></entry><entry><title>Snack</title><link href="https://xkcd.com/2538/" rel="alternate"></link><updated>2021-11-05T00:00:00Z</updated><id>https://xkcd.com/2538/</id><summary type="html">&lt;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." /&gt;</summary></entry></feed>