-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplate_test.go
96 lines (84 loc) · 1.54 KB
/
template_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
package template
import (
"bytes"
"github.com/achun/testing-want"
"os"
"path"
"path/filepath"
"strings"
"testing"
"text/template/parse"
)
var pwd = initPWD()
func initPWD() string {
pwd, err := os.Getwd()
if err != nil {
os.Exit(1)
}
return filepath.ToSlash(pwd)
}
func walkTree(node parse.Node) {
switch n := node.(type) {
case *parse.ListNode:
for _, node := range n.Nodes {
walkTree(node)
}
return
}
}
var uriTests = []string{
".",
"b",
"../a",
"../a/b",
"../a/b/",
"../a/b/./c/../../.././a",
`$`,
`$/.`,
`C:\a/..\a/b`,
`C:/a\\..\a/b/\`,
`#/a/..\a/b`,
`#/a/..\a/b/\`,
`\\/\#/a/..\a/b/\`,
`#/a\\/b///c\..\../\.././a`,
}
var fixtures = map[string]interface{}{
"title": `>title`,
"body": `/admin/body.html`,
"js": `/admin/js.tmpl`,
"href": ">>>",
"name": "admin",
}
func TestCleanURI(T *testing.T) {
wt := want.T(T)
wd, _ := os.Getwd()
wt.Equal(clearSlash(wd), pwd)
for _, s := range uriTests {
n := path.Clean(strings.Replace(s, "\\", "/", -1))
if n[0] == '.' {
n = ""
}
wt.Equal(cleanURI(s), n, s)
}
}
func TestTemplate(T *testing.T) {
var buf bytes.Buffer
wt := want.T(T)
t, err := New("./fixtures/base/layout.html")
wt.Nil(err)
wt.Equal(t.RootDir(), pwd+`/fixtures/base`)
wt.Equal(t.Name(), pwd+"/fixtures/base/layout.html")
t.Walk(pwd+`/fixtures/base`, ".html.tmpl")
wt.Nil(t.Execute(&buf, fixtures))
wt.Equal(buf.String(),
`<html>
<head>
<meta charset="UTF-8">
<script>"admin"</script>
</head>
<body>
<h1><a href="%3e%3e%3e">admin</a></h1>
</body>
<script>"foot"</script>
</html>`)
}