Debian Codegraph @

markup/goldmark/autoid_test.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package goldmark
15
16 import (
17 "strings"
18 "testing"
19
20 "github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
21
22 qt "github.com/frankban/quicktest"
23 )
24
25 func TestSanitizeAnchorName(t *testing.T) {
26 c := qt.New(t)
27
28 // Tests generated manually on github.com
29 tests := `
30 God is good: 神真美好
31 Number 32
32 Question?
33 1+2=3
34 Special !"#$%&(parens)=?´* chars
35 Resumé
36 One-Hyphen
37 Multiple--Hyphens
38 Trailing hyphen-
39 Many spaces here
40 Forward/slash
41 Backward\slash
42 Under_score
43 Nonbreaking Space
44 Tab Space
45 `
46
47 expect := `
48 god-is-good-神真美好
49 number-32
50 question
51 123
52 special-parens-chars
53 resumé
54 one-hyphen
55 multiple--hyphens
56 trailing-hyphen-
57 many---spaces--here
58 forwardslash
59 backwardslash
60 under_score
61 nonbreakingspace
62 tabspace
63 `
64
65 tests, expect = strings.TrimSpace(tests), strings.TrimSpace(expect)
66
67 testlines, expectlines := strings.Split(tests, "\n"), strings.Split(expect, "\n")
68
69 testlines = append(testlines, "Trailing Space ")
70 expectlines = append(expectlines, "trailing-space")
71
72 if len(testlines) != len(expectlines) {
73 panic("test setup failed")
74 }
75
76 for i, input := range testlines {
77 expect := expectlines[i]
78 c.Run(input, func(c *qt.C) {
79 b := []byte(input)
80 got := string(sanitizeAnchorName(b, goldmark_config.AutoIDTypeGitHub))
81 c.Assert(got, qt.Equals, expect)
82 c.Assert(sanitizeAnchorNameString(input, goldmark_config.AutoIDTypeGitHub), qt.Equals, expect)
83 c.Assert(string(b), qt.Equals, input)
84 })
85 }
86 }
87
88 func TestSanitizeAnchorNameAsciiOnly(t *testing.T) {
89 c := qt.New(t)
90
91 c.Assert(sanitizeAnchorNameString("god is神真美好 good", goldmark_config.AutoIDTypeGitHubAscii), qt.Equals, "god-is-good")
92 c.Assert(sanitizeAnchorNameString("Resumé", goldmark_config.AutoIDTypeGitHubAscii), qt.Equals, "resume")
93 }
94
95 func TestSanitizeAnchorNameBlackfriday(t *testing.T) {
96 c := qt.New(t)
97 c.Assert(sanitizeAnchorNameString("Let's try this, shall we?", goldmark_config.AutoIDTypeBlackfriday), qt.Equals, "let-s-try-this-shall-we")
98 }
99
100 func BenchmarkSanitizeAnchorName(b *testing.B) {
101 input := []byte("God is good: 神真美好")
102
103 for b.Loop() {
104 result := sanitizeAnchorName(input, goldmark_config.AutoIDTypeGitHub)
105 if len(result) != 24 {
106 b.Fatalf("got %d", len(result))
107 }
108 }
109 }
110
111 func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) {
112 input := []byte("God is good: 神真美好")
113
114 for b.Loop() {
115 result := sanitizeAnchorName(input, goldmark_config.AutoIDTypeGitHubAscii)
116 if len(result) != 12 {
117 b.Fatalf("got %d", len(result))
118 }
119 }
120 }
121
122 func BenchmarkSanitizeAnchorNameBlackfriday(b *testing.B) {
123 input := []byte("God is good: 神真美好")
124
125 for b.Loop() {
126 result := sanitizeAnchorName(input, goldmark_config.AutoIDTypeBlackfriday)
127 if len(result) != 24 {
128 b.Fatalf("got %d", len(result))
129 }
130 }
131 }
132
133 func BenchmarkSanitizeAnchorNameString(b *testing.B) {
134 input := "God is good: 神真美好"
135
136 for b.Loop() {
137 result := sanitizeAnchorNameString(input, goldmark_config.AutoIDTypeGitHub)
138 if len(result) != 24 {
139 b.Fatalf("got %d", len(result))
140 }
141 }
142 }