-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplate_test.go
61 lines (49 loc) · 1.35 KB
/
simplate_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
package simplate
import (
"bytes"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
// template function
dataFormatFunc := func(t time.Time) string {
return t.UTC().Format("2006年01月02日03时04分05秒UTC")
}
AddFuncMap("dataFormat", dataFormatFunc)
// initial template
InitTemplate()
xm := m.Run()
os.Exit(xm)
}
func TestSimplateViewPathTemplates(t *testing.T) {
assert.NotEqual(t, 0, len(simplateViewPathTemplates))
a := []string{}
for k, v := range simplateViewPathTemplates {
a = append(a, k)
err := v.Execute(os.Stdout, make(map[string]interface{}))
assert.Nil(t, err)
}
assert.Contains(t, a, "home/body.tpl")
assert.Contains(t, a, "home/head.tpl")
assert.Contains(t, a, "home/index.html")
}
func TestExecuteTemplate(t *testing.T) {
timestamp := 1541222924
tm := time.Unix(int64(timestamp), 0)
data := make(map[string]interface{})
data["STime"] = tm
var buf bytes.Buffer
err := ExecuteTemplate(&buf, "home/index.html", data)
assert.Nil(t, err)
assert.Contains(t, buf.String(), "2018年11月03日05时28分44秒UTC")
}
func TestExecuteTemplateLayout(t *testing.T) {
data := make(map[string]interface{})
data["layout"] = "layout/home.html"
var buf bytes.Buffer
err := ExecuteTemplate(&buf, "home/head.tpl", data)
assert.Nil(t, err)
assert.Contains(t, buf.String(), "home.html")
}