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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goldmark
import (
"strings"
"testing"
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
qt "github.com/frankban/quicktest"
)
func TestSanitizeAnchorName(t *testing.T) {
c := qt.New(t)
// Tests generated manually on github.com
tests := `
God is good: 神真美好
Number 32
Question?
1+2=3
Special !"#$%&(parens)=?´* chars
Resumé
One-Hyphen
Multiple--Hyphens
Trailing hyphen-
Many spaces here
Forward/slash
Backward\slash
Under_score
Nonbreaking Space
Tab Space
`
expect := `
god-is-good-神真美好
number-32
question
123
special-parens-chars
resumé
one-hyphen
multiple--hyphens
trailing-hyphen-
many---spaces--here
forwardslash
backwardslash
under_score
nonbreakingspace
tabspace
`
tests, expect = strings.TrimSpace(tests), strings.TrimSpace(expect)
testlines, expectlines := strings.Split(tests, "\n"), strings.Split(expect, "\n")
testlines = append(testlines, "Trailing Space ")
expectlines = append(expectlines, "trailing-space")
if len(testlines) != len(expectlines) {
panic("test setup failed")
}
for i, input := range testlines {
input := input
expect := expectlines[i]
c.Run(input, func(c *qt.C) {
b := []byte(input)
got := string(sanitizeAnchorName(b, goldmark_config.AutoHeadingIDTypeGitHub))
c.Assert(got, qt.Equals, expect)
c.Assert(sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub), qt.Equals, expect)
c.Assert(string(b), qt.Equals, input)
})
}
}
func TestSanitizeAnchorNameAsciiOnly(t *testing.T) {
c := qt.New(t)
c.Assert(sanitizeAnchorNameString("god is神真美好 good", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "god-is-good")
c.Assert(sanitizeAnchorNameString("Resumé", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "resume")
}
func TestSanitizeAnchorNameBlackfriday(t *testing.T) {
c := qt.New(t)
c.Assert(sanitizeAnchorNameString("Let's try this, shall we?", goldmark_config.AutoHeadingIDTypeBlackfriday), qt.Equals, "let-s-try-this-shall-we")
}
func BenchmarkSanitizeAnchorName(b *testing.B) {
input := []byte("God is good: 神真美好")
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHub)
if len(result) != 24 {
b.Fatalf("got %d", len(result))
}
}
}
func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) {
input := []byte("God is good: 神真美好")
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHubAscii)
if len(result) != 12 {
b.Fatalf("got %d", len(result))
}
}
}
func BenchmarkSanitizeAnchorNameBlackfriday(b *testing.B) {
input := []byte("God is good: 神真美好")
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeBlackfriday)
if len(result) != 24 {
b.Fatalf("got %d", len(result))
}
}
}
func BenchmarkSanitizeAnchorNameString(b *testing.B) {
input := "God is good: 神真美好"
b.ResetTimer()
for i := 0; i < b.N; i++ {
result := sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub)
if len(result) != 24 {
b.Fatalf("got %d", len(result))
}
}
}
|