-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_test.go
More file actions
150 lines (128 loc) · 3.15 KB
/
files_test.go
File metadata and controls
150 lines (128 loc) · 3.15 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package tmpl
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
type fataler interface {
Fatal(...interface{})
}
type templateFile struct {
name string
contents string
}
func createTestDir(t fataler, files []templateFile) string {
dir, err := ioutil.TempDir("", "template")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
fdir := filepath.Dir(file.name)
if err := os.MkdirAll(filepath.Join(dir, fdir), 0777); err != nil {
t.Fatal(err)
}
f, err := os.Create(filepath.Join(dir, file.name))
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = io.WriteString(f, file.contents)
if err != nil {
t.Fatal(err)
}
}
return dir
}
func TestFilesTemplate(t *testing.T) {
dir := createTestDir(t, []templateFile{
{"base.tmpl", "from a file!"},
})
defer os.RemoveAll(dir)
j := func(path string) string {
return filepath.Join(dir, path)
}
tmp := Parse(j("base.tmpl"))
for i := 0; i < 10; i++ {
var buf bytes.Buffer
if err := tmp.Execute(&buf, nil); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != "from a file!" {
t.Fatalf("\nExp %q\nGot %q", "from a file!", got)
}
}
}
func TestFilesTemplateWithBlocks(t *testing.T) {
dir := createTestDir(t, []templateFile{
{"base.tmpl", `{% evoke foo %}`},
{"foo.block", `{% block foo %}bar{% end block %}`},
})
defer os.RemoveAll(dir)
j := func(path string) string {
return filepath.Join(dir, path)
}
tmp := Parse(j("base.tmpl"))
tmp.Blocks(j("*.block"))
for i := 0; i < 10; i++ {
var buf bytes.Buffer
if err := tmp.Execute(&buf, nil); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != "bar" {
t.Fatalf("\nExp %q\nGot %q", "bar", got)
}
}
}
func TestFilesTemplateWithTempBlocks(t *testing.T) {
dir := createTestDir(t, []templateFile{
{"base.tmpl", `{% evoke foo %}`},
{"foo.block", `{% block foo %}bar{% end block %}`},
})
defer os.RemoveAll(dir)
j := func(path string) string {
return filepath.Join(dir, path)
}
tmp := Parse(j("base.tmpl"))
for i := 0; i < 10; i++ {
var buf bytes.Buffer
if err := tmp.Execute(&buf, nil, j("*.block")); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != "bar" {
t.Fatalf("\nExp %q\nGot %q", "bar", got)
}
}
//ensure test block doesn't stay
if err := tmp.Execute(ioutil.Discard, nil); err == nil {
t.Fatal("Expected error with no block definition")
}
}
func TestFilesTemplateWithBothBlocks(t *testing.T) {
dir := createTestDir(t, []templateFile{
{"base.tmpl", `{% evoke foo %}{% evoke bar %}`},
{"foo.block", `{% block foo %}bar{% end block %}`},
{"bar.block", `{% block bar %}baz{% end block %}`},
})
defer os.RemoveAll(dir)
j := func(path string) string {
return filepath.Join(dir, path)
}
tmp := Parse(j("base.tmpl"))
tmp.Blocks(j("foo.block"))
for i := 0; i < 10; i++ {
var buf bytes.Buffer
if err := tmp.Execute(&buf, nil, j("bar.block")); err != nil {
t.Fatal(err)
}
if got := buf.String(); got != "barbaz" {
t.Fatalf("\nExp %q\nGot %q", "barbaz", got)
}
}
//ensure test block doesn't stay
if err := tmp.Execute(ioutil.Discard, nil); err == nil {
t.Fatal("Expected error with no block definition")
}
}