Skip to content

Commit

Permalink
feat(Template): testing fs bindata
Browse files Browse the repository at this point in the history
  • Loading branch information
lxShaDoWxl committed Nov 5, 2018
1 parent 2f00ad1 commit 771fe35
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 0 deletions.
109 changes: 109 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package beego

import (
"bytes"
"github.com/astaxie/beego/testdata"
"github.com/elazarl/go-bindata-assetfs"
"net/http"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -256,3 +259,109 @@ func TestTemplateLayout(t *testing.T) {
}
os.RemoveAll(dir)
}

type TestingFileSystem struct {
assetfs *assetfs.AssetFS
}

func (d TestingFileSystem) Open(name string) (http.File, error) {
return d.assetfs.Open(name)
}
func (d TestingFileSystem) Walk(root string, walkFn filepath.WalkFunc) error {

f, err := d.Open(root)
if err != nil {
return err
}
info, err := f.Stat()
if err != nil {
err = walkFn(root, nil, err)
} else {
err = d.walk(root, info, walkFn)
}
if err == filepath.SkipDir {
return nil
}
return err
}

// walk recursively descends path, calling walkFn.
func (d TestingFileSystem) walk(path string, info os.FileInfo, walkFn filepath.WalkFunc) error {
var err error
if !info.IsDir() {
return walkFn(path, info, nil)
}

dir, err := d.Open(path)
defer dir.Close()
dirs, err := dir.Readdir(-1)
err1 := walkFn(path, info, err)
// If err != nil, walk can't walk into this directory.
// err1 != nil means walkFn want walk to skip this directory or stop walking.
// Therefore, if one of err and err1 isn't nil, walk will return.
if err != nil || err1 != nil {
// The caller's behavior is controlled by the return value, which is decided
// by walkFn. walkFn may ignore err and return nil.
// If walkFn returns SkipDir, it will be handled by the caller.
// So walk should return whatever walkFn returns.
return err1
}

for _, fileInfo := range dirs {
filename := filepath.Join(path, fileInfo.Name())
err = d.walk(filename, fileInfo, walkFn)
if err != nil {
if !fileInfo.IsDir() || err != filepath.SkipDir {
return err
}
}
}
return nil
}

var outputBinData = `<!DOCTYPE html>
<html>
<head>
<title>beego welcome template</title>
</head>
<body>
<h1>Hello, blocks!</h1>
<h1>Hello, astaxie!</h1>
<h2>Hello</h2>
<p> This is SomeVar: val</p>
</body>
</html>
`

func TestFsBinData(t *testing.T) {
SetTemplateFSFunc(func() IFileSystem {
return TestingFileSystem{&assetfs.AssetFS{Asset: testdata.Asset, AssetDir: testdata.AssetDir, AssetInfo: testdata.AssetInfo}}
})
dir := "views"
if err := AddViewPath("views"); err != nil {
t.Fatal(err)
}
beeTemplates := beeViewPathTemplates[dir]
if len(beeTemplates) != 3 {
t.Fatalf("should be 3 but got %v", len(beeTemplates))
}
if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil {
t.Fatal(err)
}
out := bytes.NewBufferString("")
if err := beeTemplates["index.tpl"].ExecuteTemplate(out, "index.tpl", map[string]string{"Title": "Hello", "SomeVar": "val"}); err != nil {
t.Fatal(err)
}

if out.String() != outputBinData {
t.Log(out.String())
t.Fatal("Compare failed")
}
}
2 changes: 2 additions & 0 deletions testdata/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build_view:
$(GOPATH)/bin/go-bindata-assetfs -pkg testdata views/...
Loading

0 comments on commit 771fe35

Please sign in to comment.