github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/markup/goldmark/autoid_test.go (about)

     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  		input := input
    78  		expect := expectlines[i]
    79  		c.Run(input, func(c *qt.C) {
    80  			b := []byte(input)
    81  			got := string(sanitizeAnchorName(b, goldmark_config.AutoHeadingIDTypeGitHub))
    82  			c.Assert(got, qt.Equals, expect)
    83  			c.Assert(sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub), qt.Equals, expect)
    84  			c.Assert(string(b), qt.Equals, input)
    85  		})
    86  	}
    87  }
    88  
    89  func TestSanitizeAnchorNameAsciiOnly(t *testing.T) {
    90  	c := qt.New(t)
    91  
    92  	c.Assert(sanitizeAnchorNameString("god is神真美好 good", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "god-is-good")
    93  	c.Assert(sanitizeAnchorNameString("Resumé", goldmark_config.AutoHeadingIDTypeGitHubAscii), qt.Equals, "resume")
    94  }
    95  
    96  func TestSanitizeAnchorNameBlackfriday(t *testing.T) {
    97  	c := qt.New(t)
    98  	c.Assert(sanitizeAnchorNameString("Let's try this, shall we?", goldmark_config.AutoHeadingIDTypeBlackfriday), qt.Equals, "let-s-try-this-shall-we")
    99  }
   100  
   101  func BenchmarkSanitizeAnchorName(b *testing.B) {
   102  	input := []byte("God is good: 神真美好")
   103  	b.ResetTimer()
   104  	for i := 0; i < b.N; i++ {
   105  		result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHub)
   106  		if len(result) != 24 {
   107  			b.Fatalf("got %d", len(result))
   108  		}
   109  	}
   110  }
   111  
   112  func BenchmarkSanitizeAnchorNameAsciiOnly(b *testing.B) {
   113  	input := []byte("God is good: 神真美好")
   114  	b.ResetTimer()
   115  	for i := 0; i < b.N; i++ {
   116  		result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeGitHubAscii)
   117  		if len(result) != 12 {
   118  			b.Fatalf("got %d", len(result))
   119  		}
   120  	}
   121  }
   122  
   123  func BenchmarkSanitizeAnchorNameBlackfriday(b *testing.B) {
   124  	input := []byte("God is good: 神真美好")
   125  	b.ResetTimer()
   126  	for i := 0; i < b.N; i++ {
   127  		result := sanitizeAnchorName(input, goldmark_config.AutoHeadingIDTypeBlackfriday)
   128  		if len(result) != 24 {
   129  			b.Fatalf("got %d", len(result))
   130  		}
   131  	}
   132  }
   133  
   134  func BenchmarkSanitizeAnchorNameString(b *testing.B) {
   135  	input := "God is good: 神真美好"
   136  	b.ResetTimer()
   137  	for i := 0; i < b.N; i++ {
   138  		result := sanitizeAnchorNameString(input, goldmark_config.AutoHeadingIDTypeGitHub)
   139  		if len(result) != 24 {
   140  			b.Fatalf("got %d", len(result))
   141  		}
   142  	}
   143  }