1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
package test
import (
"fmt"
"io"
"os"
"testing"
"git.brandonirizarry.xyz/links/internal/findlinks"
"github.com/google/go-cmp/cmp"
)
type parserFn func(io.Reader) ([]findlinks.Link, error)
func findLinksFile(filename string, parser parserFn) ([]findlinks.Link, error) {
f, err := os.Open(filename)
if err != nil {
panic("can't open test file")
}
defer f.Close()
return parser(f)
}
func TestFindlinks(t *testing.T) {
type test struct {
filename string
links []findlinks.Link
}
tests := []test{
{
"html/ex1.html",
[]findlinks.Link{
{
Href: "/other-page",
Text: "A link to another page",
},
},
},
{
"html/ex2.html",
[]findlinks.Link{
{
Href: "https://www.twitter.com/joncalhoun",
Text: "Check me out on twitter",
},
{
Href: "https://github.com/gophercises",
Text: "Gophercises is on Github !",
},
},
},
}
for _, test := range tests {
parsers := []parserFn{findlinks.Parse}
for i, p := range parsers {
testName := fmt.Sprintf("Parser %d %s", i+1, test.filename)
t.Run(testName, func(t *testing.T) {
links, err := findLinksFile(test.filename, p)
if err != nil {
t.Error(err)
}
if !cmp.Equal(links, test.links) {
t.Errorf("got %v, want %v", links, test.links)
}
})
}
}
}
|