-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildin_string_test.go
150 lines (144 loc) · 3.27 KB
/
buildin_string_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
package template
import (
"testing"
)
func TestStringFilters(t *testing.T) {
cases := []struct {
name string
template string
context map[string]interface{}
expected string
}{
{
name: "DefaultFilter",
template: "{{ name | default:'Unknown' }}",
context: map[string]interface{}{"name": ""},
expected: "Unknown",
},
{
name: "TrimFilter",
template: "{{ ' hello ' | trim }}",
expected: "hello",
},
{
name: "SplitFilter",
template: "{{ 'one,two,three' | split:',' | size }}",
expected: "3",
},
{
name: "ReplaceFilter",
template: "{{ 'hello world' | replace:'world','there' }}",
expected: "hello there",
},
{
name: "RemoveFilter",
template: "{{ 'hello world' | remove:' world' }}",
expected: "hello",
},
{
name: "AppendFilter",
template: "{{ 'hello' | append:' world' }}",
expected: "hello world",
},
{
name: "PrependFilter",
template: "{{ 'world' | prepend:'hello ' }}",
expected: "hello world",
},
{
name: "LengthFilter",
template: "{{ 'hello' | length }}",
expected: "5",
},
{
name: "UpperFilter",
template: "{{ 'hello' | upper }}",
expected: "HELLO",
},
{
name: "LowerFilter",
template: "{{ 'HELLO' | lower }}",
expected: "hello",
},
{
name: "TitleizeFilter",
template: "{{ 'hello world' | titleize }}",
expected: "Hello World",
},
{
name: "CapitalizeFilter",
template: "{{ 'hello' | capitalize }}",
expected: "Hello",
},
{
name: "CamelizeFilter",
template: "{{ 'hello_world' | camelize }}",
expected: "helloWorld",
},
{
name: "PascalizeFilter",
template: "{{ 'hello_world' | pascalize }}",
expected: "HelloWorld",
},
{
name: "DasherizeFilter",
template: "{{ 'hello world' | dasherize }}",
expected: "hello-world",
},
{
name: "SlugifyFilter",
template: "{{ 'Hello Wörld & Friends' | slugify }}",
expected: "hello-world-and-friends",
},
{
name: "PluralizeFilterSingular",
template: "{{ count | pluralize:'apple','apples' }}",
context: map[string]interface{}{"count": 1},
expected: "apple",
},
{
name: "PluralizeFilterPlural",
template: "{{ count | pluralize:'apple','apples' }}",
context: map[string]interface{}{"count": 2},
expected: "apples",
},
{
name: "OrdinalizeFilter",
template: "{{ '1' | ordinalize }}",
expected: "1st",
},
{
name: "TruncateFilter",
template: "{{ 'hello world' | truncate:5 }}",
expected: "hello...",
},
{
name: "TruncateWordsFilter",
template: "{{ 'hello beautiful world' | truncateWords:2 }}",
expected: "hello beautiful...",
},
}
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)
}
})
}
}