2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-08-31 18:04:12 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
2019-06-11 17:24:26 +08:00
|
|
|
package gview_test
|
|
|
|
|
|
|
|
import (
|
2021-05-13 00:16:45 +08:00
|
|
|
"context"
|
2019-12-26 11:03:59 +08:00
|
|
|
"github.com/gogf/gf/encoding/ghtml"
|
2019-11-01 15:31:26 +08:00
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2019-06-11 17:24:26 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-01-05 19:55:17 +08:00
|
|
|
"strings"
|
2019-06-11 17:24:26 +08:00
|
|
|
"testing"
|
2019-11-01 15:31:26 +08:00
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/os/gview"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
2019-06-11 17:24:26 +08:00
|
|
|
)
|
|
|
|
|
2019-06-13 20:29:40 +08:00
|
|
|
func init() {
|
|
|
|
os.Setenv("GF_GVIEW_ERRORPRINT", "false")
|
|
|
|
}
|
|
|
|
|
2019-08-31 18:04:12 +08:00
|
|
|
func Test_Basic(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-11 17:24:26 +08:00
|
|
|
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" }})
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := view.ParseContent(context.TODO(), str, g.Map{"other": "that's all"})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, "hello gf,version:1.7.0;hello gf,version:1.7.0;that's all")
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
//测试api方法
|
|
|
|
str = `hello {{.name}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, g.Map{"name": "gf"})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, "hello gf")
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
//测试instance方法
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.Instance().ParseContent(context.TODO(), str, g.Map{"name": "gf"})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, "hello gf")
|
2019-06-11 17:24:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-31 18:04:12 +08:00
|
|
|
func Test_Func(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-11 17:24:26 +08:00
|
|
|
str := `{{eq 1 1}};{{eq 1 2}};{{eq "A" "B"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `true;false;false`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{ne 1 2}};{{ne 1 1}};{{ne "A" "B"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `true;false;true`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{lt 1 2}};{{lt 1 1}};{{lt 1 0}};{{lt "A" "B"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `true;false;false;true`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{le 1 2}};{{le 1 1}};{{le 1 0}};{{le "A" "B"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `true;true;false;true`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{gt 1 2}};{{gt 1 1}};{{gt 1 0}};{{gt "A" "B"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `false;false;true;false`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{ge 1 2}};{{ge 1 1}};{{ge 1 0}};{{ge "A" "B"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `false;true;true;false`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"<div>测试</div>"|text}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `测试`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"<div>测试</div>"|html}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `<div>测试</div>`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"<div>测试</div>"|htmlencode}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `<div>测试</div>`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"<div>测试</div>"|htmldecode}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `<div>测试</div>`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"https://goframe.org"|url}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `https%3A%2F%2Fgoframe.org`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"https://goframe.org"|urlencode}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `https%3A%2F%2Fgoframe.org`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"https%3A%2F%2Fgoframe.org"|urldecode}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `https://goframe.org`)
|
2019-06-11 17:24:26 +08:00
|
|
|
str = `{{"https%3NA%2F%2Fgoframe.org"|urldecode}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(gstr.Contains(result, "invalid URL escape"), true)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
2019-06-11 17:49:05 +08:00
|
|
|
str = `{{1540822968 | date "Y-m-d"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `2018-10-29`)
|
2019-06-11 17:24:26 +08:00
|
|
|
str = `{{date "Y-m-d"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"我是中国人" | substr 2 -1}};{{"我是中国人" | substr 2 2}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `中国人;中国`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"我是中国人" | strlimit 2 "..."}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `我是...`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
2019-11-07 20:56:17 +08:00
|
|
|
str = `{{"I'm中国人" | replace "I'm" "我是"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `我是中国人`)
|
2019-11-07 20:56:17 +08:00
|
|
|
|
2019-06-11 17:24:26 +08:00
|
|
|
str = `{{compare "A" "B"}};{{compare "1" "2"}};{{compare 2 1}};{{compare 1 1}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `-1;-1;1;0`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"热爱GF热爱生活" | hidestr 20 "*"}};{{"热爱GF热爱生活" | hidestr 50 "*"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `热爱GF*爱生活;热爱****生活`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"热爱GF热爱生活" | highlight "GF" "red"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `热爱<span style="color:red;">GF</span>热爱生活`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
str = `{{"gf" | toupper}};{{"GF" | tolower}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `GF;gf`)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
2020-01-05 19:55:17 +08:00
|
|
|
str = `{{concat "I" "Love" "GoFrame"}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result, `ILoveGoFrame`)
|
2020-01-05 19:55:17 +08:00
|
|
|
})
|
2021-01-04 00:05:02 +08:00
|
|
|
// eq: multiple values.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
str := `{{eq 1 2 1 3 4 5}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := gview.ParseContent(context.TODO(), str, nil)
|
2021-01-04 00:05:02 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `true`)
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
str := `{{eq 6 2 1 3 4 5}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := gview.ParseContent(context.TODO(), str, nil)
|
2021-01-04 00:05:02 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `false`)
|
|
|
|
})
|
2020-01-05 19:55:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_FuncNl2Br(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-05 19:55:17 +08:00
|
|
|
str := `{{"Go\nFrame" | nl2br}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := gview.ParseContent(context.TODO(), str, nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result, `Go<br>Frame`)
|
2019-06-11 17:24:26 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-01-05 19:55:17 +08:00
|
|
|
s := ""
|
|
|
|
for i := 0; i < 3000; i++ {
|
|
|
|
s += "Go\nFrame\n中文"
|
|
|
|
}
|
|
|
|
str := `{{.content | nl2br}}`
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := gview.ParseContent(context.TODO(), str, g.Map{
|
2020-01-05 19:55:17 +08:00
|
|
|
"content": s,
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result, strings.Replace(s, "\n", "<br>", -1))
|
2020-01-05 19:55:17 +08:00
|
|
|
})
|
2019-06-11 17:24:26 +08:00
|
|
|
}
|
|
|
|
|
2019-08-31 18:04:12 +08:00
|
|
|
func Test_FuncInclude(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-11 17:24:26 +08:00
|
|
|
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)
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := view.Parse(context.TODO(), "notfound.html")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, true)
|
|
|
|
t.Assert(result, ``)
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = view.Parse(context.TODO(), "layout.html")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, false)
|
|
|
|
t.Assert(result, `<h1>HEADER</h1>
|
2019-06-11 17:24:26 +08:00
|
|
|
<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)
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = view.Parse(context.TODO(), "notfound.html")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, true)
|
|
|
|
t.Assert(result, ``)
|
2019-06-11 17:24:26 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-31 18:04:12 +08:00
|
|
|
func Test_SetPath(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-11 17:24:26 +08:00
|
|
|
view := gview.Instance("addpath")
|
|
|
|
err := view.AddPath("tmp")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
err = view.AddPath("gview.go")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
os.Setenv("GF_GVIEW_PATH", "tmp")
|
|
|
|
view = gview.Instance("setpath")
|
|
|
|
err = view.SetPath("tmp")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
err = view.SetPath("gview.go")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
view = gview.New(gfile.Pwd())
|
|
|
|
err = view.SetPath("tmp")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
err = view.SetPath("gview.go")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertNE(err, nil)
|
2019-06-11 17:24:26 +08:00
|
|
|
|
|
|
|
os.Setenv("GF_GVIEW_PATH", "template")
|
|
|
|
gfile.Mkdir(gfile.Pwd() + gfile.Separator + "template")
|
|
|
|
view = gview.New()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-31 18:04:12 +08:00
|
|
|
func Test_ParseContent(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-11 17:24:26 +08:00
|
|
|
str := `{{.name}}`
|
|
|
|
view := gview.New()
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := view.ParseContent(context.TODO(), str, g.Map{"name": func() {}})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err != nil, true)
|
|
|
|
t.Assert(result, ``)
|
2019-06-11 17:24:26 +08:00
|
|
|
})
|
|
|
|
}
|
2019-11-01 15:31:26 +08:00
|
|
|
|
|
|
|
func Test_HotReload(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-11-01 15:31:26 +08:00
|
|
|
dirPath := gfile.Join(
|
|
|
|
gfile.TempDir(),
|
|
|
|
"testdata",
|
2020-01-20 14:14:11 +08:00
|
|
|
"template-"+gconv.String(gtime.TimestampNano()),
|
2019-11-01 15:31:26 +08:00
|
|
|
)
|
|
|
|
defer gfile.Remove(dirPath)
|
|
|
|
filePath := gfile.Join(dirPath, "test.html")
|
|
|
|
|
|
|
|
// Initialize data.
|
|
|
|
err := gfile.PutContents(filePath, "test:{{.var}}")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-11-01 15:31:26 +08:00
|
|
|
|
|
|
|
view := gview.New(dirPath)
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err := view.Parse(context.TODO(), "test.html", g.Map{
|
2019-11-01 15:31:26 +08:00
|
|
|
"var": "1",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result, `test:1`)
|
2019-11-01 15:31:26 +08:00
|
|
|
|
|
|
|
// Update data.
|
|
|
|
err = gfile.PutContents(filePath, "test2:{{.var}}")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2019-11-01 15:31:26 +08:00
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2021-05-13 00:16:45 +08:00
|
|
|
result, err = view.Parse(context.TODO(), "test.html", g.Map{
|
2019-11-01 15:31:26 +08:00
|
|
|
"var": "2",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(result, `test2:2`)
|
2019-11-01 15:31:26 +08:00
|
|
|
})
|
|
|
|
}
|
2019-12-26 11:03:59 +08:00
|
|
|
|
|
|
|
func Test_XSS(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-26 11:03:59 +08:00
|
|
|
v := gview.New()
|
|
|
|
s := "<br>"
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{.v}}", g.Map{
|
2019-12-26 11:03:59 +08:00
|
|
|
"v": s,
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, s)
|
2019-12-26 11:03:59 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-12-26 11:03:59 +08:00
|
|
|
v := gview.New()
|
|
|
|
v.SetAutoEncode(true)
|
|
|
|
s := "<br>"
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{.v}}", g.Map{
|
2019-12-26 11:03:59 +08:00
|
|
|
"v": s,
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, ghtml.Entities(s))
|
2019-12-26 11:03:59 +08:00
|
|
|
})
|
2020-02-06 15:17:10 +08:00
|
|
|
// Tag "if".
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-06 15:17:10 +08:00
|
|
|
v := gview.New()
|
|
|
|
v.SetAutoEncode(true)
|
|
|
|
s := "<br>"
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{if eq 1 1}}{{.v}}{{end}}", g.Map{
|
2020-02-06 15:17:10 +08:00
|
|
|
"v": s,
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, ghtml.Entities(s))
|
2020-02-06 15:17:10 +08:00
|
|
|
})
|
2019-12-26 11:03:59 +08:00
|
|
|
}
|
2020-11-08 17:11:04 +08:00
|
|
|
|
|
|
|
type TypeForBuildInFuncMap struct {
|
|
|
|
Name string
|
|
|
|
Score float32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TypeForBuildInFuncMap) Test() (*TypeForBuildInFuncMap, error) {
|
|
|
|
return &TypeForBuildInFuncMap{"john", 99.9}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_BuildInFuncMap(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
v := gview.New()
|
|
|
|
v.Assign("v", new(TypeForBuildInFuncMap))
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{range $k, $v := map .v.Test}} {{$k}}:{{$v}} {{end}}")
|
2020-11-08 17:11:04 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(gstr.Contains(r, "Name:john"), true)
|
|
|
|
t.Assert(gstr.Contains(r, "Score:99.9"), true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type TypeForBuildInFuncMaps struct {
|
|
|
|
Name string
|
|
|
|
Score float32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TypeForBuildInFuncMaps) Test() ([]*TypeForBuildInFuncMaps, error) {
|
|
|
|
return []*TypeForBuildInFuncMaps{
|
|
|
|
{"john", 99.9},
|
|
|
|
{"smith", 100},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_BuildInFuncMaps(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
v := gview.New()
|
|
|
|
v.Assign("v", new(TypeForBuildInFuncMaps))
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{range $k, $v := maps .v.Test}} {{$k}}:{{$v.Name}} {{$v.Score}} {{end}}")
|
2020-11-08 17:11:04 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, ` 0:john 99.9 1:smith 100 `)
|
|
|
|
})
|
|
|
|
}
|
2020-11-08 18:01:09 +08:00
|
|
|
|
|
|
|
func Test_BuildInFuncDump(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
v := gview.New()
|
|
|
|
v.Assign("v", g.Map{
|
|
|
|
"name": "john",
|
|
|
|
"score": 100,
|
|
|
|
})
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{dump .}}")
|
2020-11-08 18:01:09 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(gstr.Contains(r, `"name": "john"`), true)
|
|
|
|
t.Assert(gstr.Contains(r, `"score": 100`), true)
|
|
|
|
})
|
|
|
|
}
|
2020-11-28 22:48:01 +08:00
|
|
|
|
|
|
|
func Test_BuildInFuncJson(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
v := gview.New()
|
|
|
|
v.Assign("v", g.Map{
|
|
|
|
"name": "john",
|
|
|
|
})
|
2021-05-13 00:16:45 +08:00
|
|
|
r, err := v.ParseContent(context.TODO(), "{{json .v}}")
|
2020-11-28 22:48:01 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(r, `{"name":"john"}`)
|
|
|
|
})
|
|
|
|
}
|