-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_date_test.go
More file actions
130 lines (122 loc) · 3.32 KB
/
filter_date_test.go
File metadata and controls
130 lines (122 loc) · 3.32 KB
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
package template
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testTime creates a fixed time for consistent testing.
func testTime() time.Time {
return time.Date(2024, 3, 30, 15, 4, 5, 0, time.UTC)
}
func TestDateFilters(t *testing.T) {
current := testTime()
cases := []struct {
name string
template string
context map[string]any
expected string
}{
{
name: "FormattedDate",
template: "Formatted date: {{ current | date:'F j, Y' }}",
context: map[string]any{"current": current},
expected: "Formatted date: March 30, 2024",
},
{
name: "DayAndMonthNames",
template: "Day and month: {{ current | date:'l, F' }}",
context: map[string]any{"current": current},
expected: "Day and month: Saturday, March",
},
{
name: "TimeWithAmPm",
template: "Time: {{ current | date:'g:i A' }}",
context: map[string]any{"current": current},
expected: "Time: 3:04 PM",
},
{
name: "DateFormatWithoutFormat",
template: "Default date format: {{ current | date }}",
context: map[string]any{"current": current},
expected: "Default date format: 2024-03-30 15:04:05",
},
{
name: "WeekOfYearAndDayOfWeek",
template: "Week of year and day of week: {{ current | date:'W, N' }}",
context: map[string]any{"current": current},
expected: "Week of year and day of week: 13, 6",
},
{
name: "UnixTimestamp",
template: "Unix timestamp: {{ current | date:'S' }}",
context: map[string]any{"current": current},
expected: "Unix timestamp: 1711811045",
},
{
name: "TimeAgo",
template: "Time ago: {{ past | timeago }}",
context: map[string]any{
"past": time.Now().Add(-5 * 24 * time.Hour),
},
expected: "Time ago: 5 days ago",
},
{
name: "MonthAsNumber",
template: "Current month: {{ current | month }}",
context: map[string]any{"current": current},
expected: "Current month: 3",
},
{
name: "FullMonthName",
template: "Current month: {{ current | month_full }}",
context: map[string]any{"current": current},
expected: "Current month: March",
},
{
name: "Year",
template: "Current year: {{ current | year }}",
context: map[string]any{"current": current},
expected: "Current year: 2024",
},
{
name: "Day",
template: "Current day: {{ current | day }}",
context: map[string]any{"current": current},
expected: "Current day: 30",
},
{
name: "Weekday",
template: "Current day of the week: {{ current | weekday }}",
context: map[string]any{"current": current},
expected: "Current day of the week: Saturday",
},
{
name: "WeekFilter",
template: "Week: {{ current | week }}",
context: map[string]any{"current": current},
expected: "Week: 13",
},
{
name: "TimeAgoSnakeCase",
template: "Time ago: {{ past | time_ago }}",
context: map[string]any{
"past": time.Now().Add(-5 * 24 * time.Hour),
},
expected: "Time ago: 5 days ago",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
tpl, err := Compile(tc.template)
require.NoError(t, err)
ctx := NewContext()
for k, v := range tc.context {
ctx.Set(k, v)
}
got, err := tpl.Render(map[string]any(ctx))
require.NoError(t, err)
assert.Equal(t, tc.expected, got)
})
}
}