github.com/hxx258456/fabric-ca-gm@v0.0.3-0.20221111064038-a268ad7e3a37/internal/pkg/util/flag_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package util_test 8 9 import ( 10 "testing" 11 "time" 12 13 . "github.com/hxx258456/fabric-ca-gm/internal/pkg/util" 14 "github.com/spf13/pflag" 15 "github.com/spf13/viper" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 // A test struct 20 type A struct { 21 ADur time.Duration `help:"Duration"` 22 ASlice []string `help:"Slice description"` 23 AStr string `def:"defval" help:"Str1 description"` 24 AInt int `def:"10" help:"Int1 description"` 25 AB B `help:"FB description"` 26 AStr2 string `skip:"true"` 27 AIntArray []int `help:"IntArray description"` 28 AMap map[string]string `skip:"true"` 29 ABPtr *B `help:"FBP description"` 30 AInterface interface{} `skip:"true"` 31 ABad ABad `skip:"true"` 32 } 33 34 // B test struct 35 type B struct { 36 BStr string `help:"Str description"` 37 BInt int `skip:"true"` 38 BCPtr *C 39 } 40 41 // C test struct 42 type C struct { 43 CBool bool `def:"true" help:"Bool description"` 44 CStr string `help:"Str description"` 45 } 46 47 type ABad struct{} 48 49 type DurBad struct { 50 ADur time.Duration `def:"xx" help:"Duration"` 51 } 52 53 type Int64Struct struct { 54 Int64Var int64 `def:"3546343826724305832" help:"int64"` 55 } 56 57 func TestRegisterFlags(t *testing.T) { 58 tags := map[string]string{ 59 "help.fb.int": "This is an int field", 60 } 61 err := RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &A{}, tags) 62 assert.NoError(t, err, "failed to RegisterFlags for A") 63 64 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &C{}, tags) 65 assert.NoError(t, err, "failed to RegisterFlags for C") 66 67 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &Int64Struct{}, tags) 68 assert.NoError(t, err, "Failed to register int64 flag") 69 } 70 71 func TestParseObj(t *testing.T) { 72 cb := func(*Field) error { return nil } 73 err := ParseObj(&A{}, cb, nil) 74 assert.NoError(t, err, "failed to parse A") 75 76 err = ParseObj(&A{}, nil, nil) 77 assert.EqualError(t, err, "nil callback", "parse with nil callback should have failed") 78 } 79 80 func TestCheckForMissingValues(t *testing.T) { 81 src := &A{ 82 ADur: time.Hour, 83 AStr: "AStr", 84 AStr2: "AStr2", 85 AIntArray: []int{1, 2, 3}, 86 AMap: map[string]string{"Key1": "Val1", "Key2": "Val2"}, 87 AB: B{ 88 BStr: "BStr", 89 BCPtr: &C{ 90 CBool: true, 91 CStr: "CStr", 92 }, 93 }, 94 ABPtr: &B{ 95 BStr: "BStr", 96 BCPtr: &C{ 97 CBool: false, 98 CStr: "CStr", 99 }, 100 }, 101 AInterface: &C{ 102 CStr: "CStr", 103 }, 104 } 105 106 dst := &A{ 107 AStr2: "dstAStr2", 108 AInt: 2, 109 } 110 111 CopyMissingValues(src, dst) 112 113 assert.Equal(t, src.AStr, dst.AStr, "failed to copy field AStr") 114 assert.Equal(t, src.AB.BStr, dst.AB.BStr, "failed to copy field AB.BStr") 115 assert.Equal(t, src.ABPtr.BStr, dst.ABPtr.BStr, "failed to copy field ABPtr.BStr") 116 assert.Equal(t, src.ABPtr.BCPtr.CStr, dst.ABPtr.BCPtr.CStr, "failed to copy field ABPtr.BCPtr.CStr") 117 assert.Equal(t, src.AMap, dst.AMap, "failed to copy AMap") 118 assert.Equal(t, src.AIntArray, dst.AIntArray, "failed to copy AIntArray") 119 assert.Equal(t, "dstAStr2", dst.AStr2, "incorrectly replaced AStr2") 120 assert.Equal(t, 2, dst.AInt, "incorrectly replaced AInt") 121 } 122 123 func TestRegisterFlagsInvalidArgs(t *testing.T) { 124 data := struct{ Field string }{} 125 err := RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &data, nil) 126 assert.Error(t, err) 127 assert.Contains(t, err.Error(), "Field is missing a help tag") 128 129 data2 := struct{ Field bool }{} 130 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &data2, nil) 131 assert.Error(t, err) 132 assert.Contains(t, err.Error(), "Field is missing a help tag") 133 134 data3 := struct{ Field int }{} 135 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &data3, nil) 136 assert.Error(t, err) 137 assert.Contains(t, err.Error(), "Field is missing a help tag") 138 139 data4 := struct{ Field []string }{} 140 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &data4, nil) 141 assert.Error(t, err) 142 assert.Contains(t, err.Error(), "Field is missing a help tag") 143 144 data5 := struct{ Field time.Duration }{} 145 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &data5, nil) 146 assert.Error(t, err) 147 assert.Contains(t, err.Error(), "Field is missing a help tag") 148 149 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &DurBad{}, nil) 150 assert.Error(t, err) 151 assert.Contains(t, err.Error(), "Invalid duration value in 'def' tag") 152 153 data6 := struct{ Field float32 }{} 154 err = RegisterFlags(viper.GetViper(), &pflag.FlagSet{}, &data6, nil) 155 assert.NoError(t, err) 156 }