gf/os/gcfg/gcfg_z_unit_adapter_file_test.go
2021-11-15 20:26:31 +08:00

102 lines
2.2 KiB
Go

// 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.
// go test *.go -bench=".*" -benchmem
package gcfg_test
import (
"testing"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/test/gtest"
)
func TestAdapterFile_SetPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("config.yml")
t.AssertNil(err)
err = c.SetPath("/tmp")
t.AssertNil(err)
err = c.SetPath("gcfg.go")
t.AssertNE(err, nil)
v, err := c.Get(ctx, "name")
t.AssertNE(err, nil)
t.Assert(v, nil)
})
}
func TestAdapterFile_AddPath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("config.yml")
t.AssertNil(err)
err = c.AddPath("/tmp")
t.AssertNil(err)
err = c.AddPath("gcfg.go")
t.AssertNE(err, nil)
v, err := c.Get(ctx, "name")
t.AssertNE(err, nil)
t.Assert(v, nil)
})
}
func TestAdapterFile_SetViolenceCheck(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("config.yml")
t.AssertNil(err)
c.SetViolenceCheck(true)
v, err := c.Get(ctx, "name")
t.AssertNE(err, nil)
t.Assert(v, nil)
})
}
func TestAdapterFile_FilePath(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("config.yml")
t.AssertNil(err)
path, _ := c.GetFilePath("tmp")
t.Assert(path, "")
path, _ = c.GetFilePath("tmp")
t.Assert(path, "")
})
}
func TestAdapterFile_Content(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile()
t.AssertNil(err)
c.SetContent("gf", "config.yml")
t.Assert(c.GetContent("config.yml"), "gf")
c.SetContent("gf1", "config.yml")
t.Assert(c.GetContent("config.yml"), "gf1")
c.RemoveContent("config.yml")
c.ClearContent()
t.Assert(c.GetContent("name"), "")
})
}
func TestAdapterFile_With_UTF8_BOM(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
c, err := gcfg.NewAdapterFile("test-cfg-with-utf8-bom")
t.AssertNil(err)
t.Assert(c.SetPath("testdata"), nil)
c.SetFileName("cfg-with-utf8-bom.toml")
t.Assert(c.MustGet(ctx, "test.testInt"), 1)
t.Assert(c.MustGet(ctx, "test.testStr"), "test")
})
}