-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
292 lines (275 loc) · 6.3 KB
/
main_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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gofilters
import (
"math/rand"
"reflect"
"strings"
"testing"
"time"
)
func TestLineNumbers(t *testing.T) {
tests := []struct {
name string
value string
autoescape bool
want string
}{
{
name: "Test with autoescape false",
value: "Hello\nWorld",
autoescape: false,
want: "1. Hello\n2. World",
},
{
name: "Test with autoescape true",
value: "<Hello>\n<World>",
autoescape: true,
want: "1. <Hello>\n2. <World>",
},
{
name: "Test with single line",
value: "Hello",
autoescape: false,
want: "1. Hello",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := LineNumbers(tt.value, tt.autoescape); got != tt.want {
t.Errorf("LineNumbers() = %v, want %v", got, tt.want)
}
})
}
}
func TestDictsort(t *testing.T) {
tests := []struct {
name string
value []map[string]string
arg string
want []map[string]string
}{
{
name: "Test with unsorted dictionaries",
value: []map[string]string{
{"name": "banana", "color": "yellow"},
{"name": "apple", "color": "red"},
{"name": "grape", "color": "purple"},
},
arg: "name",
want: []map[string]string{
{"name": "apple", "color": "red"},
{"name": "banana", "color": "yellow"},
{"name": "grape", "color": "purple"},
},
},
{
name: "Test with already sorted dictionaries",
value: []map[string]string{
{"name": "apple", "color": "red"},
{"name": "banana", "color": "yellow"},
{"name": "grape", "color": "purple"},
},
arg: "name",
want: []map[string]string{
{"name": "apple", "color": "red"},
{"name": "banana", "color": "yellow"},
{"name": "grape", "color": "purple"},
},
},
{
name: "Test with dictionaries sorted by color",
value: []map[string]string{
{"name": "banana", "color": "yellow"},
{"name": "apple", "color": "red"},
{"name": "grape", "color": "purple"},
},
arg: "color",
want: []map[string]string{
{"name": "grape", "color": "purple"},
{"name": "apple", "color": "red"},
{"name": "banana", "color": "yellow"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := DictSort(tt.value, tt.arg); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Dictsort() = %v, want %v", got, tt.want)
}
})
}
}
func TestRandomItem(t *testing.T) {
rand.Seed(time.Now().UnixNano()) // Seed the random number generator
tests := []struct {
name string
value interface{}
}{
{
name: "Test with slice of integers",
value: []int{1, 2, 3, 4, 5},
},
{
name: "Test with slice of strings",
value: []string{"apple", "banana", "cherry"},
},
{
name: "Test with unsupported type",
value: 42,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := RandomItem(tt.value)
switch v := tt.value.(type) {
case []int:
if got != nil {
gotInt, ok := got.(int)
if !ok {
t.Errorf("RandomItem() returned a non-int value: %v", got)
} else if !containsInt(v, gotInt) {
t.Errorf("RandomItem() returned an integer not in the original slice: %v", gotInt)
}
}
case []string:
if got != nil {
gotStr, ok := got.(string)
if !ok {
t.Errorf("RandomItem() returned a non-string value: %v", got)
} else if !containsString(v, gotStr) {
t.Errorf("RandomItem() returned a string not in the original slice: %v", gotStr)
}
}
default:
if got != nil {
t.Errorf("RandomItem() should return nil for unsupported types, got: %v", got)
}
}
})
}
}
func TestDate(t *testing.T) {
tests := []struct {
name string
value string
format string
want string
wantErr bool
}{
{
name: "Test with date format",
value: "2021-01-02",
format: "Y-m-d",
want: "2021-01-02",
},
{
name: "Test with date and time format",
value: "2021-01-02 15:04:05",
format: "Y-m-d H:i:s",
want: "2021-01-02 15:04:05",
},
{
name: "Test with invalid date format",
value: "2021-01-02",
format: "invalid",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Date(tt.value, tt.format)
if got != tt.want {
t.Errorf("Date() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimeSince(t *testing.T) {
tests := []struct {
name string
value string
want string
}{
{
name: "Test with valid RFC3339 date",
value: time.Now().Add(-5 * time.Minute).Format(time.RFC3339),
want: "5 minutes",
},
{
name: "Test with valid RFC3339 date",
value: time.Now().Add(-1 * time.Hour).Format(time.RFC3339),
want: "1 hour",
},
{
name: "Test with valid RFC3339 date",
value: time.Now().Add(-1 * time.Second).Format(time.RFC3339),
want: "2 second",
},
{
name: "Test with invalid date",
value: "invalid date",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TimeSince(tt.value)
if !strings.HasPrefix(got, tt.want) {
t.Errorf("TimeSince() = %v, want prefix %v", got, tt.want)
}
})
}
}
func TestTimeUntil(t *testing.T) {
tests := []struct {
name string
value string
want string
}{
{
name: "Test with valid RFC3339 date",
value: time.Now().Add(5 * time.Minute).Format(time.RFC3339),
want: "5 minutes",
},
{
name: "Test with valid RFC3339 date",
value: time.Now().Add(1 * time.Hour).Format(time.RFC3339),
want: "1 hour",
},
{
name: "Test with valid RFC3339 date",
value: time.Now().Add(1 * time.Second).Format(time.RFC3339),
want: "1 second",
},
{
name: "Test with invalid date",
value: "invalid date",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TimeUntil(tt.value)
if !strings.HasPrefix(got, tt.want) {
t.Errorf("TimeUntil() = %v, want prefix %v", got, tt.want)
}
})
}
}
// Helper function to check if a slice of integers contains a specific integer
func containsInt(slice []int, item int) bool {
for _, a := range slice {
if a == item {
return true
}
}
return false
}
// Helper function to check if a slice of strings contains a specific string
func containsString(slice []string, item string) bool {
for _, a := range slice {
if a == item {
return true
}
}
return false
}