diff --git a/g/os/gspath/gspath_unit_test.go b/g/os/gspath/gspath_unit_test.go new file mode 100644 index 000000000..9f08d6c0a --- /dev/null +++ b/g/os/gspath/gspath_unit_test.go @@ -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) + }) +} diff --git a/g/os/gview/gview_unit_test.go b/g/os/gview/gview_unit_test.go new file mode 100644 index 000000000..6c910059a --- /dev/null +++ b/g/os/gview/gview_unit_test.go @@ -0,0 +1,240 @@ +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 = `{{"
测试
"|text}}` + result, err = gview.ParseContent(str, nil) + gtest.Assert(err != nil, false) + gtest.Assert(result, `测试`) + + str = `{{"
测试
"|html}}` + result, err = gview.ParseContent(str, nil) + gtest.Assert(err != nil, false) + gtest.Assert(result, `<div>测试</div>`) + + str = `{{"
测试
"|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, `
测试
`) + + 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"}};{{"1540822968" | date "Y-m-d H:i:s"}}` + result, err = gview.ParseContent(str, nil) + gtest.Assert(err != nil, false) + gtest.Assert(result, `2018-10-29;2018-10-29 22:22:48`) + str = `{{1540822968 | date "Y-m-d"}};{{"1540822968" | date "Y-m-d H:i:s"}}` + result, err = gview.ParseContent(str, nil) + gtest.Assert(err != nil, false) + gtest.Assert(result, `2018-10-29;2018-10-29 22:22:48`) + 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, `热爱GF热爱生活`) + + 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
Frame`) + }) +} + +func TestView_FuncInclude(t *testing.T) { + gtest.Case(t, func() { + header := `

HEADER

` + main := `

hello gf

` + footer := `

FOOTER

` + 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, `

HEADER

+

hello gf

+

FOOTER

`) + 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, ``) + }) +}