github.com/zhongdalu/gf@v1.0.0/g/os/gview/gview_unit_test.go (about)

     1  package gview_test
     2  
     3  import (
     4  	"github.com/zhongdalu/gf/g"
     5  	"github.com/zhongdalu/gf/g/os/gfile"
     6  	"github.com/zhongdalu/gf/g/os/gview"
     7  	"github.com/zhongdalu/gf/g/test/gtest"
     8  	"github.com/zhongdalu/gf/g/text/gstr"
     9  	"io/ioutil"
    10  	"os"
    11  	"testing"
    12  )
    13  
    14  func init() {
    15  	os.Setenv("GF_GVIEW_ERRORPRINT", "false")
    16  }
    17  
    18  func TestView_Basic(t *testing.T) {
    19  	gtest.Case(t, func() {
    20  		str := `hello {{.name}},version:{{.version}};hello {{GetName}},version:{{GetVersion}};{{.other}}`
    21  		pwd := gfile.Pwd()
    22  		view := gview.New()
    23  		view.SetDelimiters("{{", "}}")
    24  		view.AddPath(pwd)
    25  		view.SetPath(pwd)
    26  		view.Assign("name", "gf")
    27  		view.Assigns(g.Map{"version": "1.7.0"})
    28  		view.BindFunc("GetName", func() string { return "gf" })
    29  		view.BindFuncMap(gview.FuncMap{"GetVersion": func() string { return "1.7.0" }})
    30  		result, err := view.ParseContent(str, g.Map{"other": "that's all"})
    31  		gtest.Assert(err != nil, false)
    32  		gtest.Assert(result, "hello gf,version:1.7.0;hello gf,version:1.7.0;that's all")
    33  
    34  		//测试api方法
    35  		str = `hello {{.name}}`
    36  		result, err = gview.ParseContent(str, g.Map{"name": "gf"})
    37  		gtest.Assert(err != nil, false)
    38  		gtest.Assert(result, "hello gf")
    39  
    40  		//测试instance方法
    41  		result, err = gview.Instance().ParseContent(str, g.Map{"name": "gf"})
    42  		gtest.Assert(err != nil, false)
    43  		gtest.Assert(result, "hello gf")
    44  	})
    45  }
    46  
    47  func TestView_Func(t *testing.T) {
    48  	gtest.Case(t, func() {
    49  		str := `{{eq 1 1}};{{eq 1 2}};{{eq "A" "B"}}`
    50  		result, err := gview.ParseContent(str, nil)
    51  		gtest.Assert(err != nil, false)
    52  		gtest.Assert(result, `true;false;false`)
    53  
    54  		str = `{{ne 1 2}};{{ne 1 1}};{{ne "A" "B"}}`
    55  		result, err = gview.ParseContent(str, nil)
    56  		gtest.Assert(err != nil, false)
    57  		gtest.Assert(result, `true;false;true`)
    58  
    59  		str = `{{lt 1 2}};{{lt 1 1}};{{lt 1 0}};{{lt "A" "B"}}`
    60  		result, err = gview.ParseContent(str, nil)
    61  		gtest.Assert(err != nil, false)
    62  		gtest.Assert(result, `true;false;false;true`)
    63  
    64  		str = `{{le 1 2}};{{le 1 1}};{{le 1 0}};{{le "A" "B"}}`
    65  		result, err = gview.ParseContent(str, nil)
    66  		gtest.Assert(err != nil, false)
    67  		gtest.Assert(result, `true;true;false;true`)
    68  
    69  		str = `{{gt 1 2}};{{gt 1 1}};{{gt 1 0}};{{gt "A" "B"}}`
    70  		result, err = gview.ParseContent(str, nil)
    71  		gtest.Assert(err != nil, false)
    72  		gtest.Assert(result, `false;false;true;false`)
    73  
    74  		str = `{{ge 1 2}};{{ge 1 1}};{{ge 1 0}};{{ge "A" "B"}}`
    75  		result, err = gview.ParseContent(str, nil)
    76  		gtest.Assert(err != nil, false)
    77  		gtest.Assert(result, `false;true;true;false`)
    78  
    79  		str = `{{"<div>测试</div>"|text}}`
    80  		result, err = gview.ParseContent(str, nil)
    81  		gtest.Assert(err != nil, false)
    82  		gtest.Assert(result, `测试`)
    83  
    84  		str = `{{"<div>测试</div>"|html}}`
    85  		result, err = gview.ParseContent(str, nil)
    86  		gtest.Assert(err != nil, false)
    87  		gtest.Assert(result, `&lt;div&gt;测试&lt;/div&gt;`)
    88  
    89  		str = `{{"<div>测试</div>"|htmlencode}}`
    90  		result, err = gview.ParseContent(str, nil)
    91  		gtest.Assert(err != nil, false)
    92  		gtest.Assert(result, `&lt;div&gt;测试&lt;/div&gt;`)
    93  
    94  		str = `{{"&lt;div&gt;测试&lt;/div&gt;"|htmldecode}}`
    95  		result, err = gview.ParseContent(str, nil)
    96  		gtest.Assert(err != nil, false)
    97  		gtest.Assert(result, `<div>测试</div>`)
    98  
    99  		str = `{{"https://goframe.org"|url}}`
   100  		result, err = gview.ParseContent(str, nil)
   101  		gtest.Assert(err != nil, false)
   102  		gtest.Assert(result, `https%3A%2F%2Fgoframe.org`)
   103  
   104  		str = `{{"https://goframe.org"|urlencode}}`
   105  		result, err = gview.ParseContent(str, nil)
   106  		gtest.Assert(err != nil, false)
   107  		gtest.Assert(result, `https%3A%2F%2Fgoframe.org`)
   108  
   109  		str = `{{"https%3A%2F%2Fgoframe.org"|urldecode}}`
   110  		result, err = gview.ParseContent(str, nil)
   111  		gtest.Assert(err != nil, false)
   112  		gtest.Assert(result, `https://goframe.org`)
   113  		str = `{{"https%3NA%2F%2Fgoframe.org"|urldecode}}`
   114  		result, err = gview.ParseContent(str, nil)
   115  		gtest.Assert(err != nil, false)
   116  		gtest.Assert(gstr.Contains(result, "invalid URL escape"), true)
   117  
   118  		str = `{{1540822968 | date "Y-m-d"}}`
   119  		result, err = gview.ParseContent(str, nil)
   120  		gtest.Assert(err != nil, false)
   121  		gtest.Assert(result, `2018-10-29`)
   122  		str = `{{date "Y-m-d"}}`
   123  		result, err = gview.ParseContent(str, nil)
   124  		gtest.Assert(err != nil, false)
   125  
   126  		str = `{{"我是中国人" | substr 2 -1}};{{"我是中国人" | substr 2  2}}`
   127  		result, err = gview.ParseContent(str, nil)
   128  		gtest.Assert(err != nil, false)
   129  		gtest.Assert(result, `中国人;中国`)
   130  
   131  		str = `{{"我是中国人" | strlimit 2  "..."}}`
   132  		result, err = gview.ParseContent(str, nil)
   133  		gtest.Assert(err != nil, false)
   134  		gtest.Assert(result, `我是...`)
   135  
   136  		str = `{{compare "A" "B"}};{{compare "1" "2"}};{{compare 2 1}};{{compare 1 1}}`
   137  		result, err = gview.ParseContent(str, nil)
   138  		gtest.Assert(err != nil, false)
   139  		gtest.Assert(result, `-1;-1;1;0`)
   140  
   141  		str = `{{"热爱GF热爱生活" | hidestr 20  "*"}};{{"热爱GF热爱生活" | hidestr 50  "*"}}`
   142  		result, err = gview.ParseContent(str, nil)
   143  		gtest.Assert(err != nil, false)
   144  		gtest.Assert(result, `热爱GF*爱生活;热爱****生活`)
   145  
   146  		str = `{{"热爱GF热爱生活" | highlight "GF" "red"}}`
   147  		result, err = gview.ParseContent(str, nil)
   148  		gtest.Assert(err != nil, false)
   149  		gtest.Assert(result, `热爱<span style="color:red;">GF</span>热爱生活`)
   150  
   151  		str = `{{"gf" | toupper}};{{"GF" | tolower}}`
   152  		result, err = gview.ParseContent(str, nil)
   153  		gtest.Assert(err != nil, false)
   154  		gtest.Assert(result, `GF;gf`)
   155  
   156  		str = `{{"Go\nFrame" | nl2br}}`
   157  		view := gview.New()
   158  		result, err = view.ParseContent(str)
   159  		gtest.Assert(err != nil, false)
   160  		gtest.Assert(result, `Go<br>Frame`)
   161  	})
   162  }
   163  
   164  func TestView_FuncInclude(t *testing.T) {
   165  	gtest.Case(t, func() {
   166  		header := `<h1>HEADER</h1>`
   167  		main := `<h1>hello gf</h1>`
   168  		footer := `<h1>FOOTER</h1>`
   169  		layout := `{{include "header.html" .}}
   170  {{include "main.html" .}}
   171  {{include "footer.html" .}}`
   172  		templatePath := gfile.Pwd() + gfile.Separator + "template"
   173  		gfile.Mkdir(templatePath)
   174  		defer gfile.Remove(templatePath)
   175  		//headerFile, _ := gfile.Create(templatePath + gfile.Separator + "header.html")
   176  		err := ioutil.WriteFile(templatePath+gfile.Separator+"header.html", []byte(header), 0644)
   177  		if err != nil {
   178  			t.Error(err)
   179  		}
   180  		ioutil.WriteFile(templatePath+gfile.Separator+"main.html", []byte(main), 0644)
   181  		ioutil.WriteFile(templatePath+gfile.Separator+"footer.html", []byte(footer), 0644)
   182  		ioutil.WriteFile(templatePath+gfile.Separator+"layout.html", []byte(layout), 0644)
   183  		view := gview.New(templatePath)
   184  		result, err := view.Parse("notfound.html")
   185  		gtest.Assert(err != nil, true)
   186  		gtest.Assert(result, ``)
   187  		result, err = view.Parse("layout.html")
   188  		gtest.Assert(err != nil, false)
   189  		gtest.Assert(result, `<h1>HEADER</h1>
   190  <h1>hello gf</h1>
   191  <h1>FOOTER</h1>`)
   192  		notfoundPath := templatePath + gfile.Separator + "template" + gfile.Separator + "notfound.html"
   193  		gfile.Mkdir(templatePath + gfile.Separator + "template")
   194  		gfile.Create(notfoundPath)
   195  		ioutil.WriteFile(notfoundPath, []byte("notfound"), 0644)
   196  		result, err = view.Parse("notfound.html")
   197  		gtest.Assert(err != nil, true)
   198  		gtest.Assert(result, ``)
   199  	})
   200  }
   201  
   202  func TestView_SetPath(t *testing.T) {
   203  	gtest.Case(t, func() {
   204  		view := gview.Instance("addpath")
   205  		err := view.AddPath("tmp")
   206  		gtest.AssertNE(err, nil)
   207  
   208  		err = view.AddPath("gview.go")
   209  		gtest.AssertNE(err, nil)
   210  
   211  		os.Setenv("GF_GVIEW_PATH", "tmp")
   212  		view = gview.Instance("setpath")
   213  		err = view.SetPath("tmp")
   214  		gtest.AssertNE(err, nil)
   215  
   216  		err = view.SetPath("gview.go")
   217  		gtest.AssertNE(err, nil)
   218  
   219  		view = gview.New(gfile.Pwd())
   220  		err = view.SetPath("tmp")
   221  		gtest.AssertNE(err, nil)
   222  
   223  		err = view.SetPath("gview.go")
   224  		gtest.AssertNE(err, nil)
   225  
   226  		os.Setenv("GF_GVIEW_PATH", "template")
   227  		gfile.Mkdir(gfile.Pwd() + gfile.Separator + "template")
   228  		view = gview.New()
   229  	})
   230  }
   231  
   232  func TestView_ParseContent(t *testing.T) {
   233  	gtest.Case(t, func() {
   234  		str := `{{.name}}`
   235  		view := gview.New()
   236  		result, err := view.ParseContent(str, g.Map{"name": func() {}})
   237  		gtest.Assert(err != nil, true)
   238  		gtest.Assert(result, ``)
   239  	})
   240  }