-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildin_number_test.go
66 lines (60 loc) · 1.49 KB
/
buildin_number_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
package template
import (
"testing"
)
func TestNumberFilters(t *testing.T) {
cases := []struct {
name string
template string
context map[string]interface{}
expected string
}{
{
name: "NumberFilterWithFormat",
template: "{{ value | number:'#,###.##' }}",
context: map[string]interface{}{"value": 1234567.89},
expected: "1,234,567.89",
},
{
name: "BytesFilterForKilobytes",
template: "{{ value | bytes }}",
context: map[string]interface{}{"value": 1024},
expected: "1.0 kB",
},
{
name: "BytesFilterForMegabytes",
template: "{{ value | bytes }}",
context: map[string]interface{}{"value": 1048576},
expected: "1.0 MB",
},
{
name: "BytesFilterForGigabytes",
template: "{{ value | bytes }}",
context: map[string]interface{}{"value": 1073741824},
expected: "1.1 GB",
},
}
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 matches the expected result
if output != tc.expected {
t.Errorf("Expected '%s', got '%s' for test case '%s'", tc.expected, output, tc.name)
}
})
}
}