-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
fs_test.go
160 lines (133 loc) · 3.64 KB
/
fs_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
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
151
152
153
154
155
156
157
158
159
160
package blocks_test
import (
"bytes"
"strings"
"testing"
"github.com/kataras/blocks"
)
func TestMemoryFileSystem(t *testing.T) {
// Create a new MemoryFileSystem
mfs := blocks.NewMemoryFileSystem()
// Define template contents
mainTemplateContent := []byte(`
<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
{{ template "content" . }}
{{ partial "custom/user/partial" . }}
</body>
</html>
`)
contentTemplateContent := []byte(`{{ define "content" }}Hello, {{ .Name }}!{{ end }}`)
partialTemplateContent := []byte(`<h3>Partial</h3>`)
// Parse templates into the memory file system
err := mfs.ParseTemplate("layouts/main.html", mainTemplateContent, nil)
if err != nil {
t.Fatalf("Failed to parse main.html: %v", err)
}
err = mfs.ParseTemplate("index.html", contentTemplateContent, nil)
if err != nil {
t.Fatalf("Failed to parse index.html: %v", err)
}
err = mfs.ParseTemplate("custom/user/partial.html", partialTemplateContent, nil)
if err != nil {
t.Fatalf("Failed to parse partial.html: %v", err)
}
// Create a new Blocks instance using the MemoryFileSystem
views := blocks.New(mfs)
// Set the main layout file
views.DefaultLayout("main")
// Load the templates
err = views.Load()
if err != nil {
t.Fatalf("Failed to load templates: %v", err)
}
// Data for template execution
data := map[string]any{
"Title": "Test Page",
"Name": "World",
}
// Execute the template
var buf bytes.Buffer
err = views.ExecuteTemplate(&buf, "index", "", data)
if err != nil {
t.Fatalf("Failed to execute template: %v", err)
}
// Expected output
expectedOutput := `<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
</head>
<body>
Hello, World! <h3>Partial</h3>
</body>
</html>`
// Trim whitespace for comparison
expected := trimContents(expectedOutput)
result := trimContents(buf.String())
if expected != result {
t.Errorf("Expected output does not match.\nExpected:\n%s\nGot:\n%s", expected, result)
}
}
func TestYieldFunc(t *testing.T) {
// register the views, here we register them as part of the code of the shake of the example
// but you can use the `block.New`'s first input argument to load them from the disk.
mfs := blocks.NewMemoryFileSystem()
// define a book layout.
err := mfs.ParseTemplate("layouts/book.html", []byte(`
<html>
<head>
<title>Book Layout</title>
</head>
<body>
<h1>[layout] Body content is below...</h1>
{{- yield . }}
</body>
</html>
`), nil)
if err != nil {
t.Fatal(err)
}
// define the book index page.
err = mfs.ParseTemplate("book/index.html", []byte(`<h1>Hello, {{.Name}}!</h1>`), nil)
if err != nil {
t.Fatal(err)
}
views := blocks.New(mfs)
if err := views.Load(); err != nil {
t.Fatal(err)
}
expectedOutput := `<html>
<head>
<title>Book Layout</title>
</head>
<body>
<h1>[layout] Body content is below...</h1>
<h1>Hello, World!</h1>
</body>
</html>`
// Trim whitespace for comparison
expected := trimContents(expectedOutput)
got, err := views.TemplateString("book/index", "book", map[string]any{"Name": "World"})
if err != nil {
t.Fatal(err)
}
result := trimContents(got)
if expected != result {
t.Errorf("Expected output does not match.\nExpected:\n%s\nGot:\n%s", expected, result)
}
}
func trimContents(s string) string {
trimLineFunc := func(r rune) bool {
return r == '\r' || r == '\n' || r == ' ' || r == '\t' || r == '\v' || r == '\f'
}
lines := strings.Split(s, "\n")
for i, line := range lines {
lines[i] = strings.TrimFunc(line, trimLineFunc)
}
return strings.Join(lines, " ")
}