webweav.ing/feed/feed_test.go

83 lines
2.7 KiB
Go
Raw Normal View History

package feed_test
2024-01-19 03:33:37 +00:00
import "testing"
import "time"
import "git.32bit.cafe/yequari/rss-gen/feed"
2024-01-19 03:33:37 +00:00
func TestArticleParse(t *testing.T) {
testDate, err := time.Parse("2006-Jan-02", "2004-May-14")
if err != nil {
t.Errorf("creating test date failed: %s", err)
}
var tests = []struct {
name string
input string
want_time *time.Time
want_article string
}{
{
"article stripped out of basic HTML",
"<html><head></head><body><article>hello world</article></body></html>",
nil,
"<article>hello world</article>",
2024-01-19 03:33:37 +00:00
},
{
"article and time stripped out of basic HTML",
"<html><head></head><body><article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article></body></html>",
&testDate,
"<article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article>",
2024-01-19 03:33:37 +00:00
},
}
for _, tt := range tests {
t.Run(tt.name, func (t *testing.T) {
article, articleTime, err := feed.ParseArticle(tt.input)
2024-01-19 03:33:37 +00:00
if err != nil {
t.Errorf("error: %s", err)
}
if article != tt.want_article {
t.Errorf("got %s, want %s", article, tt.want_article)
}
if tt.want_time != nil && !articleTime.Equal(*tt.want_time) {
t.Errorf("got %s, want %s", articleTime, *tt.want_time)
}
})
}
}
func TestParseFeedItem(t *testing.T) {
testDate, err := time.Parse("2006-Jan-02", "2004-May-14")
if err != nil {
t.Errorf("creating test date failed: %s", err)
}
var tests = []struct {
name string
input string
want_time *time.Time
want_article string
}{
{
"article and time stripped out of basic HTML",
"<html><head></head><body><article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article></body></html>",
&testDate,
"<article><time datetime=\"2004-05-14\">May 14 2004</time>hello world</article>",
},
}
for _, tt := range tests {
t.Run(tt.name, func (t *testing.T) {
item := feed.FeedItem{}
err := item.ParseContent(tt.input)
if err != nil {
t.Errorf("error: %s", err)
}
if item.RawText != tt.want_article {
t.Errorf("got %s, want %s", item.RawText, tt.want_article)
}
if tt.want_time != nil && !item.PubTime.Equal(*tt.want_time) {
t.Errorf("got %s, want %s", item.PubTime, *tt.want_time)
}
})
}
}