-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfet_test.go
119 lines (114 loc) · 3.78 KB
/
fet_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
package fet
import (
"strings"
"testing"
"github.com/fefit/fet/types"
"github.com/stretchr/testify/assert"
)
func TestCompile(t *testing.T) {
curConf := &Config{
Mode: types.Smarty,
TemplateDir: "tests/smarty/templates",
CompileDir: "tests/smarty/views",
Ignores: []string{"tests/smarty/templates/inc"},
LeftDelimiter: "{%",
RightDelimiter: "%}",
UcaseField: true,
AutoRoot: true,
}
fet, _ := New(curConf)
assertOutputToBe := func(t *testing.T, tpl string, data interface{}, output string) {
result, err := fet.Fetch(tpl, data)
assert.Nil(t, err)
assert.Equal(t, output, strings.TrimSpace(result))
}
// test for syntax
helloFet := "hello fet!"
helloFetChars := strings.Split(helloFet, "")
t.Run("Test Smarty mode compile", func(t *testing.T) {
assertOutputToBe(t, "hello.tpl", nil, helloFet)
assertOutputToBe(t, "variable.tpl", nil, helloFet)
assertOutputToBe(t, "strvar.tpl", nil, helloFet+helloFet)
assertOutputToBe(t, "keywordvar.tpl", nil, helloFet)
assertOutputToBe(t, "concat.tpl", nil, helloFet)
// rewrite foreach
assertOutputToBe(t, "foreach.tpl", map[string][]string{
"Result": helloFetChars,
}, helloFet)
assertOutputToBe(t, "for.tpl", map[string][]string{
"Result": helloFetChars,
}, helloFet)
assertOutputToBe(t, "slice.tpl", map[string][]string{
"Result": helloFetChars,
}, "hello")
// maths
assertOutputToBe(t, "plus.tpl", nil, "5,5,5,5")
assertOutputToBe(t, "minus.tpl", nil, "0,0,0,0")
assertOutputToBe(t, "multiple.tpl", nil, "24,24,24,24")
assertOutputToBe(t, "divide.tpl", nil, "8,8,8,8")
assertOutputToBe(t, "minmax.tpl", nil, "1,2")
assertOutputToBe(t, "mod.tpl", nil, "1.15")
assertOutputToBe(t, "power.tpl", nil, "1024")
// pipe
assertOutputToBe(t, "pipe.tpl", nil, "2021-09-05 18:07:06")
// comment
assertOutputToBe(t, "comment.tpl", nil, "")
// include
assertOutputToBe(t, "include.tpl", map[string]string{
"Header": "hello",
"Footer": "fet",
}, "header:hello;footer:fet")
assertOutputToBe(t, "include_props.tpl", nil, "header:hello;footer:fet")
// extends
assertOutputToBe(t, "extends.tpl", nil, "(header)\n(content)\n(footer)")
assertOutputToBe(t, "extends_override.tpl", nil, "(override:header)\n(content)\n(footer)")
// if condition
assertOutputToBe(t, "condition.tpl", map[string]int{
"Number": 1,
}, "bigger")
assertOutputToBe(t, "condition.tpl", map[string]int{
"Number": 0,
}, "equal")
assertOutputToBe(t, "condition.tpl", map[string]int{
"Number": -1,
}, "smaller")
// compare
assertOutputToBe(t, "compare.tpl", map[string]interface{}{
"Result": map[string]int{
"Errno": 100,
},
}, helloFet)
})
}
func TestImportsDepends(t *testing.T) {
var initImports = func() Imports {
return Imports{
Nodes: map[string]*ImportNode{},
}
}
// A -> A depend itself
imports := initImports()
assert.True(t, len(imports.Add("A", "A")) > 0)
// A -> B B -> C C -> A
imports = initImports()
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("B", "C")) == 0)
assert.True(t, len(imports.Add("C", "A")) > 0)
// A -> B C -> A B -> C
imports = initImports()
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("C", "A")) == 0)
assert.True(t, len(imports.Add("B", "C")) > 0)
// C -> D A -> B D -> A B -> C
imports = initImports()
assert.True(t, len(imports.Add("C", "D")) == 0)
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("D", "A")) == 0)
assert.True(t, len(imports.Add("B", "C")) > 0)
// A -> B A -> C C -> D D -> A
imports = initImports()
assert.True(t, len(imports.Add("A", "B")) == 0)
assert.True(t, len(imports.Add("A", "C")) == 0)
assert.True(t, len(imports.Add("D", "A")) == 0)
assert.True(t, len(imports.Add("C", "D")) > 0)
}