mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:50:55 +00:00
update template's testcase
This commit is contained in:
parent
ffb347a2bb
commit
de2dd6e070
12
template.go
12
template.go
@ -65,7 +65,7 @@ func (self *templatefile) visit(paths string, f os.FileInfo, err error) error {
|
|||||||
a = a[len([]byte(self.root)):]
|
a = a[len([]byte(self.root)):]
|
||||||
file := strings.TrimLeft(replace.Replace(string(a)), "/")
|
file := strings.TrimLeft(replace.Replace(string(a)), "/")
|
||||||
subdir := filepath.Dir(file)
|
subdir := filepath.Dir(file)
|
||||||
t, err := getTemplate(file)
|
t, err := getTemplate(self.root, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("parse template err:", file, err)
|
Trace("parse template err:", file, err)
|
||||||
} else {
|
} else {
|
||||||
@ -122,8 +122,8 @@ func BuildTemplate(dir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTplDeep(file string, t *template.Template) (*template.Template, error) {
|
func getTplDeep(root, file string, t *template.Template) (*template.Template, error) {
|
||||||
fileabspath := filepath.Join(ViewsPath, file)
|
fileabspath := filepath.Join(root, file)
|
||||||
data, err := ioutil.ReadFile(fileabspath)
|
data, err := ioutil.ReadFile(fileabspath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -143,7 +143,7 @@ func getTplDeep(file string, t *template.Template) (*template.Template, error) {
|
|||||||
if !HasTemplateEXt(m[1]) {
|
if !HasTemplateEXt(m[1]) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
t, err = getTplDeep(m[1], t)
|
t, err = getTplDeep(root, m[1], t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -152,9 +152,9 @@ func getTplDeep(file string, t *template.Template) (*template.Template, error) {
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTemplate(file string) (t *template.Template, err error) {
|
func getTemplate(root, file string) (t *template.Template, err error) {
|
||||||
t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap)
|
t = template.New(file).Delims(TemplateLeft, TemplateRight).Funcs(beegoTplFuncMap)
|
||||||
t, err = getTplDeep(file, t)
|
t, err = getTplDeep(root, file, t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -18,32 +18,28 @@ func TestBuildTemplate(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, name := range files {
|
for _, name := range files {
|
||||||
if _, err := os.Create(filepath.Join(dir, name)); err != nil {
|
if f, err := os.Create(filepath.Join(dir, name)); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
} else {
|
||||||
|
f.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := BuildTemplate(dir); err != nil {
|
if err := BuildTemplate(dir); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(BeeTemplates) != 1 {
|
if len(BeeTemplates) != 2 {
|
||||||
t.Fatalf("should be 1 but got %v", len(BeeTemplates))
|
t.Fatalf("should be 2 but got %v", len(BeeTemplates))
|
||||||
}
|
|
||||||
for _, v := range BeeTemplates {
|
|
||||||
if len(v.Templates()) != 3 {
|
|
||||||
t.Errorf("should be 3 but got %v", len(v.Templates()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AddTemplateExt("mystyle")
|
AddTemplateExt("mystyle")
|
||||||
if err := BuildTemplate(dir); err != nil {
|
if err := BuildTemplate(dir); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if len(BeeTemplates) != 1 {
|
if len(BeeTemplates) != 3 {
|
||||||
t.Fatalf("should be 1 but got %v", len(BeeTemplates))
|
t.Fatalf("should be 3 but got %v", len(BeeTemplates))
|
||||||
}
|
}
|
||||||
for _, v := range BeeTemplates {
|
for _, name := range files {
|
||||||
if len(v.Templates()) != 4 {
|
os.Remove(filepath.Join(dir, name))
|
||||||
t.Errorf("should be 4 but got %v", len(v.Templates()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
os.Remove(dir)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user