github.com/zhongdalu/gf@v1.0.0/g/frame/gins/gins_view_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gins_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/zhongdalu/gf/g/frame/gins"
    14  	"github.com/zhongdalu/gf/g/os/gfile"
    15  	"github.com/zhongdalu/gf/g/os/gtime"
    16  	"github.com/zhongdalu/gf/g/test/gtest"
    17  )
    18  
    19  func Test_View(t *testing.T) {
    20  	gtest.Case(t, func() {
    21  		gtest.AssertNE(gins.View(), nil)
    22  		b, e := gins.View().ParseContent(`{{"我是中国人" | substr 2 -1}}`, nil)
    23  		gtest.Assert(e, nil)
    24  		gtest.Assert(string(b), "中国人")
    25  	})
    26  	gtest.Case(t, func() {
    27  		tpl := "t.tpl"
    28  		err := gfile.PutContents(tpl, `{{"我是中国人" | substr 2 -1}}`)
    29  		gtest.Assert(err, nil)
    30  		defer gfile.Remove(tpl)
    31  
    32  		b, e := gins.View().Parse("t.tpl", nil)
    33  		gtest.Assert(e, nil)
    34  		gtest.Assert(string(b), "中国人")
    35  	})
    36  	gtest.Case(t, func() {
    37  		path := fmt.Sprintf(`%s/%d`, gfile.TempDir(), gtime.Nanosecond())
    38  		tpl := fmt.Sprintf(`%s/%s`, path, "t.tpl")
    39  		err := gfile.PutContents(tpl, `{{"我是中国人" | substr 2 -1}}`)
    40  		gtest.Assert(err, nil)
    41  		defer gfile.Remove(tpl)
    42  		err = gins.View().AddPath(path)
    43  		gtest.Assert(err, nil)
    44  
    45  		b, e := gins.View().Parse("t.tpl", nil)
    46  		gtest.Assert(e, nil)
    47  		gtest.Assert(string(b), "中国人")
    48  	})
    49  }