package links
import (
"fmt"
"log"
"net/url"
"strings"
"testing"
)
type exampleType struct {
expectedCount int
rawBaseURL string
content string
}
var examples = []exampleType{
// Example 1
{2, "https://example.com", `
Ex 1
Example Page
Posts
`},
// Example 2
{4, "https://example.com", `
Ex 2
Example Page
Posts
A rouge link!
`},
// Example 3
{2, "https://example.com", `
Main Page
Example 1
Brandon's Blog
Post 1
Bad link
`},
// Example 4
{3, "https://example.com/example1", `
Back to homepage
Go to top of page
Second example
Boot Dev homepage
`},
}
func TestFindCountHrefs(t *testing.T) {
for i, ex := range examples {
name := fmt.Sprintf("Example %d", i+1)
baseURL, err := url.Parse(ex.rawBaseURL)
if err != nil {
t.Fatalf("can't parse %s: %v", ex.rawBaseURL, err)
}
t.Run(name, func(t *testing.T) {
r := strings.NewReader(ex.content)
hrefs, err := Parse(r, baseURL)
if err != nil {
t.Error(err)
}
if actualCount := len(hrefs); actualCount != ex.expectedCount {
t.Errorf("got %d, want %d", actualCount, ex.expectedCount)
log.Print(hrefs)
}
})
}
}