From 918fff21c024d062fec9a15b03c3533c5dab4cff Mon Sep 17 00:00:00 2001 From: demo Date: Fri, 8 May 2026 12:32:26 -0400 Subject: test: start writing basic tests I've also moved the html to the findlinks package directory for now. --- internal/findlinks/findlinks_test.go | 51 ++++++++++++++++++++++++++++ internal/findlinks/html/ex1.html | 6 ++++ internal/findlinks/html/ex2.html | 17 ++++++++++ internal/findlinks/html/ex3.html | 62 ++++++++++++++++++++++++++++++++++ internal/findlinks/html/ex4.html | 5 +++ internal/findlinks/html/ex_spaces.html | 5 +++ 6 files changed, 146 insertions(+) create mode 100644 internal/findlinks/findlinks_test.go create mode 100644 internal/findlinks/html/ex1.html create mode 100644 internal/findlinks/html/ex2.html create mode 100644 internal/findlinks/html/ex3.html create mode 100644 internal/findlinks/html/ex4.html create mode 100644 internal/findlinks/html/ex_spaces.html (limited to 'internal') diff --git a/internal/findlinks/findlinks_test.go b/internal/findlinks/findlinks_test.go new file mode 100644 index 0000000..79fb241 --- /dev/null +++ b/internal/findlinks/findlinks_test.go @@ -0,0 +1,51 @@ +package findlinks + +import ( + "os" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func findLinksFile(filename string) ([]Link, error) { + f, err := os.Open(filename) + if err != nil { + panic("can't open test file") + } + defer f.Close() + + return FindLinks(f) +} + +func TestFindlinks(t *testing.T) { + type test struct { + filename string + links []Link + } + + tests := []test{ + { + "html/ex1.html", + []Link{ + { + Href: "/other-page", + Text: "A link to another page", + }, + }, + }, + } + + for _, test := range tests { + t.Run(test.filename, func(t *testing.T) { + links, err := findLinksFile(test.filename) + if err != nil { + t.Error(err) + } + + if !cmp.Equal(links, test.links) { + t.Errorf("got %v, want %v", links, test.links) + } + }) + } + +} diff --git a/internal/findlinks/html/ex1.html b/internal/findlinks/html/ex1.html new file mode 100644 index 0000000..833f110 --- /dev/null +++ b/internal/findlinks/html/ex1.html @@ -0,0 +1,6 @@ + + +

Hello!

+ A link to another page + + diff --git a/internal/findlinks/html/ex2.html b/internal/findlinks/html/ex2.html new file mode 100644 index 0000000..3d22f94 --- /dev/null +++ b/internal/findlinks/html/ex2.html @@ -0,0 +1,17 @@ + + + + + +

Social stuffs

+
+ + Check me out on twitter + + + + Gophercises is on Github! + +
+ + diff --git a/internal/findlinks/html/ex3.html b/internal/findlinks/html/ex3.html new file mode 100644 index 0000000..1e718e2 --- /dev/null +++ b/internal/findlinks/html/ex3.html @@ -0,0 +1,62 @@ + + + + + + + + + + + Gophercises - Coding exercises for budding gophers + + + +
+
+ +
+ +

coding exercises for budding gophers

+
+
+
+ + + Lost? Need help? +
+
+

Gophercises is 100% FREE, but is currently in beta. There will be bugs, and things will be changing significantly over the coming weeks.

+
+
+
+ + + diff --git a/internal/findlinks/html/ex4.html b/internal/findlinks/html/ex4.html new file mode 100644 index 0000000..0e893b8 --- /dev/null +++ b/internal/findlinks/html/ex4.html @@ -0,0 +1,5 @@ + + + dog cat + + diff --git a/internal/findlinks/html/ex_spaces.html b/internal/findlinks/html/ex_spaces.html new file mode 100644 index 0000000..943f405 --- /dev/null +++ b/internal/findlinks/html/ex_spaces.html @@ -0,0 +1,5 @@ + + + + + -- cgit v1.2.3