generated from atomicgo/template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_test.go
45 lines (38 loc) · 1.09 KB
/
f_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
package f
import (
"testing"
"atomicgo.dev/assert"
)
type Person struct {
Name string
Age int
}
func TestFormat(t *testing.T) {
marvin := Person{Name: "Marvin", Age: 22}
tests := []struct {
template string
data any
want string
}{
{"Hello ${Name}", marvin, "Hello Marvin"},
{"${Name} is ${Age} years old", marvin, "Marvin is 22 years old"},
{"${Name} is ${Age} years old", map[string]any{"Name": "Marvin", "Age": 22}, "Marvin is 22 years old"},
{"${Name} is 22 years old? ${Age == 22}", marvin, "Marvin is 22 years old? true"},
{"${Name} is 22 years old? ${Age == 22 ? 'yes' : 'no'}", marvin, "Marvin is 22 years old? yes"},
{"${Name} is 60 years old? ${Age == 60 ? 'yes' : 'no'}", marvin, "Marvin is 60 years old? no"},
{"${1+2}", nil, "3"},
{"${1+2*3}", nil, "7"},
}
for _, tt := range tests {
t.Run(tt.template, func(t *testing.T) {
got, err := FormatSafe(tt.template, tt.data)
if err != nil {
t.Errorf("Format() error = %v", err)
return
}
if !assert.Equal(got, tt.want) {
t.Errorf("Format() = %v, want %v", got, tt.want)
}
})
}
}