github.com/astaxie/beego@v1.12.3/template_test.go (about) 1 // Copyright 2014 beego Author. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package beego 16 17 import ( 18 "bytes" 19 "net/http" 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/astaxie/beego/testdata" 25 "github.com/elazarl/go-bindata-assetfs" 26 ) 27 28 var header = `{{define "header"}} 29 <h1>Hello, astaxie!</h1> 30 {{end}}` 31 32 var index = `<!DOCTYPE html> 33 <html> 34 <head> 35 <title>beego welcome template</title> 36 </head> 37 <body> 38 {{template "block"}} 39 {{template "header"}} 40 {{template "blocks/block.tpl"}} 41 </body> 42 </html> 43 ` 44 45 var block = `{{define "block"}} 46 <h1>Hello, blocks!</h1> 47 {{end}}` 48 49 func tmpDir(s string) string { 50 return filepath.Join(os.TempDir(), s) 51 } 52 53 func TestTemplate(t *testing.T) { 54 dir := tmpDir("TestTemplate") 55 files := []string{ 56 "header.tpl", 57 "index.tpl", 58 "blocks/block.tpl", 59 } 60 if err := os.MkdirAll(dir, 0777); err != nil { 61 t.Fatal(err) 62 } 63 for k, name := range files { 64 os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777) 65 if f, err := os.Create(filepath.Join(dir, name)); err != nil { 66 t.Fatal(err) 67 } else { 68 if k == 0 { 69 f.WriteString(header) 70 } else if k == 1 { 71 f.WriteString(index) 72 } else if k == 2 { 73 f.WriteString(block) 74 } 75 76 f.Close() 77 } 78 } 79 if err := AddViewPath(dir); err != nil { 80 t.Fatal(err) 81 } 82 beeTemplates := beeViewPathTemplates[dir] 83 if len(beeTemplates) != 3 { 84 t.Fatalf("should be 3 but got %v", len(beeTemplates)) 85 } 86 if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil { 87 t.Fatal(err) 88 } 89 for _, name := range files { 90 os.RemoveAll(filepath.Join(dir, name)) 91 } 92 os.RemoveAll(dir) 93 } 94 95 var menu = `<div class="menu"> 96 <ul> 97 <li>menu1</li> 98 <li>menu2</li> 99 <li>menu3</li> 100 </ul> 101 </div> 102 ` 103 var user = `<!DOCTYPE html> 104 <html> 105 <head> 106 <title>beego welcome template</title> 107 </head> 108 <body> 109 {{template "../public/menu.tpl"}} 110 </body> 111 </html> 112 ` 113 114 func TestRelativeTemplate(t *testing.T) { 115 dir := tmpDir("TestRelativeTemplate") 116 117 //Just add dir to known viewPaths 118 if err := AddViewPath(dir); err != nil { 119 t.Fatal(err) 120 } 121 122 files := []string{ 123 "easyui/public/menu.tpl", 124 "easyui/rbac/user.tpl", 125 } 126 if err := os.MkdirAll(dir, 0777); err != nil { 127 t.Fatal(err) 128 } 129 for k, name := range files { 130 os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777) 131 if f, err := os.Create(filepath.Join(dir, name)); err != nil { 132 t.Fatal(err) 133 } else { 134 if k == 0 { 135 f.WriteString(menu) 136 } else if k == 1 { 137 f.WriteString(user) 138 } 139 f.Close() 140 } 141 } 142 if err := BuildTemplate(dir, files[1]); err != nil { 143 t.Fatal(err) 144 } 145 beeTemplates := beeViewPathTemplates[dir] 146 if err := beeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil { 147 t.Fatal(err) 148 } 149 for _, name := range files { 150 os.RemoveAll(filepath.Join(dir, name)) 151 } 152 os.RemoveAll(dir) 153 } 154 155 var add = `{{ template "layout_blog.tpl" . }} 156 {{ define "css" }} 157 <link rel="stylesheet" href="/static/css/current.css"> 158 {{ end}} 159 160 161 {{ define "content" }} 162 <h2>{{ .Title }}</h2> 163 <p> This is SomeVar: {{ .SomeVar }}</p> 164 {{ end }} 165 166 {{ define "js" }} 167 <script src="/static/js/current.js"></script> 168 {{ end}}` 169 170 var layoutBlog = `<!DOCTYPE html> 171 <html> 172 <head> 173 <title>Lin Li</title> 174 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 175 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 176 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> 177 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css"> 178 {{ block "css" . }}{{ end }} 179 </head> 180 <body> 181 182 <div class="container"> 183 {{ block "content" . }}{{ end }} 184 </div> 185 <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 186 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> 187 {{ block "js" . }}{{ end }} 188 </body> 189 </html>` 190 191 var output = `<!DOCTYPE html> 192 <html> 193 <head> 194 <title>Lin Li</title> 195 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 196 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 197 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> 198 <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css"> 199 200 <link rel="stylesheet" href="/static/css/current.css"> 201 202 </head> 203 <body> 204 205 <div class="container"> 206 207 <h2>Hello</h2> 208 <p> This is SomeVar: val</p> 209 210 </div> 211 <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> 212 <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> 213 214 <script src="/static/js/current.js"></script> 215 216 </body> 217 </html> 218 219 220 221 222 223 ` 224 225 func TestTemplateLayout(t *testing.T) { 226 dir := tmpDir("TestTemplateLayout") 227 files := []string{ 228 "add.tpl", 229 "layout_blog.tpl", 230 } 231 if err := os.MkdirAll(dir, 0777); err != nil { 232 t.Fatal(err) 233 } 234 for k, name := range files { 235 os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777) 236 if f, err := os.Create(filepath.Join(dir, name)); err != nil { 237 t.Fatal(err) 238 } else { 239 if k == 0 { 240 f.WriteString(add) 241 } else if k == 1 { 242 f.WriteString(layoutBlog) 243 } 244 f.Close() 245 } 246 } 247 if err := AddViewPath(dir); err != nil { 248 t.Fatal(err) 249 } 250 beeTemplates := beeViewPathTemplates[dir] 251 if len(beeTemplates) != 2 { 252 t.Fatalf("should be 2 but got %v", len(beeTemplates)) 253 } 254 out := bytes.NewBufferString("") 255 if err := beeTemplates["add.tpl"].ExecuteTemplate(out, "add.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil { 256 t.Fatal(err) 257 } 258 if out.String() != output { 259 t.Log(out.String()) 260 t.Fatal("Compare failed") 261 } 262 for _, name := range files { 263 os.RemoveAll(filepath.Join(dir, name)) 264 } 265 os.RemoveAll(dir) 266 } 267 268 type TestingFileSystem struct { 269 assetfs *assetfs.AssetFS 270 } 271 272 func (d TestingFileSystem) Open(name string) (http.File, error) { 273 return d.assetfs.Open(name) 274 } 275 276 var outputBinData = `<!DOCTYPE html> 277 <html> 278 <head> 279 <title>beego welcome template</title> 280 </head> 281 <body> 282 283 284 <h1>Hello, blocks!</h1> 285 286 287 <h1>Hello, astaxie!</h1> 288 289 290 291 <h2>Hello</h2> 292 <p> This is SomeVar: val</p> 293 </body> 294 </html> 295 ` 296 297 func TestFsBinData(t *testing.T) { 298 SetTemplateFSFunc(func() http.FileSystem { 299 return TestingFileSystem{&assetfs.AssetFS{Asset: testdata.Asset, AssetDir: testdata.AssetDir, AssetInfo: testdata.AssetInfo}} 300 }) 301 dir := "views" 302 if err := AddViewPath("views"); err != nil { 303 t.Fatal(err) 304 } 305 beeTemplates := beeViewPathTemplates[dir] 306 if len(beeTemplates) != 3 { 307 t.Fatalf("should be 3 but got %v", len(beeTemplates)) 308 } 309 if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil { 310 t.Fatal(err) 311 } 312 out := bytes.NewBufferString("") 313 if err := beeTemplates["index.tpl"].ExecuteTemplate(out, "index.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil { 314 t.Fatal(err) 315 } 316 317 if out.String() != outputBinData { 318 t.Log(out.String()) 319 t.Fatal("Compare failed") 320 } 321 }