github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/env/env_z_unit_test.go (about) 1 package env_test 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 8 . "github.com/onsi/gomega" 9 10 "github.com/machinefi/w3bstream/pkg/depends/base/types" 11 . "github.com/machinefi/w3bstream/pkg/depends/conf/env" 12 "github.com/machinefi/w3bstream/pkg/depends/x/ptrx" 13 ) 14 15 func TestPathWalker(t *testing.T) { 16 pw := NewPathWalker() 17 18 pw.Enter("parent") 19 NewWithT(t).Expect(pw.Paths()).To(Equal([]interface{}{"parent"})) 20 NewWithT(t).Expect(pw.String()).To(Equal("parent")) 21 22 pw.Enter("child") 23 NewWithT(t).Expect(pw.Paths()).To(Equal([]interface{}{"parent", "child"})) 24 NewWithT(t).Expect(pw.String()).To(Equal("parent_child")) 25 26 pw.Enter("prop") 27 NewWithT(t).Expect(pw.Paths()).To(Equal([]interface{}{"parent", "child", "prop"})) 28 NewWithT(t).Expect(pw.String()).To(Equal("parent_child_prop")) 29 30 pw.Enter(100) 31 NewWithT(t).Expect(pw.Paths()).To(Equal([]interface{}{"parent", "child", "prop", 100})) 32 NewWithT(t).Expect(pw.String()).To(Equal("parent_child_prop_100")) 33 34 pw.Exit() 35 NewWithT(t).Expect(pw.Paths()).To(Equal([]interface{}{"parent", "child", "prop"})) 36 NewWithT(t).Expect(pw.String()).To(Equal("parent_child_prop")) 37 38 pw.Exit() 39 NewWithT(t).Expect(pw.Paths()).To(Equal([]interface{}{"parent", "child"})) 40 NewWithT(t).Expect(pw.String()).To(Equal("parent_child")) 41 } 42 43 type SubConfig struct { 44 Duration types.Duration 45 Password types.Password `env:""` 46 Key string `env:""` 47 Bool bool 48 Map map[string]string 49 Func func() error 50 defaultValue bool 51 } 52 53 func (c *SubConfig) SetDefault() { 54 c.defaultValue = true 55 } 56 57 type Config struct { 58 Map map[string]string 59 Slice []string `env:""` 60 PtrString *string `env:""` 61 Host string `env:""` 62 SubConfig 63 Config SubConfig 64 } 65 66 func TestEnvVars(t *testing.T) { 67 c := Config{} 68 69 c.Duration = types.Duration(time.Hour + 2*time.Second + 3*time.Microsecond) 70 c.Password = types.Password("123123") 71 c.Key = "123456" 72 c.PtrString = ptrx.String("123456=") 73 c.Slice = []string{"1", "2"} 74 c.Config.Key = "key" 75 c.Config.defaultValue = true 76 c.defaultValue = true 77 78 envVars := NewVars("S") 79 80 t.Run("Encoding", func(t *testing.T) { 81 data, _ := NewEncoder(envVars).Encode(&c) 82 NewWithT(t).Expect(string(data)).To( 83 Equal(`S__Bool=false 84 S__Config_Bool=false 85 S__Config_Duration=0s 86 S__Config_Key=key 87 S__Config_Password= 88 S__Duration=1h2s3us 89 S__Host= 90 S__Key=123456 91 S__Password=123123 92 S__PtrString=123456= 93 S__Slice_0=1 94 S__Slice_1=2 95 `)) 96 }) 97 98 t.Run("Decoding", func(t *testing.T) { 99 data, _ := NewEncoder(envVars).Encode(&c) 100 101 envVars := LoadVarsFromEnviron("S", strings.Split(string(data), "\n")) 102 103 c2 := Config{} 104 err := NewDecoder(envVars).Decode(&c2) 105 106 NewWithT(t).Expect(err).To(BeNil()) 107 NewWithT(t).Expect(c2).To(Equal(c)) 108 }) 109 }