package book import ( "testing" "time" ) func TestBody(t *testing.T) { config := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Body() want := "\n\n \n Books\n \n \n \n \n \n \n \n\n \n \n\n\n\n \n\n\n\n\n\n\n \n \n
\n \"John\n

Books

\n

\n
\n \n
\n
\n
\n \n
\n

Books

\n \n \n\n\n\n
\n\n
\n \n\n" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestName(t *testing.T) { config := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Name() want := "Books" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestCustomName(t *testing.T) { config := NewScrapeConfig() config.UseLinkName = true c := NewChapterFromURL("https://books.lapw.at/", "Custom Name", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Name() want := "Custom Name" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestAuthor(t *testing.T) { config := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Author() want := "John Doe" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestContent(t *testing.T) { config := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Content() want := "
\n \n
\n \n \n\n
\n \n\n
" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestDelay(t *testing.T) { config0 := NewScrapeConfig() config0.Delay = 500 config1 := NewScrapeConfig() start := time.Now() NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {}) elapsed := time.Since(start) got := elapsed want := time.Duration(500) * time.Millisecond if got < want { t.Errorf("got %v, wanted min %v", got, want) } } func TestContentImagesOnly(t *testing.T) { config := NewScrapeConfig() config.ImagesOnly = true c := NewChapterFromURL("https://books.lapw.at/posts/adam-wiggins-the-twelve-factor-app/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Content() want := "\"One\"A\"Code\"Scale" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestSubChapters(t *testing.T) { config0 := NewScrapeConfig() config1 := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {}) got := len(c.SubChapters()) want := 2 if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestSubChaptersSelector(t *testing.T) { config0 := NewScrapeConfig() config0.Selector = "section.concrete > article > h2 > a" config1 := NewScrapeConfig() c := NewChapterFromURL("https://12factor.net/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {}) got := len(c.SubChapters()) want := 12 if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestSubChaptersLimit(t *testing.T) { config0 := NewScrapeConfig() config0.Limit = 1 config1 := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {}) got := len(c.SubChapters()) want := 1 if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestSubChaptersLimitOver(t *testing.T) { config0 := NewScrapeConfig() config0.Limit = 3 config1 := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {}) got := len(c.SubChapters()) want := 2 if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestReverse(t *testing.T) { config0 := NewScrapeConfig() config0.Reverse = true config1 := NewScrapeConfig() c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config0, config1}, 0, func(index int, name string) {}) got := c.SubChapters()[0].Name() want := "The Twelve-Factor App" if got != want { t.Errorf("got %v, wanted %v", got, want) } } func TestNotInclude(t *testing.T) { config := NewScrapeConfig() config.Include = false c := NewChapterFromURL("https://books.lapw.at/", "", []*ScrapeConfig{config}, 0, func(index int, name string) {}) got := c.Content() want := "" if got != want { t.Errorf("got %v, wanted %v", got, want) } }