-
Notifications
You must be signed in to change notification settings - Fork 2
/
00_testcase_for_test.go
249 lines (203 loc) · 5.96 KB
/
00_testcase_for_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package evendeep
import (
"fmt"
"reflect"
"testing"
"gopkg.in/hedzr/errors.v3"
"github.com/hedzr/evendeep/dbglog"
"github.com/hedzr/evendeep/diff"
"github.com/hedzr/evendeep/flags"
"github.com/hedzr/evendeep/flags/cms"
"github.com/hedzr/evendeep/ref"
"github.com/hedzr/evendeep/typ"
logz "github.com/hedzr/logg/slog"
)
func HelperAssertYes(t *testing.T, b bool, expect, got typ.Any) { //nolint:revive,thelper
if !b {
t.Fatalf("expecting %v but got %v", expect, got)
}
}
func TestNewForTest(t *testing.T) {
copier := New( // nolint:ineffassign
WithValueConverters(&toDurationConverter{}),
WithValueCopiers(&toDurationConverter{}),
WithCloneStyle(),
WithCopyStyle(),
WithCopyStrategyOpt,
WithMergeStrategyOpt,
WithStrategiesReset(),
WithStrategies(cms.SliceMerge, cms.MapMerge),
WithAutoExpandStructOpt,
WithAutoNewForStructFieldOpt,
WithCopyUnexportedFieldOpt,
WithCopyFunctionResultToTargetOpt,
WithPassSourceToTargetFunctionOpt,
WithSyncAdvancingOpt,
WithTryApplyConverterAtFirstOpt,
WithByNameStrategyOpt,
WithByOrdinalStrategyOpt,
WithIgnoreNamesReset(),
WithIgnoreNames("Bugs*", "Test*"),
WithStructTagName(flags.CopyTagName),
WithoutPanic(),
WithStringMarshaller(nil),
)
a := 1
var b int
if err := copier.CopyTo(a, &b); err != nil {
t.Error("bad")
}
}
// NewForTest creates a new copier with most common options.
func NewForTest() DeepCopier {
var copier DeepCopier
lazyInitRoutines()
c1 := newCopier()
WithStrategies(cms.SliceMerge, cms.MapMerge)(c1)
if !c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceMerge, cms.MapMerge, cms.OmitIfEmpty, cms.Default) {
logz.Panic("except flag set with optional values but not matched, 1")
}
c1 = newDeepCopier()
WithStrategies(cms.SliceCopyAppend, cms.MapCopy)(c1)
if !c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceCopyAppend, cms.MapCopy, cms.OmitIfEmpty, cms.Default) {
logz.Panic("except flag set with optional values but not matched, 2")
}
c1 = newCloner()
WithStrategies(cms.SliceCopy)(c1)
if !c1.flags.IsAnyFlagsOK(cms.ByOrdinal, cms.SliceCopy, cms.MapCopy, cms.OmitIfEmpty, cms.Default) {
logz.Panic("except flag set with optional values but not matched, 3")
}
copier = NewFlatDeepCopier(
WithStrategies(cms.SliceMerge, cms.MapMerge),
WithValueConverters(&toDurationConverter{}),
WithValueCopiers(&toDurationConverter{}),
WithCloneStyle(),
WithCopyStyle(),
WithAutoExpandStructOpt,
WithCopyStrategyOpt,
WithStrategiesReset(),
WithMergeStrategyOpt,
WithCopyUnexportedField(true),
WithCopyFunctionResultToTarget(true),
WithIgnoreNamesReset(),
WithIgnoreNames("Bugs*", "Test*"),
)
return copier
}
//
//
//
//
//
//
// Verifier _
type Verifier func(src, dst, expect typ.Any, e error) (err error)
// TestCase _
type TestCase struct {
Description string // description of what test is checking
Src, Dst typ.Any //
Expect typ.Any // expected output
Opts []Opt
Verifier Verifier
}
// NewTestCases _
func NewTestCases(c ...TestCase) []TestCase {
return c
}
// NewTestCase _
func NewTestCase(
description string, // description of what test is checking
src, dst typ.Any, //
expect typ.Any, // expected output
opts []Opt,
verifier Verifier,
) TestCase {
return TestCase{
Description: description, Src: src, Dst: dst,
Expect: expect, Opts: opts, Verifier: verifier,
}
}
// ExtrasOpt for TestCase
type ExtrasOpt func(tc *TestCase)
// RunTestCasesWith _
func RunTestCasesWith(tc *TestCase) (desc string, helperSubtest func(t *testing.T)) {
desc = tc.Description
helperSubtest = func(t *testing.T) { //nolint:thelper
c := NewFlatDeepCopier(tc.Opts...)
err := c.CopyTo(&tc.Src, &tc.Dst)
verifier := tc.Verifier
if verifier == nil {
verifier = DoTestCasesVerifier(t)
}
// t.Logf("\nexpect: %+v\n got: %+v.", tc.expect, tc.dst)
if err = verifier(tc.Src, tc.Dst, tc.Expect, err); err == nil {
return
}
t.Fatalf("%s FAILED, %+v", tc.Description, err)
}
return
}
// DefaultDeepCopyTestRunner _
func DefaultDeepCopyTestRunner(ix int, tc TestCase, opts ...Opt) func(t *testing.T) {
return func(t *testing.T) {
c := NewFlatDeepCopier(append(opts, tc.Opts...)...)
dbglog.Log("- Case %3d: %v", ix, tc.Description)
err := c.CopyTo(&tc.Src, &tc.Dst)
verifier := tc.Verifier
if verifier == nil {
verifier = DoTestCasesVerifier(t)
}
// t.Logf("\nexpect: %+v\n got: %+v.", tc.expect, tc.dst)
if err = verifier(tc.Src, tc.Dst, tc.Expect, err); err == nil {
logz.Print("test passed", "pass-index", ix)
return
}
logz.Error("Error occurs", "index", ix, "error", err)
t.Fatal("FAILED", "index", ix, "desc", tc.Description, "error", err)
}
}
// runTestCases _
func runTestCases(t *testing.T, cases ...TestCase) { //nolint:unused
for ix, tc := range cases {
if !t.Run(fmt.Sprintf("%3d. %s", ix, tc.Description),
DefaultDeepCopyTestRunner(ix, tc)) {
break
}
}
}
// runTestCasesWithOpts _
func runTestCasesWithOpts(t *testing.T, cases []TestCase, opts ...Opt) { //nolint:unused
for ix, tc := range cases {
if !t.Run(fmt.Sprintf("%3d. %s", ix, tc.Description), DefaultDeepCopyTestRunner(ix, tc, opts...)) {
break
}
}
}
func DoTestCasesVerifier(t *testing.T) Verifier {
t.Log()
return func(src, dst, expect typ.Any, e error) (err error) {
a, b := reflect.ValueOf(dst), reflect.ValueOf(expect)
aa, _ := ref.Rdecode(a)
bb, _ := ref.Rdecode(b)
_, _ = src, dst
av, bv := aa.Interface(), bb.Interface()
logz.Print("mismatched",
logz.Group("expect", "bv", bv, "typ", ref.Typfmtv(&bb), "t", aa.Type()),
logz.Group("got", "av", av, "typ", ref.Typfmtv(&aa), "t", bb.Type()),
"error", e)
dif, equal := diff.New(expect, dst,
diff.WithSliceOrderedComparison(false),
diff.WithStripPointerAtFirst(true),
)
if !equal {
_, _ = fmt.Println(dif)
err = errors.New("diff.PrettyDiff identified its not equal:\ndifferent:\n%v", dif).WithErrors(e)
return
}
// if !reflect.DeepEqual(av, bv) {
// err = errors.New("reflect.DeepEqual identified its not equal")
// }
err = e
return
}
}