-
Notifications
You must be signed in to change notification settings - Fork 0
/
tempest_test.go
124 lines (99 loc) · 2.94 KB
/
tempest_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
package tempest_test
import (
"strings"
"testing"
"github.com/noelukwa/tempest"
"github.com/noelukwa/tempest/test"
)
func TestLoadFs(t *testing.T) {
tempst := tempest.New()
temps, err := tempst.LoadFS(test.Files)
if err != nil {
t.Fatal(err)
}
if len(temps) != 3 {
t.Errorf("expected 2 templates, got %d", len(temps))
}
if _, ok := temps["index"]; !ok {
t.Error("expected index template to be loaded")
}
if _, ok := temps["about"]; !ok {
t.Error("expected about template to be loaded")
}
{
// Test index template
tmpl := temps["index"]
// initialise content to be type strings Builder and load content of temp into it, then compare
content := strings.Builder{}
tmpl.Execute(&content, nil)
if content.String() != "main-header index main-footer" {
t.Errorf("expected : %s got : %s", "main-header index main-footer", content.String())
}
}
{
// Test about template
tmpl := temps["about"]
// initialise content to be type strings Builder and load content of temp into it, then compare
content := strings.Builder{}
tmpl.Execute(&content, nil)
if content.String() != "main-header about main-footer" {
t.Errorf("expected : %s got : %s", "main-header about main-footer", content.String())
}
}
{
// Test nested template
tmpl := temps["admin/dash"]
// initialise content to be type strings Builder and load content of temp into it, then compare
content := strings.Builder{}
tmpl.Execute(&content, nil)
if content.String() != "main-header admin-layout admin-dash main-footer" {
t.Errorf("expected : %s got : %s", "main-header admin-layout admin-dash main-footer", content.String())
}
}
}
func TestLoadWithConfig(t *testing.T) {
tempest := tempest.WithConfig(&tempest.Config{
Layout: "base",
IncludesDir: "partials",
})
temps, err := tempest.LoadFS(test.SpecialFiles)
if err != nil {
t.Fatal(err)
}
if _, ok := temps["home"]; !ok {
t.Error("expected home template to be loaded")
}
{
// Test index template
tmpl := temps["home"]
// initialise content to be type strings Builder and load content of temp into it, then compare
content := strings.Builder{}
tmpl.Execute(&content, nil)
if content.String() != "header home-page footer" {
t.Errorf("expected : %s got : %s", "header home-page footer", content.String())
}
}
}
func TestLoadWithoutLayout(t *testing.T) {
tempest := tempest.New()
temps, err := tempest.LoadFS(test.WithoutLayout)
if err != nil {
t.Fatal(err)
}
if _, ok := temps["home"]; !ok {
t.Error("expected home template to be loaded")
}
{
// Test index template
tmpl := temps["home"]
// initialise content to be type strings Builder and load content of temp into it, then compare
content := strings.Builder{}
err := tmpl.Execute(&content, nil)
if err != nil {
t.Fatal(err)
}
if content.String() != "main-header home-page main-footer" {
t.Errorf("expected: %s got: %s", "main-header home-page main-footer", content.String())
}
}
}