Debian Codegraph @

helpers/general_test.go

raw
1 // Copyright 2024 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 helpers_test
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 "github.com/gohugoio/hugo/helpers"
21 )
22
23 func TestResolveMarkup(t *testing.T) {
24 spec := newTestContentSpec(nil)
25
26 for i, this := range []struct {
27 in string
28 expect string
29 }{
30 {"md", "markdown"},
31 {"markdown", "markdown"},
32 {"mdown", "markdown"},
33 {"asciidocext", "asciidoc"},
34 {"adoc", "asciidoc"},
35 {"ad", "asciidoc"},
36 {"rst", "rst"},
37 {"pandoc", "pandoc"},
38 {"pdc", "pandoc"},
39 {"html", "html"},
40 {"htm", "html"},
41 {"org", "org"},
42 {"excel", ""},
43 } {
44 result := spec.ResolveMarkup(this.in)
45 if result != this.expect {
46 t.Errorf("[%d] got %s but expected %s", i, result, this.expect)
47 }
48 }
49 }
50
51 func TestFirstUpper(t *testing.T) {
52 for i, this := range []struct {
53 in string
54 expect string
55 }{
56 {"foo", "Foo"},
57 {"foo bar", "Foo bar"},
58 {"Foo Bar", "Foo Bar"},
59 {"", ""},
60 {"å", "Å"},
61 } {
62 result := helpers.FirstUpper(this.in)
63 if result != this.expect {
64 t.Errorf("[%d] got %s but expected %s", i, result, this.expect)
65 }
66 }
67 }
68
69 func TestHasStringsPrefix(t *testing.T) {
70 for i, this := range []struct {
71 s []string
72 prefix []string
73 expect bool
74 }{
75 {[]string{"a"}, []string{"a"}, true},
76 {[]string{}, []string{}, true},
77 {[]string{"a", "b", "c"}, []string{"a", "b"}, true},
78 {[]string{"d", "a", "b", "c"}, []string{"a", "b"}, false},
79 {[]string{"abra", "ca", "dabra"}, []string{"abra", "ca"}, true},
80 {[]string{"abra", "ca"}, []string{"abra", "ca", "dabra"}, false},
81 } {
82 result := helpers.HasStringsPrefix(this.s, this.prefix)
83 if result != this.expect {
84 t.Fatalf("[%d] got %t but expected %t", i, result, this.expect)
85 }
86 }
87 }
88
89 func TestHasStringsSuffix(t *testing.T) {
90 for i, this := range []struct {
91 s []string
92 suffix []string
93 expect bool
94 }{
95 {[]string{"a"}, []string{"a"}, true},
96 {[]string{}, []string{}, true},
97 {[]string{"a", "b", "c"}, []string{"b", "c"}, true},
98 {[]string{"abra", "ca", "dabra"}, []string{"abra", "ca"}, false},
99 {[]string{"abra", "ca", "dabra"}, []string{"ca", "dabra"}, true},
100 } {
101 result := helpers.HasStringsSuffix(this.s, this.suffix)
102 if result != this.expect {
103 t.Fatalf("[%d] got %t but expected %t", i, result, this.expect)
104 }
105 }
106 }
107
108 func TestSliceToLower(t *testing.T) {
109 t.Parallel()
110 tests := []struct {
111 value []string
112 expected []string
113 }{
114 {[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
115 {[]string{"a", "B", "c"}, []string{"a", "b", "c"}},
116 {[]string{"A", "B", "C"}, []string{"a", "b", "c"}},
117 }
118
119 for _, test := range tests {
120 res := helpers.SliceToLower(test.value)
121 for i, val := range res {
122 if val != test.expected[i] {
123 t.Errorf("Case mismatch. Expected %s, got %s", test.expected[i], res[i])
124 }
125 }
126 }
127 }
128
129 func TestGetTitleFunc(t *testing.T) {
130 title := "somewhere over the Rainbow"
131 c := qt.New(t)
132
133 c.Assert(helpers.GetTitleFunc("go")(title), qt.Equals, "Somewhere Over The Rainbow")
134 c.Assert(helpers.GetTitleFunc("chicago")(title), qt.Equals, "Somewhere over the Rainbow")
135 c.Assert(helpers.GetTitleFunc("Chicago")(title), qt.Equals, "Somewhere over the Rainbow")
136 c.Assert(helpers.GetTitleFunc("ap")(title), qt.Equals, "Somewhere Over the Rainbow")
137 c.Assert(helpers.GetTitleFunc("ap")(title), qt.Equals, "Somewhere Over the Rainbow")
138 c.Assert(helpers.GetTitleFunc("")(title), qt.Equals, "Somewhere Over the Rainbow")
139 c.Assert(helpers.GetTitleFunc("unknown")(title), qt.Equals, "Somewhere Over the Rainbow")
140 c.Assert(helpers.GetTitleFunc("none")(title), qt.Equals, title)
141 c.Assert(helpers.GetTitleFunc("firstupper")(title), qt.Equals, "Somewhere over the Rainbow")
142 }
143
144 func TestStringSliceToList(t *testing.T) {
145 for _, tt := range []struct {
146 slice []string
147 conjunction string
148 want string
149 }{
150 {[]string{}, "", ""},
151 {[]string{"foo"}, "", "foo"},
152 {[]string{"foo"}, "and", "foo"},
153 {[]string{"foo", "bar"}, "", "foo and bar"},
154 {[]string{"foo", "bar"}, "and", "foo and bar"},
155 {[]string{"foo", "bar"}, "or", "foo or bar"},
156 {[]string{"foo", "bar", "baz"}, "", "foo, bar, and baz"},
157 {[]string{"foo", "bar", "baz"}, "and", "foo, bar, and baz"},
158 {[]string{"foo", "bar", "baz"}, "or", "foo, bar, or baz"},
159 } {
160 got := helpers.StringSliceToList(tt.slice, tt.conjunction)
161 if got != tt.want {
162 t.Errorf("StringSliceToList() got: %q, want: %q", got, tt.want)
163 }
164 }
165 }