diff options
Diffstat (limited to 'internal/links/find_count_test.go')
| -rw-r--r-- | internal/links/find_count_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
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", ` +<html>c + <head> + <title>Ex 1</title> + <head> + <body> + <a href="https://example.com/">Example Page</a> + <a href="/posts">Posts</a> + </body> +</html> +`}, + + // Example 2 + {4, "https://example.com", `<html> + <head> + <title>Ex 2</title> + <head> + <body> + <a href="https://example.com/">Example Page</a> + <a href="/posts">Posts</a> + <a href="#">A <a href="#">rouge</a> link!</a> + </body> +</html> +`}, + + // Example 3 + {2, "https://example.com", `<html> +<a href="https://example.com">Main Page</a> +<a href="/example1">Example 1</a> +<a href="https://brandonirizarry.xyz">Brandon's Blog</a> +<a href="https://brandonirizarry.xyz/post1">Post 1</a> +<a href=":foo">Bad link</a> +</html> +`}, +} + +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) + } + }) + } +} |
