forked from bingoohuang/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
placeholder_test.go
69 lines (57 loc) · 1.96 KB
/
placeholder_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
package xlsx_test
import (
"testing"
"github.com/bingoohuang/xlsx"
"github.com/stretchr/testify/assert"
)
func TestParsePlaceholder(t *testing.T) {
assert.Equal(t, xlsx.PlaceholderValue{
Content: "Age",
Parts: []xlsx.PlaceholderPart{{Part: "Age"}},
}, xlsx.ParsePlaceholder("Age"))
assert.Equal(t, xlsx.PlaceholderValue{
Content: "{{name}}",
Parts: []xlsx.PlaceholderPart{{Part: "{{name}}", Var: "name"}},
}, xlsx.ParsePlaceholder("{{name}}"))
assert.Equal(t, xlsx.PlaceholderValue{
Content: "{{name}} {{ age }}",
Parts: []xlsx.PlaceholderPart{
{Part: "{{name}}", Var: "name"},
{Part: " ", Var: ""},
{Part: "{{ age }}", Var: "age"},
},
}, xlsx.ParsePlaceholder("{{name}} {{ age }}"))
assert.Equal(t, xlsx.PlaceholderValue{
Content: "Hello {{name}} world {{ age }}",
Parts: []xlsx.PlaceholderPart{
{Part: "Hello ", Var: ""},
{Part: "{{name}}", Var: "name"},
{Part: " world ", Var: ""},
{Part: "{{ age }}", Var: "age"},
},
}, xlsx.ParsePlaceholder("Hello {{name}} world {{ age }}"))
assert.Equal(t, xlsx.PlaceholderValue{
Content: "Age{{",
Parts: []xlsx.PlaceholderPart{
{Part: "Age{{", Var: ""},
},
}, xlsx.ParsePlaceholder("Age{{"))
plName := xlsx.ParsePlaceholder("{{name}}")
vars, ok := plName.ParseVars("bingoohuang")
assert.True(t, ok)
assert.Equal(t, map[string]string{"name": "bingoohuang"}, vars)
nameArgs := xlsx.ParsePlaceholder("{{name}} {{ age }}")
vars, ok = nameArgs.ParseVars("bingoohuang 100")
assert.True(t, ok)
assert.Equal(t, map[string]string{"name": "bingoohuang", "age": "100"}, vars)
nameArgs = xlsx.ParsePlaceholder("中国{{v1}}人民{{v2}}")
vars, ok = nameArgs.ParseVars("中国中央人民政府")
assert.True(t, ok)
assert.Equal(t, map[string]string{"v1": "中央", "v2": "政府"}, vars)
vars, ok = nameArgs.ParseVars("英国中央人民政府")
assert.False(t, ok)
assert.Nil(t, vars)
vars, ok = nameArgs.ParseVars("中国中央北京政府")
assert.False(t, ok)
assert.Nil(t, vars)
}