github.com/gogf/gf/v2@v2.7.4/frame/gins/gins_z_unit_view_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  package gins
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/gogf/gf/v2/internal/instance"
    15  	"github.com/gogf/gf/v2/os/gcfg"
    16  	"github.com/gogf/gf/v2/os/gfile"
    17  	"github.com/gogf/gf/v2/os/gtime"
    18  	"github.com/gogf/gf/v2/test/gtest"
    19  )
    20  
    21  func Test_View(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		t.AssertNE(View(), nil)
    24  		b, e := View().ParseContent(context.TODO(), `{{"我是中国人" | substr 2 -1}}`, nil)
    25  		t.Assert(e, nil)
    26  		t.Assert(b, "中国")
    27  	})
    28  	gtest.C(t, func(t *gtest.T) {
    29  		tpl := "t.tpl"
    30  		err := gfile.PutContents(tpl, `{{"我是中国人" | substr 2 -1}}`)
    31  		t.AssertNil(err)
    32  		defer gfile.Remove(tpl)
    33  
    34  		b, e := View().Parse(context.TODO(), "t.tpl", nil)
    35  		t.Assert(e, nil)
    36  		t.Assert(b, "中国")
    37  	})
    38  	gtest.C(t, func(t *gtest.T) {
    39  		path := fmt.Sprintf(`%s/%d`, gfile.Temp(), gtime.TimestampNano())
    40  		tpl := fmt.Sprintf(`%s/%s`, path, "t.tpl")
    41  		err := gfile.PutContents(tpl, `{{"我是中国人" | substr 2 -1}}`)
    42  		t.AssertNil(err)
    43  		defer gfile.Remove(tpl)
    44  		err = View().AddPath(path)
    45  		t.AssertNil(err)
    46  
    47  		b, e := View().Parse(context.TODO(), "t.tpl", nil)
    48  		t.Assert(e, nil)
    49  		t.Assert(b, "中国")
    50  	})
    51  }
    52  
    53  func Test_View_Config(t *testing.T) {
    54  	var ctx = context.TODO()
    55  	// view1 test1
    56  	gtest.C(t, func(t *gtest.T) {
    57  		dirPath := gtest.DataPath("view1")
    58  		Config().GetAdapter().(*gcfg.AdapterFile).SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
    59  		defer Config().GetAdapter().(*gcfg.AdapterFile).ClearContent()
    60  		defer instance.Clear()
    61  
    62  		view := View("test1")
    63  		t.AssertNE(view, nil)
    64  		err := view.AddPath(dirPath)
    65  		t.AssertNil(err)
    66  
    67  		str := `hello ${.name},version:${.version}`
    68  		view.Assigns(map[string]interface{}{"version": "1.9.0"})
    69  		result, err := view.ParseContent(ctx, str, nil)
    70  		t.AssertNil(err)
    71  		t.Assert(result, "hello test1,version:1.9.0")
    72  
    73  		result, err = view.ParseDefault(ctx)
    74  		t.AssertNil(err)
    75  		t.Assert(result, "test1:test1")
    76  	})
    77  	// view1 test2
    78  	gtest.C(t, func(t *gtest.T) {
    79  		dirPath := gtest.DataPath("view1")
    80  		Config().GetAdapter().(*gcfg.AdapterFile).SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
    81  		defer Config().GetAdapter().(*gcfg.AdapterFile).ClearContent()
    82  		defer instance.Clear()
    83  
    84  		view := View("test2")
    85  		t.AssertNE(view, nil)
    86  		err := view.AddPath(dirPath)
    87  		t.AssertNil(err)
    88  
    89  		str := `hello #{.name},version:#{.version}`
    90  		view.Assigns(map[string]interface{}{"version": "1.9.0"})
    91  		result, err := view.ParseContent(context.TODO(), str, nil)
    92  		t.AssertNil(err)
    93  		t.Assert(result, "hello test2,version:1.9.0")
    94  
    95  		result, err = view.ParseDefault(context.TODO())
    96  		t.AssertNil(err)
    97  		t.Assert(result, "test2:test2")
    98  	})
    99  	// view2
   100  	gtest.C(t, func(t *gtest.T) {
   101  		dirPath := gtest.DataPath("view2")
   102  		Config().GetAdapter().(*gcfg.AdapterFile).SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
   103  		defer Config().GetAdapter().(*gcfg.AdapterFile).ClearContent()
   104  		defer instance.Clear()
   105  
   106  		view := View()
   107  		t.AssertNE(view, nil)
   108  		err := view.AddPath(dirPath)
   109  		t.AssertNil(err)
   110  
   111  		str := `hello {.name},version:{.version}`
   112  		view.Assigns(map[string]interface{}{"version": "1.9.0"})
   113  		result, err := view.ParseContent(context.TODO(), str, nil)
   114  		t.AssertNil(err)
   115  		t.Assert(result, "hello test,version:1.9.0")
   116  
   117  		result, err = view.ParseDefault(context.TODO())
   118  		t.AssertNil(err)
   119  		t.Assert(result, "test:test")
   120  	})
   121  	// view2
   122  	gtest.C(t, func(t *gtest.T) {
   123  		dirPath := gtest.DataPath("view2")
   124  		Config().GetAdapter().(*gcfg.AdapterFile).SetContent(gfile.GetContents(gfile.Join(dirPath, "config.toml")))
   125  		defer Config().GetAdapter().(*gcfg.AdapterFile).ClearContent()
   126  		defer instance.Clear()
   127  
   128  		view := View("test100")
   129  		t.AssertNE(view, nil)
   130  		err := view.AddPath(dirPath)
   131  		t.AssertNil(err)
   132  
   133  		str := `hello {.name},version:{.version}`
   134  		view.Assigns(map[string]interface{}{"version": "1.9.0"})
   135  		result, err := view.ParseContent(context.TODO(), str, nil)
   136  		t.AssertNil(err)
   137  		t.Assert(result, "hello test,version:1.9.0")
   138  
   139  		result, err = view.ParseDefault(context.TODO())
   140  		t.AssertNil(err)
   141  		t.Assert(result, "test:test")
   142  	})
   143  }