2013-08-01 04:10:56 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-09-13 05:57:40 +00:00
|
|
|
var header string = `{{define "header"}}
|
|
|
|
<h1>Hello, astaxie!</h1>
|
|
|
|
{{end}}`
|
|
|
|
|
|
|
|
var index string = `<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>beego welcome template</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
{{template "block"}}
|
|
|
|
{{template "header"}}
|
|
|
|
{{template "blocks/block.tpl"}}
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
|
|
|
var block string = `{{define "block"}}
|
|
|
|
<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)
|
|
|
|
}
|
2013-09-12 09:32:11 +00:00
|
|
|
if len(BeeTemplates) != 3 {
|
|
|
|
t.Fatalf("should be 3 but got %v", len(BeeTemplates))
|
2013-08-01 04:10:56 +00:00
|
|
|
}
|
2013-09-13 05:57:40 +00:00
|
|
|
if err := BeeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil {
|
|
|
|
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
|
|
|
}
|