github.com/gogf/gf@v1.16.9/os/gcfg/gcfg_z_unit_instance_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 // go test *.go -bench=".*" -benchmem 8 9 package gcfg 10 11 import ( 12 "github.com/gogf/gf/container/gmap" 13 "github.com/gogf/gf/debug/gdebug" 14 "github.com/gogf/gf/os/genv" 15 "github.com/gogf/gf/os/gfile" 16 "github.com/gogf/gf/test/gtest" 17 "testing" 18 ) 19 20 func Test_Instance_Basic(t *testing.T) { 21 config := ` 22 array = [1.0, 2.0, 3.0] 23 v1 = 1.0 24 v2 = "true" 25 v3 = "off" 26 v4 = "1.234" 27 28 [redis] 29 cache = "127.0.0.1:6379,1" 30 disk = "127.0.0.1:6379,0" 31 32 ` 33 gtest.C(t, func(t *gtest.T) { 34 path := DefaultConfigFile 35 err := gfile.PutContents(path, config) 36 t.Assert(err, nil) 37 defer func() { 38 t.Assert(gfile.Remove(path), nil) 39 }() 40 41 c := Instance() 42 t.Assert(c.Get("v1"), 1) 43 t.AssertEQ(c.GetInt("v1"), 1) 44 t.AssertEQ(c.GetInt8("v1"), int8(1)) 45 t.AssertEQ(c.GetInt16("v1"), int16(1)) 46 t.AssertEQ(c.GetInt32("v1"), int32(1)) 47 t.AssertEQ(c.GetInt64("v1"), int64(1)) 48 t.AssertEQ(c.GetUint("v1"), uint(1)) 49 t.AssertEQ(c.GetUint8("v1"), uint8(1)) 50 t.AssertEQ(c.GetUint16("v1"), uint16(1)) 51 t.AssertEQ(c.GetUint32("v1"), uint32(1)) 52 t.AssertEQ(c.GetUint64("v1"), uint64(1)) 53 54 t.AssertEQ(c.GetVar("v1").String(), "1") 55 t.AssertEQ(c.GetVar("v1").Bool(), true) 56 t.AssertEQ(c.GetVar("v2").String(), "true") 57 t.AssertEQ(c.GetVar("v2").Bool(), true) 58 59 t.AssertEQ(c.GetString("v1"), "1") 60 t.AssertEQ(c.GetFloat32("v4"), float32(1.234)) 61 t.AssertEQ(c.GetFloat64("v4"), float64(1.234)) 62 t.AssertEQ(c.GetString("v2"), "true") 63 t.AssertEQ(c.GetBool("v2"), true) 64 t.AssertEQ(c.GetBool("v3"), false) 65 66 t.AssertEQ(c.Contains("v1"), true) 67 t.AssertEQ(c.Contains("v2"), true) 68 t.AssertEQ(c.Contains("v3"), true) 69 t.AssertEQ(c.Contains("v4"), true) 70 t.AssertEQ(c.Contains("v5"), false) 71 72 t.AssertEQ(c.GetInts("array"), []int{1, 2, 3}) 73 t.AssertEQ(c.GetStrings("array"), []string{"1", "2", "3"}) 74 t.AssertEQ(c.GetArray("array"), []interface{}{1, 2, 3}) 75 t.AssertEQ(c.GetInterfaces("array"), []interface{}{1, 2, 3}) 76 t.AssertEQ(c.GetMap("redis"), map[string]interface{}{ 77 "disk": "127.0.0.1:6379,0", 78 "cache": "127.0.0.1:6379,1", 79 }) 80 filepath, _ := c.GetFilePath() 81 t.AssertEQ(filepath, gfile.Pwd()+gfile.Separator+path) 82 }) 83 } 84 85 func Test_Instance_AutoLocateConfigFile(t *testing.T) { 86 gtest.C(t, func(t *gtest.T) { 87 t.Assert(Instance("gf") != nil, true) 88 }) 89 // Automatically locate the configuration file with supported file extensions. 90 gtest.C(t, func(t *gtest.T) { 91 pwd := gfile.Pwd() 92 t.AssertNil(gfile.Chdir(gdebug.TestDataPath())) 93 defer gfile.Chdir(pwd) 94 t.Assert(Instance("c1") != nil, true) 95 t.Assert(Instance("c1").Get("my-config"), "1") 96 t.Assert(Instance("folder1/c1").Get("my-config"), "2") 97 }) 98 // Automatically locate the configuration file with supported file extensions. 99 gtest.C(t, func(t *gtest.T) { 100 pwd := gfile.Pwd() 101 t.AssertNil(gfile.Chdir(gdebug.TestDataPath("folder1"))) 102 defer gfile.Chdir(pwd) 103 t.Assert(Instance("c2").Get("my-config"), 2) 104 }) 105 // Default configuration file. 106 gtest.C(t, func(t *gtest.T) { 107 instances.Clear() 108 pwd := gfile.Pwd() 109 t.AssertNil(gfile.Chdir(gdebug.TestDataPath("default"))) 110 defer gfile.Chdir(pwd) 111 t.Assert(Instance().Get("my-config"), 1) 112 113 instances.Clear() 114 t.AssertNil(genv.Set("GF_GCFG_FILE", "config.json")) 115 defer genv.Set("GF_GCFG_FILE", "") 116 t.Assert(Instance().Get("my-config"), 2) 117 }) 118 } 119 120 func Test_Instance_EnvPath(t *testing.T) { 121 gtest.C(t, func(t *gtest.T) { 122 genv.Set("GF_GCFG_PATH", gdebug.TestDataPath("envpath")) 123 defer genv.Set("GF_GCFG_PATH", "") 124 t.Assert(Instance("c3") != nil, true) 125 t.Assert(Instance("c3").Get("my-config"), "3") 126 t.Assert(Instance("c4").Get("my-config"), "4") 127 instances = gmap.NewStrAnyMap(true) 128 }) 129 } 130 131 func Test_Instance_EnvFile(t *testing.T) { 132 gtest.C(t, func(t *gtest.T) { 133 genv.Set("GF_GCFG_PATH", gdebug.TestDataPath("envfile")) 134 defer genv.Set("GF_GCFG_PATH", "") 135 genv.Set("GF_GCFG_FILE", "c6.json") 136 defer genv.Set("GF_GCFG_FILE", "") 137 t.Assert(Instance().Get("my-config"), "6") 138 instances = gmap.NewStrAnyMap(true) 139 }) 140 }