package main
import (
"testing"
)
func TestParseArticles(t *testing.T) {
htmlContent := `
`
expectedArticles := []Article{
{Title: "Majorana, the search for the most elusive neutrino of all", Link: "https://newscenter.lbl.gov/2012/05/16/majorana-demonstrator/", Comments: 1, CommentsLink: "item?id=40477653"},
{Title: "Abusing Go's Infrastructure", Link: "https://reverse.put.as/2024/05/24/abusing-go-infrastructure/", Comments: 62, CommentsLink: "item?id=40474712"},
}
articles, err := parseArticles(htmlContent)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(articles) != len(expectedArticles) {
t.Fatalf("Expected %d articles, got %d", len(expectedArticles), len(articles))
}
for i, article := range articles {
if article.Title != expectedArticles[i].Title {
t.Errorf("Expected title %q, got %q", expectedArticles[i].Title, article.Title)
}
if article.Link != expectedArticles[i].Link {
t.Errorf("Expected link %q, got %q", expectedArticles[i].Link, article.Link)
}
if article.Comments != expectedArticles[i].Comments {
t.Errorf("Expected comments %d, got %d", expectedArticles[i].Comments, article.Comments)
}
if article.CommentsLink != expectedArticles[i].CommentsLink {
t.Errorf("Expected comments link %q, got %q", expectedArticles[i].CommentsLink, article.CommentsLink)
}
}
}
// func TestParseArticles(t *testing.T) {
// t.Run("Empty HTML", func(t *testing.T) {
// articles, err := parseArticles("")
// assert.NoError(t, err)
// assert.Empty(t, articles)
// })
// t.Run("Valid HTML", func(t *testing.T) {
// html := `
//
// |
//
// Article 1
//
// |
//
//
// |
//
// Article 2
//
// |
//
// `
// articles, err := parseArticles(html)
// assert.NoError(t, err)
// assert.Len(t, articles, 2)
// assert.Equal(t, "Article 1", articles[0].Title)
// assert.Equal(t, "https://example.com", articles[0].Link)
// assert.Equal(t, "Article 2", articles[1].Title)
// assert.Equal(t, "https://example.com/2", articles[1].Link)
// })
// t.Run("Error in goquery", func(t *testing.T) {
// doc := &goquery.Document{}
// // doc.SetError(fmt.Errorf("test error"))
// articles, err := parseArticlesFromDocument(doc)
// assert.Error(t, err)
// assert.Nil(t, articles)
// })
// }
// func parseArticlesFromDocument(doc *goquery.Document) ([]Article, error) {
// var articles []Article
// doc.Find("tr.athing").Each(func(i int, s *goquery.Selection) {
// title := s.Find("td.title > span.titleline > a").Text()
// link, _ := s.Find("td.title > span.titleline > a").Attr("href")
// commentsCount, err := extractCommentsCount(s)
// if err != nil {
// commentsCount = 0
// }
// article := Article{
// Title: title,
// Link: link,
// Comments: commentsCount,
// }
// articles = append(articles, article)
// })
// return articles, nil
// }