Debian Codegraph @

tpl/strings/truncate_test.go

raw
1 // Copyright 2016 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 strings
15
16 import (
17 "html/template"
18 "reflect"
19 "strings"
20 "testing"
21 )
22
23 func TestTruncate(t *testing.T) {
24 t.Parallel()
25
26 var err error
27 cases := []struct {
28 v1 any
29 v2 any
30 v3 any
31 want any
32 isErr bool
33 }{
34 {10, "I am a test sentence", nil, template.HTML("I am a …"), false},
35 {10, "", "I am a test sentence", template.HTML("I am a"), false},
36 {10, "", "a b c d e f g h i j k", template.HTML("a b c d e"), false},
37 {12, "", "<b>Should be escaped</b>", template.HTML("&lt;b&gt;Should be"), false},
38 {10, template.HTML(" <a href='#'>Read more</a>"), "I am a test sentence", template.HTML("I am a <a href='#'>Read more</a>"), false},
39 {20, template.HTML("I have a <a href='/markdown'>Markdown link</a> inside."), nil, template.HTML("I have a <a href='/markdown'>Markdown …</a>"), false},
40 {10, "IamanextremelylongwordthatjustgoesonandonandonjusttoannoyyoualmostasifIwaswritteninGermanActuallyIbettheresagermanwordforthis", nil, template.HTML("Iamanextre …"), false},
41 {10, template.HTML("<p>IamanextremelylongwordthatjustgoesonandonandonjusttoannoyyoualmostasifIwaswritteninGermanActuallyIbettheresagermanwordforthis</p>"), nil, template.HTML("<p>Iamanextre …</p>"), false},
42 {13, template.HTML("With <a href=\"/markdown\">Markdown</a> inside."), nil, template.HTML("With <a href=\"/markdown\">Markdown …</a>"), false},
43 {14, "Hello中国 Good 好的", nil, template.HTML("Hello中国 Good 好 …"), false},
44 {12, "", "日本語でHugoを使います", template.HTML("日本語でHugoを使いま"), false},
45 {12, "", "日本語で Hugo を使います", template.HTML("日本語で Hugo を使"), false},
46 {8, "", "日本語でHugoを使います", template.HTML("日本語でHugo"), false},
47 {10, "", "日本語で Hugo を使います", template.HTML("日本語で Hugo"), false},
48 {9, "", "日本語で Hugo を使います", template.HTML("日本語で Hugo"), false},
49 {7, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
50 {7, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
51 {6, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
52 {6, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
53 {5, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
54 {5, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
55 {4, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
56 {4, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
57 {15, "", template.HTML("A <br> tag that's not closed"), template.HTML("A <br> tag that's"), false},
58 {14, template.HTML("<p>Hello中国 Good 好的</p>"), nil, template.HTML("<p>Hello中国 Good 好 …</p>"), false},
59 {2, template.HTML("<p>P1</p><p>P2</p>"), nil, template.HTML("<p>P1 …</p>"), false},
60 {3, template.HTML(strings.Repeat("<p>P</p>", 20)), nil, template.HTML("<p>P</p><p>P</p><p>P …</p>"), false},
61 {18, template.HTML("<p>test <b>hello</b> test something</p>"), nil, template.HTML("<p>test <b>hello</b> test …</p>"), false},
62 {4, template.HTML("<p>a<b><i>b</b>c d e</p>"), nil, template.HTML("<p>a<b><i>b</b>c …</p>"), false},
63 {
64 42,
65 template.HTML(`With strangely formatted
66 <a
67 href="#"
68 target="_blank"
69 >HTML</a
70 >
71 inside.`),
72 nil,
73 template.HTML(`With strangely formatted
74 <a
75 href="#"
76 target="_blank"
77 >HTML …</a>`),
78 false,
79 },
80 {10, nil, nil, template.HTML(""), true},
81 {nil, nil, nil, template.HTML(""), true},
82 }
83 for i, c := range cases {
84 var result template.HTML
85 if c.v2 == nil {
86 result, err = ns.Truncate(c.v1)
87 } else if c.v3 == nil {
88 result, err = ns.Truncate(c.v1, c.v2)
89 } else {
90 result, err = ns.Truncate(c.v1, c.v2, c.v3)
91 }
92
93 if c.isErr {
94 if err == nil {
95 t.Errorf("[%d] Slice didn't return an expected error", i)
96 }
97 } else {
98 if err != nil {
99 t.Errorf("[%d] failed: %s", i, err)
100 continue
101 }
102 if !reflect.DeepEqual(result, c.want) {
103 t.Errorf("[%d] got '%s' but expected '%s'", i, result, c.want)
104 }
105 }
106 }
107
108 // Too many arguments
109 _, err = ns.Truncate(10, " ...", "I am a test sentence", "wrong")
110 if err == nil {
111 t.Errorf("Should have errored")
112 }
113 }
114
115 func BenchmarkTruncate(b *testing.B) {
116 b.Run("Plain text", func(b *testing.B) {
117 for b.Loop() {
118 ns.Truncate(10, "I am a test sentence")
119 }
120 })
121
122 b.Run("With link", func(b *testing.B) {
123 for b.Loop() {
124 ns.Truncate(10, "I have a <a href='/markdown'>Markdown link</a> inside")
125 }
126 })
127 }