-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
73 lines (69 loc) · 2.43 KB
/
main_test.go
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
package main
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_convertAnchorIDs(t *testing.T) {
tests := []struct {
// I initially wanted to use Hugo's SanitizeAnchorName, but it would
// require a complicated setup. Instead, I pretend that I'm Hugo and I
// do the conversion myself.
headingToAnchor map[string]string
given, expect string
}{
{
headingToAnchor: map[string]string{"`go get -u` vs. `go.mod` (= *_problem_*)": "go-get--u-vs-gomod--_problem_"},
given: "# `go get -u` vs. `go.mod` (= *_problem_*)\n" +
"[`go get -u` vs. `go.mod` (= *_problem_*)](#go-get--u-vs-gomod--_problem_)\n",
expect: "# `go get -u` vs. `go.mod` (= *_problem_*)\n" +
"[`go get -u` vs. `go.mod` (= *_problem_*)](#-raw-go-get-u-endraw-vs-raw-gomod-endraw-problem)\n",
},
{
// The closing endraw is always followed by a dash.
headingToAnchor: map[string]string{"`foo`": "foo"},
given: "# `foo`\n" +
"[`foo`](#foo)\n",
expect: "# `foo`\n" +
"[`foo`](#-raw-foo-endraw-)\n",
},
{
// Upper case letters are converted to lower case.
headingToAnchor: map[string]string{"Title Is CAPITAL": "title-is-capital"},
given: "### Title Is CAPITAL\n" +
"[Title Is CAPITAL](#title-is-capital)\n",
expect: "### Title Is CAPITAL\n" +
"[Title Is CAPITAL](#title-is-capital)\n",
},
{
// It should not affect the anchor links that were manually created.
headingToAnchor: map[string]string{"Docker proxy vs. Local registry": "docker-proxy-vs-local-registry"},
given: "## Docker proxy vs. Local registry\n" +
"[see this section](#docker-proxy-vs-local-registry)\n",
expect: "## Docker proxy vs. Local registry\n" +
"[see this section](#docker-proxy-vs-local-registry)\n",
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
assert.Equal(t, tt.expect, convertAnchorIDs("path/to/file.md", tt.given, func(s string) string {
return tt.headingToAnchor[s]
}))
})
}
}
func Test_addPostURLInHTMLImages(t *testing.T) {
tests := []struct {
given, expect string
}{
{
`<img alt="Super example" src="dnat-google-vpc-how-comes-back.svg" width="80%"/>`,
`<img alt="Super example" src="/you-should-write-comments/dnat-google-vpc-how-comes-back.svg" width="80%"/>`,
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
assert.Equal(t, tt.expect, addPostURLInHTMLImages(tt.given, "/you-should-write-comments/"))
})
}
}