-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_engine_test.go
198 lines (160 loc) · 5.38 KB
/
go_engine_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/view source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package view
import (
"bytes"
"errors"
"html/template"
"io/ioutil"
"strings"
"testing"
"aahframework.org/config.v0"
"aahframework.org/log.v0"
"aahframework.org/test.v0/assert"
)
func TestViewAppPages(t *testing.T) {
// _ = log.SetLevel("trace")
log.SetWriter(ioutil.Discard)
cfg, _ := config.ParseString(`view { }`)
ge := loadGoViewEngine(t, cfg, "views", false)
data := map[string]interface{}{
"GreetName": "aah framework",
"PageName": "home page",
}
tmpl, err := ge.Get("master.html", "pages/app", "index.html")
assert.Nil(t, err)
assert.NotNil(t, tmpl)
var buf bytes.Buffer
err = tmpl.ExecuteTemplate(&buf, "master.html", data)
assert.FailNowOnError(t, err, "")
htmlStr := buf.String()
t.Logf("HTML String: %s", htmlStr)
assert.True(t, strings.Contains(htmlStr, "<title>aah framework - Home</title>"))
assert.True(t, strings.Contains(htmlStr, "aah framework home page"))
tmpl, err = ge.Get("no_master", "pages/app", "index.html")
assert.NotNil(t, err)
assert.Nil(t, tmpl)
}
func TestViewUserPages(t *testing.T) {
// _ = log.SetLevel("trace")
log.SetWriter(ioutil.Discard)
cfg, _ := config.ParseString(`view {
delimiters = "{{.}}"
}`)
ge := loadGoViewEngine(t, cfg, "views", true)
data := map[string]interface{}{
"GreetName": "aah framework",
"PageName": "user home page",
}
ge.CaseSensitive = true
tmpl, err := ge.Get("master.html", "pages/user", "index.html")
assert.Nil(t, err)
assert.NotNil(t, tmpl)
var buf bytes.Buffer
err = tmpl.ExecuteTemplate(&buf, "master.html", data)
assert.FailNowOnError(t, err, "")
htmlStr := buf.String()
t.Logf("HTML String: %s", htmlStr)
assert.True(t, strings.Contains(htmlStr, "<title>aah framework - User Home</title>"))
assert.True(t, strings.Contains(htmlStr, "aah framework user home page"))
assert.True(t, strings.Contains(htmlStr, `cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js`))
tmpl, err = ge.Get("master.html", "pages/user", "not_exists.html")
assert.NotNil(t, err)
assert.Nil(t, tmpl)
}
func TestViewUserPagesNoLayout(t *testing.T) {
// _ = log.SetLevel("trace")
log.SetWriter(ioutil.Discard)
cfg, _ := config.ParseString(`view {
delimiters = "{{.}}"
default_layout = false
}`)
ge := loadGoViewEngine(t, cfg, "views", false)
data := map[string]interface{}{
"GreetName": "aah framework",
"PageName": "user home page",
}
tmpl, err := ge.Get("", "pages/user", "index-nolayout.html")
assert.Nil(t, err)
assert.NotNil(t, tmpl)
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
assert.FailNowOnError(t, err, "")
htmlStr := buf.String()
t.Logf("HTML String: %s", htmlStr)
assert.True(t, strings.Contains(htmlStr, "aah framework user home page - no layout"))
}
func TestViewBaseDirNotExists(t *testing.T) {
viewsDir := join("testdata", "views1")
ge := &GoViewEngine{}
cfg, _ := config.ParseString(`view { }`)
err := ge.Init(newVFS(), cfg, viewsDir)
assert.NotNil(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "goviewengine: views base dir is not exists:"))
}
func TestViewDelimitersError(t *testing.T) {
viewsDir := join("testdata", "views")
ge := &GoViewEngine{}
cfg, _ := config.ParseString(`view {
delimiters = "{{."
}`)
err := ge.Init(newVFS(), cfg, viewsDir)
assert.NotNil(t, err)
assert.Equal(t, "goviewengine: config 'view.delimiters' value is invalid", err.Error())
}
func TestViewErrors(t *testing.T) {
// _ = log.SetLevel("trace")
log.SetWriter(ioutil.Discard)
cfg, _ := config.ParseString(`view {
default_layout = false
}`)
fs := newVFS()
// No layout directiry
viewsDir := join("testdata", "views-no-layouts-dir")
ge := &GoViewEngine{}
err := ge.Init(fs, cfg, viewsDir)
assert.NotNil(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "goviewengine: layouts base dir is not exists:"))
// No Common directory
viewsDir = join("testdata", "views-no-common-dir")
ge = &GoViewEngine{}
err = ge.Init(fs, cfg, viewsDir)
assert.NotNil(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "goviewengine: common base dir is not exists:"))
// No Pages directory
viewsDir = join("testdata", "views-no-pages-dir")
ge = &GoViewEngine{}
err = ge.Init(fs, cfg, viewsDir)
assert.NotNil(t, err)
assert.True(t, strings.HasPrefix(err.Error(), "goviewengine: pages base dir is not exists:"))
// handle errors methods
err = ge.ParseErrors([]error{errors.New("error 1"), errors.New("error 2")})
assert.NotNil(t, err)
assert.Equal(t, "goviewengine: error processing templates, please check the log", err.Error())
}
func loadGoViewEngine(t *testing.T, cfg *config.Config, dir string, hotreload bool) *GoViewEngine {
// dummy func for test
AddTemplateFunc(template.FuncMap{
"anticsrftoken": func(arg interface{}) string {
return ""
},
"rurl": func(args map[string]interface{}, key string) string {
return "//localhost:8080/login"
},
"qparam": func(args map[string]interface{}, key string) string {
return "/index"
},
})
viewsDir := join("testdata", dir)
ge := &GoViewEngine{}
err := ge.Init(newVFS(), cfg, viewsDir)
assert.FailNowOnError(t, err, "")
ge.hotReload = hotreload
assert.Equal(t, viewsDir, ge.BaseDir)
assert.NotNil(t, ge.AppConfig)
assert.NotNil(t, ge.Templates)
assert.NotNil(t, (&EngineBase{}).Init(nil, nil, "", "", ""))
log.SetWriter(ioutil.Discard)
return ge
}