gf/os/gview/gview_unit_config_test.go

68 lines
1.7 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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.
package gview_test
import (
"context"
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gview"
"github.com/gogf/gf/test/gtest"
"testing"
)
func Test_Config(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
config := gview.Config{
Paths: []string{gdebug.TestDataPath("config")},
Data: g.Map{
"name": "gf",
},
DefaultFile: "test.html",
Delimiters: []string{"${", "}"},
}
view := gview.New()
err := view.SetConfig(config)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
str := `hello ${.name},version:${.version}`
view.Assigns(g.Map{"version": "1.7.0"})
result, err := view.ParseContent(context.TODO(), str, nil)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(result, "hello gf,version:1.7.0")
result, err = view.ParseDefault(context.TODO())
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(result, "name:gf")
})
}
2019-12-02 20:27:21 +08:00
func Test_ConfigWithMap(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-12-02 20:27:21 +08:00
view := gview.New()
err := view.SetConfigWithMap(g.Map{
"Paths": []string{gdebug.TestDataPath("config")},
2019-12-02 20:27:21 +08:00
"DefaultFile": "test.html",
"Delimiters": []string{"${", "}"},
"Data": g.Map{
"name": "gf",
},
})
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
2019-12-02 20:27:21 +08:00
str := `hello ${.name},version:${.version}`
view.Assigns(g.Map{"version": "1.7.0"})
result, err := view.ParseContent(context.TODO(), str, nil)
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(result, "hello gf,version:1.7.0")
2019-12-02 20:27:21 +08:00
result, err = view.ParseDefault(context.TODO())
2020-03-19 22:56:12 +08:00
t.Assert(err, nil)
t.Assert(result, "name:gf")
2019-12-02 20:27:21 +08:00
})
}