2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-11-28 11:18:09 +08:00
|
|
|
//
|
|
|
|
// 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 (
|
2021-05-13 00:16:45 +08:00
|
|
|
"context"
|
2019-11-28 11:18:09 +08:00
|
|
|
"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) {
|
2019-11-28 11:18:09 +08:00
|
|
|
config := gview.Config{
|
2020-03-21 19:41:05 +08:00
|
|
|
Paths: []string{gdebug.TestDataPath("config")},
|
2019-11-28 11:18:09 +08:00
|
|
|
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)
|
2019-11-28 11:18:09 +08:00
|
|
|
|
|
|
|
str := `hello ${.name},version:${.version}`
|
|
|
|
view.Assigns(g.Map{"version": "1.7.0"})
|
2021-05-13 00:16:45 +08:00
|
|
|
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-11-28 11:18:09 +08:00
|
|
|
|
2021-05-13 00:16:45 +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-11-28 11:18:09 +08:00
|
|
|
})
|
|
|
|
}
|
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{
|
2020-03-21 19:41:05 +08:00
|
|
|
"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"})
|
2021-05-13 00:16:45 +08:00
|
|
|
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
|
|
|
|
2021-05-13 00:16:45 +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
|
|
|
})
|
|
|
|
}
|