summaryrefslogtreecommitdiff
path: root/internal/links/find_test.go
blob: e0866a5e28936ff488c7761dc8e2f92176227c5f (plain)
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
package links

import (
	"fmt"
	"strings"
	"testing"
)

type exampleType struct {
	expectedCount int
	content       string
}

var examples = []exampleType{
	{2, `
<html>c
  <head>
    <title>Ex 1</title>
  <head>
  <body>
    <a href="https://example.com/">Example Page</a>
    <a href="/posts">Posts</a>
  </body>
</html>
`},

	{4, `<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>
`},
}

func TestFindCountHrefs(t *testing.T) {
	for i, ex := range examples {
		name := fmt.Sprintf("Example %d", i+1)

		t.Run(name, func(t *testing.T) {
			r := strings.NewReader(ex.content)
			hrefs, err := parse(r)
			if err != nil {
				t.Error(err)
			}

			if actualCount := len(hrefs); actualCount != ex.expectedCount {
				t.Errorf("got %d, want %d", actualCount, ex.expectedCount)
			}
		})
	}
}