-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_functions_test.go
241 lines (228 loc) · 6.25 KB
/
template_functions_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package partial
import (
"fmt"
"html/template"
"reflect"
"testing"
"time"
)
// Tests
func TestSafeHTML(t *testing.T) {
input := "<p>Hello, World!</p>"
expected := template.HTML("<p>Hello, World!</p>")
output := safeHTML(input)
if output != expected {
t.Errorf("safeHTML(%q) = %q; want %q", input, output, expected)
}
}
func TestTitle(t *testing.T) {
cases := []struct {
input string
expected string
}{
{"hello world", "Hello World"},
{"HELLO WORLD", "Hello World"},
{"go is awesome", "Go Is Awesome"},
{"", ""},
// Test cases with accented characters
{"élan vital", "Élan Vital"},
{"über cool", "Über Cool"},
{"façade", "Façade"},
{"mañana", "Mañana"},
{"crème brûlée", "Crème Brûlée"},
// Test cases with non-Latin scripts
{"россия", "Россия"}, // Russian (Cyrillic script)
{"中国", "中国"}, // Chinese characters
{"こんにちは 世界", "こんにちは 世界"}, // Japanese (Hiragana and Kanji)
{"مرحبا بالعالم", "مرحبا بالعالم"}, // Arabic script
{"γειά σου κόσμε", "Γειά Σου Κόσμε"}, // Greek script
// Test cases with mixed scripts
{"hello 世界", "Hello 世界"},
{"こんにちは world", "こんにちは World"},
}
for _, c := range cases {
output := title(c.input)
if output != c.expected {
t.Errorf("title(%q) = %q; want %q", c.input, output, c.expected)
}
}
}
func TestSubstr(t *testing.T) {
cases := []struct {
input string
start int
length int
expected string
}{
{"Hello, World!", 7, 5, "World"},
{"Hello, World!", 0, 5, "Hello"},
{"Hello, World!", 7, 20, "World!"},
{"Hello, World!", 20, 5, ""},
{"Hello, World!", 0, 0, ""},
}
for _, c := range cases {
output := substr(c.input, c.start, c.length)
if output != c.expected {
t.Errorf("substr(%q, %d, %d) = %q; want %q", c.input, c.start, c.length, output, c.expected)
}
}
}
func TestUcFirst(t *testing.T) {
cases := []struct {
input string
expected string
}{
{"hello world", "Hello world"},
{"Hello world", "Hello world"},
{"h", "H"},
{"", ""},
// Test cases with accented characters
{"élan vital", "Élan vital"},
{"über cool", "Über cool"},
{"façade", "Façade"},
{"mañana", "Mañana"},
{"crème brûlée", "Crème brûlée"},
// Test cases with non-Latin scripts
{"россия", "Россия"}, // Russian (Cyrillic script)
{"中国", "中国"}, // Chinese characters
{"こんにちは 世界", "こんにちは 世界"}, // Japanese (Hiragana and Kanji)
{"مرحبا بالعالم", "مرحبا بالعالم"}, // Arabic script
{"γειά σου κόσμε", "Γειά σου κόσμε"}, // Greek script
// Test cases with mixed scripts
{"hello 世界", "Hello 世界"},
{"こんにちは world", "こんにちは world"},
}
for _, c := range cases {
output := ucfirst(c.input)
if output != c.expected {
t.Errorf("ucfirst(%q) = %q; want %q", c.input, output, c.expected)
}
}
}
func TestFormatDate(t *testing.T) {
t1 := time.Date(2021, time.December, 31, 23, 59, 59, 0, time.UTC)
cases := []struct {
input time.Time
layout string
expected string
}{
{t1, "2006-01-02", "2021-12-31"},
{t1, "Jan 2, 2006", "Dec 31, 2021"},
{t1, time.RFC3339, "2021-12-31T23:59:59Z"},
}
for _, c := range cases {
output := formatDate(c.input, c.layout)
if output != c.expected {
t.Errorf("formatDate(%v, %q) = %q; want %q", c.input, c.layout, output, c.expected)
}
}
}
func TestParseDate(t *testing.T) {
cases := []struct {
layout string
value string
expected time.Time
wantErr bool
}{
{"2006-01-02", "2021-12-31", time.Date(2021, time.December, 31, 0, 0, 0, 0, time.UTC), false},
{"Jan 2, 2006", "Dec 31, 2021", time.Date(2021, time.December, 31, 0, 0, 0, 0, time.UTC), false},
{"2006-01-02", "invalid date", time.Time{}, true},
}
for _, c := range cases {
output, err := parseDate(c.layout, c.value)
if (err != nil) != c.wantErr {
t.Errorf("parseDate(%q, %q) error = %v; wantErr %v", c.layout, c.value, err, c.wantErr)
continue
}
if !c.wantErr && !output.Equal(c.expected) {
t.Errorf("parseDate(%q, %q) = %v; want %v", c.layout, c.value, output, c.expected)
}
}
}
func TestFirst(t *testing.T) {
cases := []struct {
input []any
expected any
}{
{[]any{1, 2, 3}, 1},
{[]any{"a", "b", "c"}, "a"},
{[]any{}, nil},
}
for _, c := range cases {
output := first(c.input)
if !reflect.DeepEqual(output, c.expected) {
t.Errorf("first(%v) = %v; want %v", c.input, output, c.expected)
}
}
}
func TestLast(t *testing.T) {
cases := []struct {
input []any
expected any
}{
{[]any{1, 2, 3}, 3},
{[]any{"a", "b", "c"}, "c"},
{[]any{}, nil},
}
for _, c := range cases {
output := last(c.input)
if !reflect.DeepEqual(output, c.expected) {
t.Errorf("last(%v) = %v; want %v", c.input, output, c.expected)
}
}
}
func TestHasKey(t *testing.T) {
cases := []struct {
input map[string]any
key string
expected bool
}{
{map[string]any{"a": 1, "b": 2}, "a", true},
{map[string]any{"a": 1, "b": 2}, "c", false},
{map[string]any{}, "a", false},
}
for _, c := range cases {
output := hasKey(c.input, c.key)
if output != c.expected {
t.Errorf("hasKey(%v, %q) = %v; want %v", c.input, c.key, output, c.expected)
}
}
}
func TestKeys(t *testing.T) {
cases := []struct {
input map[string]any
expected []string
}{
{map[string]any{"a": 1, "b": 2}, []string{"a", "b"}},
{map[string]any{}, []string{}},
}
for _, c := range cases {
output := keys(c.input)
if !equalStringSlices(output, c.expected) {
t.Errorf("keys(%v) = %v; want %v", c.input, output, c.expected)
}
}
}
// Helper function to compare slices regardless of order
func equalStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
aMap := make(map[string]int)
bMap := make(map[string]int)
for _, v := range a {
aMap[v]++
}
for _, v := range b {
bMap[v]++
}
return reflect.DeepEqual(aMap, bMap)
}
func TestDebug(t *testing.T) {
input := map[string]any{"a": 1, "b": "test"}
expected := fmt.Sprintf("%+v", input)
output := debug(input)
if output != expected {
t.Errorf("debug(%v) = %q; want %q", input, output, expected)
}
}