1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 04:30:55 +00:00
Beego/template_test.go

137 lines
2.9 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2013-08-01 04:10:56 +00:00
package beego
import (
"os"
"path/filepath"
"testing"
)
2015-09-08 15:29:58 +00:00
var header = `{{define "header"}}
2013-09-13 05:57:40 +00:00
<h1>Hello, astaxie!</h1>
{{end}}`
2015-09-08 15:29:58 +00:00
var index = `<!DOCTYPE html>
2013-09-13 05:57:40 +00:00
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
{{template "block"}}
{{template "header"}}
{{template "blocks/block.tpl"}}
</body>
</html>
`
2015-09-08 15:29:58 +00:00
var block = `{{define "block"}}
2013-09-13 05:57:40 +00:00
<h1>Hello, blocks!</h1>
{{end}}`
func TestTemplate(t *testing.T) {
2013-08-01 04:10:56 +00:00
dir := "_beeTmp"
files := []string{
2013-09-13 05:57:40 +00:00
"header.tpl",
"index.tpl",
"blocks/block.tpl",
2013-08-01 04:10:56 +00:00
}
if err := os.MkdirAll(dir, 0777); err != nil {
t.Fatal(err)
}
2013-09-13 05:57:40 +00:00
for k, name := range files {
os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
2013-09-12 09:32:11 +00:00
if f, err := os.Create(filepath.Join(dir, name)); err != nil {
2013-08-01 04:10:56 +00:00
t.Fatal(err)
2013-09-12 09:32:11 +00:00
} else {
2013-09-13 05:57:40 +00:00
if k == 0 {
f.WriteString(header)
} else if k == 1 {
f.WriteString(index)
} else if k == 2 {
f.WriteString(block)
}
2013-09-12 09:32:11 +00:00
f.Close()
2013-08-01 04:10:56 +00:00
}
}
if err := BuildTemplate(dir); err != nil {
t.Fatal(err)
}
2016-03-04 04:00:43 +00:00
if len(beeTemplates) != 3 {
t.Fatalf("should be 3 but got %v", len(beeTemplates))
2013-08-01 04:10:56 +00:00
}
2016-03-04 04:00:43 +00:00
if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil {
2013-09-13 05:57:40 +00:00
t.Fatal(err)
}
2013-09-12 09:32:11 +00:00
for _, name := range files {
2013-09-13 05:57:40 +00:00
os.RemoveAll(filepath.Join(dir, name))
2013-08-01 04:10:56 +00:00
}
2013-09-13 05:57:40 +00:00
os.RemoveAll(dir)
2013-08-01 04:10:56 +00:00
}
2013-10-30 15:02:53 +00:00
2015-09-08 15:29:58 +00:00
var menu = `<div class="menu">
2013-10-30 15:02:53 +00:00
<ul>
<li>menu1</li>
<li>menu2</li>
<li>menu3</li>
</ul>
</div>
`
2015-09-08 15:29:58 +00:00
var user = `<!DOCTYPE html>
2013-10-30 15:02:53 +00:00
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
{{template "../public/menu.tpl"}}
</body>
</html>
`
func TestRelativeTemplate(t *testing.T) {
dir := "_beeTmp"
files := []string{
"easyui/public/menu.tpl",
"easyui/rbac/user.tpl",
}
if err := os.MkdirAll(dir, 0777); err != nil {
t.Fatal(err)
}
for k, name := range files {
os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
if f, err := os.Create(filepath.Join(dir, name)); err != nil {
t.Fatal(err)
} else {
if k == 0 {
f.WriteString(menu)
} else if k == 1 {
f.WriteString(user)
}
f.Close()
}
}
if err := BuildTemplate(dir, files[1]); err != nil {
2013-10-30 15:02:53 +00:00
t.Fatal(err)
}
2016-03-04 04:00:43 +00:00
if err := beeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil {
2013-10-30 15:02:53 +00:00
t.Fatal(err)
}
for _, name := range files {
os.RemoveAll(filepath.Join(dir, name))
}
os.RemoveAll(dir)
}