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 TestFirstLower(t *testing.T) {
70 c := qt.New(t)
71 for _, this := range []struct {
72 in string
73 expect string
74 }{
75 {"Foo", "foo"},
76 {"Foo Bar", "foo Bar"},
77 {"foo bar", "foo bar"},
78 {"", ""},
79 {"Å", "å"},
80 } {
81 c.Assert(helpers.FirstLower(this.in), qt.Equals, this.expect)
82 }
83 }
84
85 func TestHasStringsPrefix(t *testing.T) {
86 for i, this := range []struct {
87 s []string
88 prefix []string
89 expect bool
90 }{
91 {[]string{"a"}, []string{"a"}, true},
92 {[]string{}, []string{}, true},
93 {[]string{"a", "b", "c"}, []string{"a", "b"}, true},
94 {[]string{"d", "a", "b", "c"}, []string{"a", "b"}, false},
95 {[]string{"abra", "ca", "dabra"}, []string{"abra", "ca"}, true},
96 {[]string{"abra", "ca"}, []string{"abra", "ca", "dabra"}, false},
97 } {
98 result := helpers.HasStringsPrefix(this.s, this.prefix)
99 if result != this.expect {
100 t.Fatalf("[%d] got %t but expected %t", i, result, this.expect)
101 }
102 }
103 }
104
105 func TestHasStringsSuffix(t *testing.T) {
106 for i, this := range []struct {
107 s []string
108 suffix []string
109 expect bool
110 }{
111 {[]string{"a"}, []string{"a"}, true},
112 {[]string{}, []string{}, true},
113 {[]string{"a", "b", "c"}, []string{"b", "c"}, true},
114 {[]string{"abra", "ca", "dabra"}, []string{"abra", "ca"}, false},
115 {[]string{"abra", "ca", "dabra"}, []string{"ca", "dabra"}, true},
116 } {
117 result := helpers.HasStringsSuffix(this.s, this.suffix)
118 if result != this.expect {
119 t.Fatalf("[%d] got %t but expected %t", i, result, this.expect)
120 }
121 }
122 }
123
124 func TestSliceToLower(t *testing.T) {
125 t.Parallel()
126 tests := []struct {
127 value []string
128 expected []string
129 }{
130 {[]string{"a", "b", "c"}, []string{"a", "b", "c"}},
131 {[]string{"a", "B", "c"}, []string{"a", "b", "c"}},
132 {[]string{"A", "B", "C"}, []string{"a", "b", "c"}},
133 }
134
135 for _, test := range tests {
136 res := helpers.SliceToLower(test.value)
137 for i, val := range res {
138 if val != test.expected[i] {
139 t.Errorf("Case mismatch. Expected %s, got %s", test.expected[i], res[i])
140 }
141 }
142 }
143 }
144
145 func TestGetTitleFunc(t *testing.T) {
146 title := "somewhere over the Rainbow"
147 c := qt.New(t)
148
149 c.Assert(helpers.GetTitleFunc("go")(title), qt.Equals, "Somewhere Over The Rainbow")
150 c.Assert(helpers.GetTitleFunc("chicago")(title), qt.Equals, "Somewhere over the Rainbow")
151 c.Assert(helpers.GetTitleFunc("Chicago")(title), qt.Equals, "Somewhere over the Rainbow")
152 c.Assert(helpers.GetTitleFunc("ap")(title), qt.Equals, "Somewhere Over the Rainbow")
153 c.Assert(helpers.GetTitleFunc("ap")(title), qt.Equals, "Somewhere Over the Rainbow")
154 c.Assert(helpers.GetTitleFunc("")(title), qt.Equals, "Somewhere Over the Rainbow")
155 c.Assert(helpers.GetTitleFunc("unknown")(title), qt.Equals, "Somewhere Over the Rainbow")
156 c.Assert(helpers.GetTitleFunc("none")(title), qt.Equals, title)
157 c.Assert(helpers.GetTitleFunc("firstupper")(title), qt.Equals, "Somewhere over the Rainbow")
158 }
159
160 func TestStringSliceToList(t *testing.T) {
161 for _, tt := range []struct {
162 slice []string
163 conjunction string
164 want string
165 }{
166 {[]string{}, "", ""},
167 {[]string{"foo"}, "", "foo"},
168 {[]string{"foo"}, "and", "foo"},
169 {[]string{"foo", "bar"}, "", "foo and bar"},
170 {[]string{"foo", "bar"}, "and", "foo and bar"},
171 {[]string{"foo", "bar"}, "or", "foo or bar"},
172 {[]string{"foo", "bar", "baz"}, "", "foo, bar, and baz"},
173 {[]string{"foo", "bar", "baz"}, "and", "foo, bar, and baz"},
174 {[]string{"foo", "bar", "baz"}, "or", "foo, bar, or baz"},
175 } {
176 got := helpers.StringSliceToList(tt.slice, tt.conjunction)
177 if got != tt.want {
178 t.Errorf("StringSliceToList() got: %q, want: %q", got, tt.want)
179 }
180 }
181 }