2020-01-04 10:28:19 +00:00
|
|
|
|
// 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"
|
|
|
|
|
|
2020-01-05 10:52:00 +00:00
|
|
|
|
"github.com/gohugoio/hugo/markup/goldmark/goldmark_config"
|
|
|
|
|
|
2020-01-04 10:28:19 +00:00
|
|
|
|
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
|
2020-01-05 14:58:14 +00:00
|
|
|
|
Nonbreaking Space
|
|
|
|
|
Tab Space
|
2020-01-04 10:28:19 +00:00
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
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
|
2020-01-05 14:58:14 +00:00
|
|
|
|
nonbreakingspace
|
|
|
|
|
tabspace
|
2020-01-04 10:28:19 +00:00
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
tests, expect = strings.TrimSpace(tests), strings.TrimSpace(expect)
|
|
|
|
|
|
|
|
|
|
testlines, expectlines := strings.Split(tests, "\n"), strings.Split(expect, "\n")
|
|
|
|
|
|
2020-07-04 21:02:00 +00:00
|
|
|
|
testlines = append(testlines, "Trailing Space ")
|
|
|
|
|
expectlines = append(expectlines, "trailing-space")
|
|
|
|
|
|
2020-01-04 10:28:19 +00:00
|
|
|
|
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)
|
2020-01-05 10:52:00 +00:00
|
|
|
|
got := string(sanitizeAnchorName(b, goldmark_config.AutoHeadingIDTypeGitHub))
|
2020-01-04 10:28:19 +00:00
|
|
|
|
c.Assert(got, qt.Equals, expect)
|
2020-01-05 10:52:00 +00:00
|
|
|
|
c.Assert(sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub), qt.Equals, expect)
|
2020-01-04 10:28:19 +00:00
|
|
|
|
c.Assert(string(b), qt.Equals, input)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSanitizeAnchorNameAsciiOnly(t *testing.T) {
|
|
|
|
|
c := qt.New(t)
|
|
|
|
|
|
2020-01-05 10:52:00 +00:00
|
|
|
|
c.Assert(sanitizeAnchorNameString("god is神真美好 good", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "god-is-good")
|
|
|
|
|
c.Assert(sanitizeAnchorNameString("Resumé", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "resume")
|
|
|
|
|
|
|
|
|
|
}
|
2020-01-04 10:28:19 +00:00
|
|
|
|
|
2020-01-05 10:52:00 +00:00
|
|
|
|
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")
|
2020-01-04 10:28:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkSanitizeAnchorName(b *testing.B) {
|
|
|
|
|
input := []byte("God is good: 神真美好")
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-05 10:52:00 +00:00
|
|
|
|
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHub)
|
2020-01-04 10:28:19 +00:00
|
|
|
|
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++ {
|
2020-01-05 10:52:00 +00:00
|
|
|
|
result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHubAscii)
|
2020-01-04 10:28:19 +00:00
|
|
|
|
if len(result) != 12 {
|
|
|
|
|
b.Fatalf("got %d", len(result))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 10:52:00 +00:00
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 10:28:19 +00:00
|
|
|
|
func BenchmarkSanitizeAnchorNameString(b *testing.B) {
|
|
|
|
|
input := "God is good: 神真美好"
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-01-05 10:52:00 +00:00
|
|
|
|
result := sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub)
|
2020-01-04 10:28:19 +00:00
|
|
|
|
if len(result) != 24 {
|
|
|
|
|
b.Fatalf("got %d", len(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|