github.com/gogf/gf/v2@v2.7.4/os/gview/gview_z_unit_config_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 gview_test 8 9 import ( 10 "context" 11 "testing" 12 13 "github.com/gogf/gf/v2/frame/g" 14 "github.com/gogf/gf/v2/i18n/gi18n" 15 "github.com/gogf/gf/v2/internal/command" 16 "github.com/gogf/gf/v2/os/gview" 17 "github.com/gogf/gf/v2/test/gtest" 18 ) 19 20 func Test_Config(t *testing.T) { 21 // show error print 22 command.Init("-gf.gview.errorprint=true") 23 gtest.C(t, func(t *gtest.T) { 24 config := gview.Config{ 25 Paths: []string{gtest.DataPath("config")}, 26 Data: g.Map{ 27 "name": "gf", 28 }, 29 DefaultFile: "test.html", 30 Delimiters: []string{"${", "}"}, 31 } 32 33 view := gview.New() 34 err := view.SetConfig(config) 35 t.AssertNil(err) 36 37 view.SetI18n(gi18n.New()) 38 39 str := `hello ${.name},version:${.version}` 40 view.Assigns(g.Map{"version": "1.7.0"}) 41 result, err := view.ParseContent(context.TODO(), str, nil) 42 t.AssertNil(err) 43 t.Assert(result, "hello gf,version:1.7.0") 44 45 result, err = view.ParseDefault(context.TODO()) 46 t.AssertNil(err) 47 t.Assert(result, "name:gf") 48 49 t.Assert(view.GetDefaultFile(), "test.html") 50 }) 51 // SetConfig path fail: notexist 52 gtest.C(t, func(t *gtest.T) { 53 config := gview.Config{ 54 Paths: []string{"notexist", gtest.DataPath("config/test.html")}, 55 Data: g.Map{ 56 "name": "gf", 57 }, 58 DefaultFile: "test.html", 59 Delimiters: []string{"${", "}"}, 60 } 61 62 view := gview.New() 63 err := view.SetConfig(config) 64 t.AssertNE(err, nil) 65 }) 66 // SetConfig path fail: set file path 67 gtest.C(t, func(t *gtest.T) { 68 config := gview.Config{ 69 Paths: []string{gtest.DataPath("config/test.html")}, 70 Data: g.Map{ 71 "name": "gf", 72 }, 73 DefaultFile: "test.html", 74 Delimiters: []string{"${", "}"}, 75 } 76 77 view := gview.New() 78 err := view.SetConfig(config) 79 t.AssertNE(err, nil) 80 }) 81 } 82 83 func Test_ConfigWithMap(t *testing.T) { 84 gtest.C(t, func(t *gtest.T) { 85 view := gview.New() 86 err := view.SetConfigWithMap(g.Map{ 87 "Paths": []string{gtest.DataPath("config")}, 88 "DefaultFile": "test.html", 89 "Delimiters": []string{"${", "}"}, 90 "Data": g.Map{ 91 "name": "gf", 92 }, 93 }) 94 t.AssertNil(err) 95 96 str := `hello ${.name},version:${.version}` 97 view.Assigns(g.Map{"version": "1.7.0"}) 98 result, err := view.ParseContent(context.TODO(), str, nil) 99 t.AssertNil(err) 100 t.Assert(result, "hello gf,version:1.7.0") 101 102 result, err = view.ParseDefault(context.TODO()) 103 t.AssertNil(err) 104 t.Assert(result, "name:gf") 105 }) 106 // path as paths 107 gtest.C(t, func(t *gtest.T) { 108 view := gview.New() 109 err := view.SetConfigWithMap(g.Map{ 110 "Path": gtest.DataPath("config"), 111 "DefaultFile": "test.html", 112 "Delimiters": []string{"${", "}"}, 113 "Data": g.Map{ 114 "name": "gf", 115 }, 116 }) 117 t.AssertNil(err) 118 119 str := `hello ${.name},version:${.version}` 120 view.Assigns(g.Map{"version": "1.7.0"}) 121 result, err := view.ParseContent(context.TODO(), str, nil) 122 t.AssertNil(err) 123 t.Assert(result, "hello gf,version:1.7.0") 124 125 result, err = view.ParseDefault(context.TODO()) 126 t.AssertNil(err) 127 t.Assert(result, "name:gf") 128 }) 129 // path as paths 130 gtest.C(t, func(t *gtest.T) { 131 view := gview.New() 132 err := view.SetConfigWithMap(g.Map{ 133 "Path": []string{gtest.DataPath("config")}, 134 "DefaultFile": "test.html", 135 "Delimiters": []string{"${", "}"}, 136 "Data": g.Map{ 137 "name": "gf", 138 }, 139 }) 140 t.AssertNil(err) 141 142 str := `hello ${.name},version:${.version}` 143 view.Assigns(g.Map{"version": "1.7.0"}) 144 result, err := view.ParseContent(context.TODO(), str, nil) 145 t.AssertNil(err) 146 t.Assert(result, "hello gf,version:1.7.0") 147 148 result, err = view.ParseDefault(context.TODO()) 149 t.AssertNil(err) 150 t.Assert(result, "name:gf") 151 }) 152 // map is nil 153 gtest.C(t, func(t *gtest.T) { 154 view := gview.New() 155 err := view.SetConfigWithMap(nil) 156 t.AssertNE(err, nil) 157 }) 158 }