github.com/hedzr/evendeep@v0.4.8/tag_test.go (about) 1 package evendeep 2 3 import ( 4 "github.com/hedzr/evendeep/flags" 5 "github.com/hedzr/evendeep/flags/cms" 6 "github.com/hedzr/evendeep/internal/tool" 7 8 "reflect" 9 "testing" 10 ) 11 12 func TestFieldTags_Parse(t *testing.T) { 13 t.Run("test fieldTags parse", subtestParse) 14 t.Run("test fieldTags flags tests", subtestFlagTests) 15 } 16 17 type AFT struct { 18 flat01 *int `copy:",flat"` 19 20 flags flags.Flags `copy:",cleareq"` //nolint:unused,structcheck //test only 21 converter *ValueConverter //nolint:unused,structcheck //test only 22 wouldBe int `copy:",must,keepneq,omitzero,slicecopyappend,mapmerge"` //nolint:unused,structcheck //test only 23 24 ignored01 int `copy:"-"` 25 } 26 27 func prepareAFT() (a AFT, expects []flags.Flags) { 28 expects = []flags.Flags{ 29 // flat01 30 {cms.Flat: true, cms.Default: true, cms.SliceCopy: true, cms.MapCopy: true, cms.NoOmitTarget: true, cms.NoOmit: true, cms.ByOrdinal: true}, 31 32 {cms.Default: true, cms.ClearIfEq: true, cms.SliceCopy: true, cms.MapCopy: true, cms.NoOmitTarget: true, cms.NoOmit: true, cms.ByOrdinal: true}, 33 {cms.Default: true, cms.SliceCopy: true, cms.MapCopy: true, cms.NoOmitTarget: true, cms.NoOmit: true, cms.ByOrdinal: true}, 34 {cms.Must: true, cms.KeepIfNotEq: true, cms.SliceCopyAppend: true, cms.MapMerge: true, cms.NoOmitTarget: true, cms.OmitIfZero: true, cms.ByOrdinal: true}, 35 36 // ignored01 37 {cms.Ignore: true, cms.SliceCopy: true, cms.MapCopy: true, cms.NoOmitTarget: true, cms.NoOmit: true, cms.ByOrdinal: true}, 38 39 {cms.ByOrdinal: true, cms.ByName: true}, 40 } 41 42 return 43 } 44 45 func subtestParse(t *testing.T) { 46 47 a, expects := prepareAFT() 48 49 // c := newCopier() 50 51 v := reflect.ValueOf(&a) 52 v = tool.Rindirect(v) 53 54 for i := 0; i < v.NumField(); i++ { 55 fld := v.Type().Field(i) 56 ft := parseFieldTags(fld.Tag, "") 57 if !ft.isFlagIgnored() { 58 t.Logf("%q flags: %v [without ignore]", fld.Tag, ft) 59 } else { 60 t.Logf("%q flags: %v [ignore]", fld.Tag, ft) 61 } 62 testDeepEqual(t.Errorf, ft.flags, expects[i]) 63 } 64 } 65 66 func subtestFlagTests(t *testing.T) { 67 type AFS1 struct { 68 flags flags.Flags `copy:",cleareq,must"` //nolint:unused,structcheck //test 69 converter *ValueConverter `copy:",ignore"` //nolint:unused,structcheck //test 70 wouldbe int `copy:",must,keepneq,omitzero,slicecopyappend,mapmerge"` //nolint:unused,structcheck //test 71 } 72 var a AFS1 73 v := reflect.ValueOf(&a) 74 v = tool.Rindirect(v) 75 sf, _ := v.Type().FieldByName("wouldbe") 76 sf0, _ := v.Type().FieldByName("flags") 77 sf1, _ := v.Type().FieldByName("converter") 78 79 var ft fieldTags 80 ft.Parse(sf.Tag, "") 81 ft.Parse(sf0.Tag, "") // entering 'continue' branch 82 ft.Parse(sf1.Tag, "") // entering 'delete' branch 83 84 var z *fieldTags 85 z.isFlagExists(cms.SliceCopy) 86 87 v = reflect.ValueOf(&z) 88 tool.Rwant(v, reflect.Struct) 89 ve := v.Elem() 90 t.Logf("z: %v, nil: %v", tool.Valfmt(&ve), tool.Valfmt(nil)) 91 92 var nilArray = [1]*int{(*int)(nil)} 93 v = reflect.ValueOf(nilArray) 94 t.Logf("nilArray: %v, nil: %v", tool.Valfmt(&v), tool.Valfmt(nil)) 95 96 v = reflect.ValueOf(&fieldTags{ 97 flags: nil, 98 converter: nil, 99 copier: nil, 100 nameConverter: nil, 101 nameConvertRule: "", 102 }) 103 tool.Rwant(v, reflect.Struct) 104 105 var ss1 = []int{8, 9} 106 var ss2 = []int64{} 107 var ss3 = []int{} 108 var ss4 = [4]int{} 109 var vv1 = reflect.ValueOf(ss1) 110 var tt3 = reflect.TypeOf(ss3) 111 var tp4 = reflect.TypeOf(&ss4) 112 t.Logf("ss1.type: %v", tool.Typfmtv(&vv1)) 113 t.Log(tool.CanConvertHelper(reflect.ValueOf(&ss1), reflect.TypeOf(&ss2))) 114 t.Log(tool.CanConvertHelper(vv1, reflect.TypeOf(ss2))) 115 t.Log(tool.CanConvertHelper(vv1, tt3)) 116 t.Log(tool.CanConvertHelper(vv1, tp4)) 117 } 118 119 func TestFieldTags_CalcTargetName(t *testing.T) { 120 ft := fieldTags{ 121 flags: flags.Flags{ 122 cms.Default: true, 123 }, 124 converter: nil, 125 copier: nil, 126 nameConverter: func(source string, ctx *NameConverterContext) (target string, ok bool) { 127 if source == "" { 128 target, ok = "hehe", true 129 } 130 return 131 }, 132 nameConvertRule: "", 133 } 134 135 if tn, ok := ft.CalcTargetName("", nil); !ok || tn != "hehe" { 136 t.FailNow() 137 } 138 }