package feed_test import ( "testing" "time" "git.32bit.cafe/yequari/webweav.ing/feed" ) func TestTimeParsing(t *testing.T) { testDate, err := time.Parse("2006-Jan-02 15:04:05 -7", "2004-May-14 07:30:55 -7") if err != nil { t.Errorf("creating test date failed: %s", err) } var tests = []struct { name string input string format string }{ { "YYYY-MM-DD", `
hello world
`, time.DateOnly, }, { "YYYY-MM-DD HH:MM", `
hello world
`, "2006-01-02 15:04", }, { "YYYY-MM-DD HH:MM:SS", `
hello world
`, "2006-01-02 15:04:05", }, { "YYYY-MM-DDTHH:MM:SS", `
hello world
`, "2006-01-02T15:04:05", }, { "YYYY-MM-DDTHH:MM", `
hello world
`, "2006-01-02T15:04", }, } 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) } actualTime := item.PubTime.Format(tt.format) expectedTime := testDate.Format(tt.format) if actualTime != expectedTime { t.Errorf("got %s, want %s", actualTime, expectedTime) } }) } } 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", "
hello world
", &testDate, "
hello world
", }, } 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) } }) } }