mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
Merge pull request #170 from piaohao/master
This commit is contained in:
commit
c124f172b2
101
g/os/gspath/gspath_unit_test.go
Normal file
101
g/os/gspath/gspath_unit_test.go
Normal file
@ -0,0 +1,101 @@
|
||||
package gspath_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gspath"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSPath_Api(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
pwd := gfile.Pwd()
|
||||
root := pwd + gfile.Separator
|
||||
gfile.Create(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
defer gfile.Remove(root + "gf_tmp")
|
||||
fp, isDir := gspath.Search(root, "gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gspath.Search(root, "gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
|
||||
fp, isDir = gspath.SearchWithCache(root, "gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gspath.SearchWithCache(root, "gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSPath_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
pwd := gfile.Pwd()
|
||||
root := pwd + gfile.Separator
|
||||
gfile.Create(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
defer gfile.Remove(root + "gf_tmp")
|
||||
gsp := gspath.New(root, false)
|
||||
realPath, err := gsp.Add(root + "gf_tmp")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(realPath, root+"gf_tmp")
|
||||
realPath, err = gsp.Add("gf_tmp1")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
realPath, err = gsp.Add(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
gsp.Remove("gf_tmp1")
|
||||
gtest.Assert(gsp.Size(), 2)
|
||||
gtest.Assert(len(gsp.Paths()), 2)
|
||||
gtest.Assert(len(gsp.AllPaths()), 0)
|
||||
realPath, err = gsp.Set(root + "gf_tmp1")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
realPath, err = gsp.Set(root + "gf_tmp" + gfile.Separator + "gf.txt")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
gsp.Set(root)
|
||||
fp, isDir := gsp.Search("gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gsp.Search("gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
fp, isDir = gsp.Search("/", "gf.txt")
|
||||
gtest.Assert(fp, root+gfile.Separator)
|
||||
gtest.Assert(isDir, true)
|
||||
|
||||
gsp = gspath.New(root, true)
|
||||
realPath, err = gsp.Add(root + "gf_tmp")
|
||||
gtest.Assert(err, nil)
|
||||
gtest.Assert(realPath, root+"gf_tmp")
|
||||
|
||||
gfile.Mkdir(root + "gf_tmp1")
|
||||
gfile.Rename(root+"gf_tmp1", root+"gf_tmp2")
|
||||
gfile.Rename(root+"gf_tmp2", root+"gf_tmp1")
|
||||
defer gfile.Remove(root + "gf_tmp1")
|
||||
realPath, err = gsp.Add("gf_tmp1")
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(realPath, root+"gf_tmp1")
|
||||
realPath, err = gsp.Add("gf_tmp3")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(realPath, "")
|
||||
gsp.Remove(root + "gf_tmp")
|
||||
gsp.Remove(root + "gf_tmp1")
|
||||
gsp.Remove(root + "gf_tmp3")
|
||||
gtest.Assert(gsp.Size(), 3)
|
||||
gtest.Assert(len(gsp.Paths()), 3)
|
||||
gsp.AllPaths()
|
||||
gsp.Set(root)
|
||||
fp, isDir = gsp.Search("gf_tmp")
|
||||
gtest.Assert(fp, root+"gf_tmp")
|
||||
gtest.Assert(isDir, true)
|
||||
fp, isDir = gsp.Search("gf_tmp", "gf.txt")
|
||||
gtest.Assert(fp, root+"gf_tmp"+gfile.Separator+"gf.txt")
|
||||
gtest.Assert(isDir, false)
|
||||
fp, isDir = gsp.Search("/", "gf.txt")
|
||||
gtest.Assert(fp, pwd)
|
||||
gtest.Assert(isDir, true)
|
||||
})
|
||||
}
|
236
g/os/gview/gview_unit_test.go
Normal file
236
g/os/gview/gview_unit_test.go
Normal file
@ -0,0 +1,236 @@
|
||||
package gview_test
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/g"
|
||||
"github.com/gogf/gf/g/os/gfile"
|
||||
"github.com/gogf/gf/g/os/gview"
|
||||
"github.com/gogf/gf/g/test/gtest"
|
||||
"github.com/gogf/gf/g/text/gstr"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestView_Basic(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
str := `hello {{.name}},version:{{.version}};hello {{GetName}},version:{{GetVersion}};{{.other}}`
|
||||
pwd := gfile.Pwd()
|
||||
view := gview.New()
|
||||
view.SetDelimiters("{{", "}}")
|
||||
view.AddPath(pwd)
|
||||
view.SetPath(pwd)
|
||||
view.Assign("name", "gf")
|
||||
view.Assigns(g.Map{"version": "1.7.0"})
|
||||
view.BindFunc("GetName", func() string { return "gf" })
|
||||
view.BindFuncMap(gview.FuncMap{"GetVersion": func() string { return "1.7.0" }})
|
||||
result, err := view.ParseContent(str, g.Map{"other": "that's all"})
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, "hello gf,version:1.7.0;hello gf,version:1.7.0;that's all")
|
||||
|
||||
//测试api方法
|
||||
str = `hello {{.name}}`
|
||||
result, err = gview.ParseContent(str, g.Map{"name": "gf"})
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, "hello gf")
|
||||
|
||||
//测试instance方法
|
||||
result, err = gview.Instance().ParseContent(str, g.Map{"name": "gf"})
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, "hello gf")
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_Func(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
str := `{{eq 1 1}};{{eq 1 2}};{{eq "A" "B"}}`
|
||||
result, err := gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;false;false`)
|
||||
|
||||
str = `{{ne 1 2}};{{ne 1 1}};{{ne "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;false;true`)
|
||||
|
||||
str = `{{lt 1 2}};{{lt 1 1}};{{lt 1 0}};{{lt "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;false;false;true`)
|
||||
|
||||
str = `{{le 1 2}};{{le 1 1}};{{le 1 0}};{{le "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `true;true;false;true`)
|
||||
|
||||
str = `{{gt 1 2}};{{gt 1 1}};{{gt 1 0}};{{gt "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `false;false;true;false`)
|
||||
|
||||
str = `{{ge 1 2}};{{ge 1 1}};{{ge 1 0}};{{ge "A" "B"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `false;true;true;false`)
|
||||
|
||||
str = `{{"<div>测试</div>"|text}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `测试`)
|
||||
|
||||
str = `{{"<div>测试</div>"|html}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<div>测试</div>`)
|
||||
|
||||
str = `{{"<div>测试</div>"|htmlencode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<div>测试</div>`)
|
||||
|
||||
str = `{{"<div>测试</div>"|htmldecode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<div>测试</div>`)
|
||||
|
||||
str = `{{"https://goframe.org"|url}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `https%3A%2F%2Fgoframe.org`)
|
||||
|
||||
str = `{{"https://goframe.org"|urlencode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `https%3A%2F%2Fgoframe.org`)
|
||||
|
||||
str = `{{"https%3A%2F%2Fgoframe.org"|urldecode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `https://goframe.org`)
|
||||
str = `{{"https%3NA%2F%2Fgoframe.org"|urldecode}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(gstr.Contains(result, "invalid URL escape"), true)
|
||||
|
||||
str = `{{1540822968 | date "Y-m-d"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `2018-10-29`)
|
||||
str = `{{date "Y-m-d"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
|
||||
str = `{{"我是中国人" | substr 2 -1}};{{"我是中国人" | substr 2 2}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `中国人;中国`)
|
||||
|
||||
str = `{{"我是中国人" | strlimit 2 "..."}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `我是...`)
|
||||
|
||||
str = `{{compare "A" "B"}};{{compare "1" "2"}};{{compare 2 1}};{{compare 1 1}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `-1;-1;1;0`)
|
||||
|
||||
str = `{{"热爱GF热爱生活" | hidestr 20 "*"}};{{"热爱GF热爱生活" | hidestr 50 "*"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `热爱GF*爱生活;热爱****生活`)
|
||||
|
||||
str = `{{"热爱GF热爱生活" | highlight "GF" "red"}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `热爱<span style="color:red;">GF</span>热爱生活`)
|
||||
|
||||
str = `{{"gf" | toupper}};{{"GF" | tolower}}`
|
||||
result, err = gview.ParseContent(str, nil)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `GF;gf`)
|
||||
|
||||
str = `{{"Go\nFrame" | nl2br}}`
|
||||
view := gview.New()
|
||||
result, err = view.ParseContent(str)
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `Go<br>Frame`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_FuncInclude(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
header := `<h1>HEADER</h1>`
|
||||
main := `<h1>hello gf</h1>`
|
||||
footer := `<h1>FOOTER</h1>`
|
||||
layout := `{{include "header.html" .}}
|
||||
{{include "main.html" .}}
|
||||
{{include "footer.html" .}}`
|
||||
templatePath := gfile.Pwd() + gfile.Separator + "template"
|
||||
gfile.Mkdir(templatePath)
|
||||
defer gfile.Remove(templatePath)
|
||||
//headerFile, _ := gfile.Create(templatePath + gfile.Separator + "header.html")
|
||||
err := ioutil.WriteFile(templatePath+gfile.Separator+"header.html", []byte(header), 0644)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
ioutil.WriteFile(templatePath+gfile.Separator+"main.html", []byte(main), 0644)
|
||||
ioutil.WriteFile(templatePath+gfile.Separator+"footer.html", []byte(footer), 0644)
|
||||
ioutil.WriteFile(templatePath+gfile.Separator+"layout.html", []byte(layout), 0644)
|
||||
view := gview.New(templatePath)
|
||||
result, err := view.Parse("notfound.html")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(result, ``)
|
||||
result, err = view.Parse("layout.html")
|
||||
gtest.Assert(err != nil, false)
|
||||
gtest.Assert(result, `<h1>HEADER</h1>
|
||||
<h1>hello gf</h1>
|
||||
<h1>FOOTER</h1>`)
|
||||
notfoundPath := templatePath + gfile.Separator + "template" + gfile.Separator + "notfound.html"
|
||||
gfile.Mkdir(templatePath + gfile.Separator + "template")
|
||||
gfile.Create(notfoundPath)
|
||||
ioutil.WriteFile(notfoundPath, []byte("notfound"), 0644)
|
||||
result, err = view.Parse("notfound.html")
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(result, ``)
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_SetPath(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
view := gview.Instance("addpath")
|
||||
err := view.AddPath("tmp")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
err = view.AddPath("gview.go")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
os.Setenv("GF_GVIEW_PATH", "tmp")
|
||||
view = gview.Instance("setpath")
|
||||
err = view.SetPath("tmp")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
err = view.SetPath("gview.go")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
view = gview.New(gfile.Pwd())
|
||||
err = view.SetPath("tmp")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
err = view.SetPath("gview.go")
|
||||
gtest.Assert(err == nil, false)
|
||||
|
||||
os.Setenv("GF_GVIEW_PATH", "template")
|
||||
gfile.Mkdir(gfile.Pwd() + gfile.Separator + "template")
|
||||
view = gview.New()
|
||||
})
|
||||
}
|
||||
|
||||
func TestView_ParseContent(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
str := `{{.name}}`
|
||||
view := gview.New()
|
||||
result, err := view.ParseContent(str, g.Map{"name": func() {}})
|
||||
gtest.Assert(err != nil, true)
|
||||
gtest.Assert(result, ``)
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user