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 = `{{"