-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildin_date_test.go
126 lines (118 loc) · 3.42 KB
/
buildin_date_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
package template
import (
"testing"
"time"
)
// Helper function to create a test time for consistency across tests.
func testTime() time.Time {
return time.Date(2024, 3, 30, 15, 4, 5, 0, time.UTC)
}
func TestDateFilters(t *testing.T) {
// Mock current time for consistent testing
currentTime := testTime()
// Define test cases with template string and expected output
cases := []struct {
name string
template string
context map[string]interface{}
expected string
}{
{
name: "FormattedDate",
template: "Formatted date: {{ current | date:'F j, Y' }}",
context: map[string]interface{}{"current": currentTime},
expected: "Formatted date: March 30, 2024",
},
{
name: "DayAndMonthNames",
template: "Day and month: {{ current | date:'l, F' }}",
context: map[string]interface{}{"current": currentTime},
expected: "Day and month: Saturday, March",
},
{
name: "TimeWithAmPm",
template: "Time: {{ current | date:'g:i A' }}",
context: map[string]interface{}{"current": currentTime},
expected: "Time: 3:04 PM",
},
{
name: "DateFormatWithoutFormat",
template: "Default date format: {{ current | date }}",
context: map[string]interface{}{"current": currentTime},
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]interface{}{"current": currentTime},
expected: "Week of year and day of week: 13, 06",
},
{
name: "UnixTimestamp",
template: "Unix timestamp: {{ current | date:'U' }}",
context: map[string]interface{}{"current": currentTime},
expected: "Unix timestamp: 1711811045",
},
{
name: "TimeAgo",
template: "Time ago: {{ past | timeago }}",
context: map[string]interface{}{
"past": time.Now().Add(-5 * 24 * time.Hour),
},
expected: "Time ago: 5 days ago",
},
{
name: "MonthAsNumber",
template: "Current month: {{ current | month }}",
context: map[string]interface{}{"current": currentTime},
expected: "Current month: 3",
},
{
name: "FullMonthName",
template: "Current month: {{ current | month_full }}",
context: map[string]interface{}{"current": currentTime},
expected: "Current month: March",
},
{
name: "Year",
template: "Current year: {{ current | year }}",
context: map[string]interface{}{"current": currentTime},
expected: "Current year: 2024",
},
{
name: "Day",
template: "Current day: {{ current | day }}",
context: map[string]interface{}{"current": currentTime},
expected: "Current day: 30",
},
{
name: "Weekday",
template: "Current day of the week: {{ current | weekday }}",
context: map[string]interface{}{"current": currentTime},
expected: "Current day of the week: Saturday",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
// Parse the template
tpl, err := Parse(tc.template)
if err != nil {
t.Fatalf("Failed to parse template: %v", err)
}
// Create a context and add variables
context := NewContext()
for k, v := range tc.context {
context.Set(k, v)
}
// Execute the template
output, err := Execute(tpl, context)
if err != nil {
t.Fatalf("Failed to execute template: %v", err)
}
// Verify the output
if output != tc.expected {
t.Errorf("Expected '%s', got '%s'", tc.expected, output)
}
})
}
}