From 3bfb19098c69a6b810a2a4e478f4184420bf4200 Mon Sep 17 00:00:00 2001 From: demo Date: Sun, 10 May 2026 22:47:08 -0400 Subject: feat: resolve hrefs according to a base URL --- internal/links/find_count_test.go | 76 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 internal/links/find_count_test.go (limited to 'internal/links/find_count_test.go') diff --git a/internal/links/find_count_test.go b/internal/links/find_count_test.go new file mode 100644 index 0000000..2932bf9 --- /dev/null +++ b/internal/links/find_count_test.go @@ -0,0 +1,76 @@ +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", ` +c + + 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 + +`}, +} + +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) + } + }) + } +} -- cgit v1.2.3