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