-
Notifications
You must be signed in to change notification settings - Fork 7
/
cemetery_test.go
765 lines (662 loc) · 15.7 KB
/
cemetery_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
package gone
import (
"errors"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"reflect"
"testing"
)
type XPoint interface {
GetX() int
GetY() int
GetIndex() int
}
type Point struct {
Flag
x int
y int
Index int
}
func (p *Point) GetX() int {
return p.x
}
func (p *Point) GetY() int {
return p.y
}
func (p *Point) GetIndex() int {
return p.Index
}
func Test_cemetery_revive(t *testing.T) {
type fields struct {
goneList []Tomb
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "inject field is Struct",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
a Point `gone:"point-a"`
b Point `gone:"point-b"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is Struct pointer",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
a *Point `gone:"point-a"`
b *Point `gone:"point-b"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is interface",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
a XPoint `gone:"point-a"`
b XPoint `gone:"point-b"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field(with id) is interface slice",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c []XPoint `gone:"point-a"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: true,
},
{
name: "inject field is interface slice",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c []XPoint `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is struct pointer slice",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c []*Point `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is struct slice",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c []Point `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is map[string]interface",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c map[string]XPoint `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is map[string]interface",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c map[GonerId]XPoint `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is map[string]pointer",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c map[GonerId]*Point `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
{
name: "inject field is map[string]struct",
fields: fields{
goneList: []Tomb{
NewTomb(&struct {
Flag
c map[GonerId]Point `gone:"*"`
}{}).SetId("line"),
NewTomb(&Point{x: 1, y: 2}).SetId("point-a"),
NewTomb(&Point{x: -1, y: -2}).SetId("point-b"),
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &cemetery{
Logger: &defaultLogger{},
tombMap: make(map[GonerId]Tomb),
}
for _, tomb := range tt.fields.goneList {
c.Bury(tomb.GetGoner(), tomb.GetId())
}
if err := c.ReviveAllFromTombs(); (err != nil) != tt.wantErr {
t.Errorf("ReviveAllFromTombs() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
type TestLogger struct {
Flag
defaultLogger
X int
}
type ZeroPoint struct {
Flag
}
func (p *ZeroPoint) GetX() int {
return 0
}
func (p *ZeroPoint) GetY() int {
return 0
}
func (p *ZeroPoint) GetIndex() int {
return 0
}
func Test_cemetery_ReplaceBury(t *testing.T) {
t.Run("replace has default value field", func(t *testing.T) {
c := &cemetery{
Logger: &defaultLogger{},
tombMap: make(map[GonerId]Tomb),
}
c.Bury(c, IdGoneCemetery)
err := c.ReviveAllFromTombs()
assert.Nil(t, err)
logger := TestLogger{X: 100}
c.ReplaceBury(&logger, IdGoneLogger)
assert.Equal(t, c.Logger, &logger)
})
t.Run("replace revived field", func(t *testing.T) {
c := &cemetery{
Logger: &defaultLogger{},
tombMap: make(map[GonerId]Tomb),
}
const line GonerId = "the-line"
type Line struct {
Flag
A XPoint `gone:"point-a"`
B XPoint `gone:"point-b"`
c XPoint `gone:"point-a"`
}
c.Bury(c, IdGoneCemetery).
Bury(&Line{}, line).
Bury(&Point{x: -1, y: -2}, GonerId("point-a")).
Bury(&Point{x: 1, y: 2}, GonerId("point-b"))
err := c.ReviveAllFromTombs()
assert.Nil(t, err)
err = c.ReplaceBury(&ZeroPoint{}, GonerId("point-a"))
assert.Nil(t, err)
tomb := c.GetTomById(line)
goner := tomb.GetGoner().(*Line)
assert.Equal(t, goner.A.GetIndex(), 0)
assert.Equal(t, goner.A.GetX(), 0)
assert.Equal(t, goner.A.GetY(), 0)
})
t.Run("replace revived with empty goneId", func(t *testing.T) {
c := newCemetery()
err := c.ReplaceBury(&ZeroPoint{})
assert.Equal(t, err.(Error).Code(), ReplaceBuryIdParamEmpty)
})
t.Run("replace revived failed", func(t *testing.T) {
c := &cemetery{
Logger: &defaultLogger{},
tombMap: make(map[GonerId]Tomb),
}
const line GonerId = "the-line"
type Line struct {
Flag
A XPoint `gone:"point-a"`
B XPoint `gone:"point-b"`
c XPoint `gone:"point-a"`
z XPoint `gone:"point-z"`
}
c.Bury(c, IdGoneCemetery).
Bury(&Line{
z: &Point{},
}, line).
Bury(&Point{x: -1, y: -2}, GonerId("point-a")).
Bury(&Point{x: 1, y: 2}, GonerId("point-b"))
err := c.ReviveAllFromTombs()
assert.Nil(t, err)
err = c.ReplaceBury(&Line{}, line)
assert.NotNil(t, err)
})
}
type identityGoner struct {
Flag
}
func (i *identityGoner) GetId() GonerId {
return "identityGoner"
}
func TestGetGoneDefaultId(t *testing.T) {
type args struct {
goner Goner
}
tests := []struct {
name string
args args
want GonerId
}{
{
name: "identityGoner",
args: args{goner: &identityGoner{}},
want: "github.com/gone-io/gone/identityGoner#identityGoner",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetGoneDefaultId(tt.args.goner), "GetGoneDefaultId(%v)", tt.args.goner)
})
}
}
func Test_cemetery_bury(t *testing.T) {
t.Run("GonerIdIsExistedError", func(t *testing.T) {
executed := false
func() {
defer func() {
a := recover()
assert.Equal(t, a.(Error).Code(), GonerIdIsExisted)
}()
c := newCemetery()
c.Bury(&Point{x: 1, y: 2}, GonerId("point-a"))
executed = true
c.Bury(&Point{x: 1, y: 2}, GonerId("point-a"))
}()
assert.True(t, executed)
})
}
func Test_parseGoneTagId(t *testing.T) {
id, _ := parseGoneTagId("")
assert.Equal(t, string(id), "")
id, _ = parseGoneTagId("xxx")
assert.Equal(t, string(id), "xxx")
id, ext := parseGoneTagId("xxx,2222,2222333")
assert.Equal(t, string(id), "xxx")
assert.Equal(t, "2222,2222333", ext)
}
type vampire1 struct {
Flag
}
func (g *vampire1) Suck(conf string, v reflect.Value) SuckError {
v.SetString(conf)
return nil
}
type vampire2 struct {
Flag
}
func (g *vampire2) Suck(conf string, v reflect.Value, field reflect.StructField) error {
v.SetString(conf + ":" + field.Name)
return nil
}
func Test_cemetery_reviveFieldById(t *testing.T) {
Prepare(func(cemetery Cemetery) error {
cemetery.
BuryOnce(&vampire1{}, GonerId("v1")).
BuryOnce(&vampire1{}, GonerId("v1")).
BuryOnce(&vampire2{}, GonerId("v2")).
BuryOnce(&vampire2{}, GonerId("v2"))
return nil
}).AfterStart(func(in struct {
test1 string `gone:"v1,xxxx"`
test2 string `gone:"v2,xxxx"`
test3 []*vampire2 `gone:"*"`
test4 []*vampire1 `gone:"*"`
}) {
assert.Equal(t, "xxxx", in.test1)
assert.Equal(t, "xxxx:test2", in.test2)
assert.Equal(t, 1, len(in.test3))
assert.Equal(t, 1, len(in.test4))
}).Run()
}
func Test_cemetery_checkRevive(t *testing.T) {
type Line struct {
Flag
PointA *Point `gone:"point-a"`
PointB *Point `gone:"point-b"`
}
test := NewBuryMockCemeteryForTest()
test.
Bury(&Point{x: 1, y: 2, Index: 1}, GonerId("point-a")).
Bury(&Point{x: 1, y: 2, Index: 2}, GonerId("point-b")).
Bury(&Line{}, GonerId("line"))
theTomb := test.GetTomById(GonerId("line"))
err := test.(*cemetery).checkRevive(theTomb)
assert.Nil(t, err)
}
func Test_cemetery_InjectFuncParameters(t *testing.T) {
Prepare().Test(func(cemetery Cemetery) {
t.Run("fn is not a func", func(t *testing.T) {
_, err := cemetery.InjectFuncParameters("", nil, nil)
assert.Error(t, err)
assert.Equal(t, err.(Error).Code(), NotCompatible)
})
t.Run("fn has two parameters", func(t *testing.T) {
executed := false
fn := func(cemetery Cemetery, in struct {
cemetery Cemetery `gone:"*"`
}) {
assert.NotNil(t, in.cemetery)
assert.NotNil(t, cemetery)
assert.Equal(t, cemetery, in.cemetery)
executed = true
}
args, err := cemetery.InjectFuncParameters(fn, nil, nil)
assert.Nil(t, err)
reflect.ValueOf(fn).Call(args)
assert.True(t, executed)
})
t.Run("fn parameter is not struct", func(t *testing.T) {
fn := func(int) {}
_, err := cemetery.InjectFuncParameters(fn, nil, nil)
assert.Error(t, err)
assert.Equal(t, err.(Error).Code(), NotCompatible)
})
t.Run("filter some parameters", func(t *testing.T) {
executed := false
fn := func(
cemetery Cemetery,
in struct {
cemetery Cemetery `gone:"*"`
},
test bool,
) {
assert.NotNil(t, in.cemetery)
assert.NotNil(t, cemetery)
assert.Equal(t, cemetery, in.cemetery)
assert.Equal(t, true, test)
executed = true
}
args, err := cemetery.InjectFuncParameters(fn, func(pt reflect.Type, i int) any {
if i == 2 {
return true
}
return nil
}, func(pt reflect.Type, i int) {
assert.Equal(t, 1, i)
})
assert.Nil(t, err)
assert.Equal(t, 3, len(args))
reflect.ValueOf(fn).Call(args)
assert.True(t, executed)
})
t.Run("Revive Failed", func(t *testing.T) {
fn := func(in struct {
cemetery Cemetery `gone:"xxxxx"`
}) {
}
_, err := cemetery.InjectFuncParameters(fn, nil, nil)
assert.Error(t, err)
assert.Equal(t, CannotFoundGonerById, err.(Error).Code())
})
})
}
func Test_cemetery_BuryOnce(t *testing.T) {
test := NewBuryMockCemeteryForTest()
defer func() {
err := recover()
assert.Error(t, err.(Error))
}()
type X struct {
Flag
}
test.BuryOnce(&X{})
}
func Test_cemetery_replaceTombsGonerField(t *testing.T) {
type X struct {
Flag
s string `gone:"s"`
}
type Y struct {
Flag
}
Prepare().Test(func(cemetery Cemetery) {
err := cemetery.Bury(&X{}).ReplaceBury(&Y{}, GonerId("s"))
assert.Error(t, err)
})
}
type sucker struct {
Flag
s string `gone:"s"`
}
func (s *sucker) Suck(conf string, v reflect.Value) SuckError {
v.SetString(conf)
return nil
}
type beenSuck struct {
Flag
s string `gone:"xxx"`
}
func Test_cemetery_reviveByVampire(t *testing.T) {
Prepare().Test(func(cemetery Cemetery) {
cemetery.Bury(&sucker{}, GonerId("xxx"))
suck := beenSuck{}
_, err := cemetery.ReviveOne(&suck)
assert.Error(t, err)
})
}
type sucker2 struct {
Flag
s string `gone:"s"`
}
func (s *sucker2) Suck(conf string, v reflect.Value, field reflect.StructField) error {
v.SetString(conf)
return nil
}
func Test_cemetery_reviveByVampire2(t *testing.T) {
Prepare().Test(func(cemetery Cemetery) {
cemetery.Bury(&sucker2{}, GonerId("xxx"))
suck := beenSuck{}
_, err := cemetery.ReviveOne(&suck)
assert.Error(t, err)
})
}
type depOnBeenSuck struct {
Flag
beenSuck beenSuck `gone:"*"`
}
func Test_cemetery_reviveOneAndItsDeps(t *testing.T) {
t.Run("error", func(t *testing.T) {
Prepare().Test(func(c Cemetery) {
suck := beenSuck{}
newTomb := NewTomb(&suck)
_, err := c.(*cemetery).reviveOneAndItsDeps(newTomb)
assert.Error(t, err)
})
})
t.Run("error in revive deps", func(t *testing.T) {
Prepare().Test(func(c Cemetery) {
c.Bury(&beenSuck{})
suck := depOnBeenSuck{}
newTomb := NewTomb(&suck)
_, err := c.(*cemetery).reviveOneAndItsDeps(newTomb)
assert.Error(t, err)
})
})
t.Run("success", func(t *testing.T) {
Prepare().Test(func(c Cemetery) {
type Y struct {
Flag
}
type X struct {
Flag
y Y `gone:"*"`
}
type DepOnX struct {
Flag
X X `gone:"*"`
}
c.Bury(&X{}).Bury(&Y{})
suck := DepOnX{}
newTomb := NewTomb(&suck)
tombs, err := c.(*cemetery).reviveOneAndItsDeps(newTomb)
assert.Nil(t, err)
assert.Equal(t, 2, len(tombs))
})
})
}
func Test_cemetery_ReviveOne(t *testing.T) {
t.Run("error", func(t *testing.T) {
type X struct {
Flag
y string `gone:"xxx"`
}
type DepOnX struct {
Flag
X X `gone:"*"`
}
Prepare().Test(func(c Cemetery) {
_, err := c.ReviveOne(&DepOnX{})
assert.Error(t, err)
})
})
}
func Test_cemetery_prophesy(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
prophet := NewMockProphet(controller)
prophet2 := NewMockProphet2(controller)
t.Run("Suc", func(t *testing.T) {
Prepare().Test(func(c Cemetery) {
prophet.EXPECT().AfterRevive().Return(nil)
prophet2.EXPECT().AfterRevive().Return(nil)
c.Bury(prophet).Bury(prophet2)
err := c.(*cemetery).prophesy()
assert.Nil(t, err)
})
})
t.Run("prophet err", func(t *testing.T) {
Prepare().Test(func(c Cemetery) {
err := errors.New("err")
prophet.EXPECT().AfterRevive().Return(err)
c.Bury(prophet)
err1 := c.(*cemetery).prophesy()
assert.Equal(t, err, err1)
})
})
t.Run("prophet err", func(t *testing.T) {
Prepare().Test(func(c Cemetery) {
err := errors.New("err")
prophet2.EXPECT().AfterRevive().Return(err)
c.Bury(prophet2)
err1 := c.(*cemetery).prophesy()
assert.Equal(t, err, err1)
})
})
}
func Test_cemetery_getGonerContainerByType(t *testing.T) {
Prepare().Test(func(c Cemetery) {
type X struct {
Flag
}
type DepOnX struct {
Flag
X X `gone:"*"`
}
c.Bury(&X{}, GonerId("x1")).Bury(&X{}, GonerId("x2"))
_, err := c.ReviveOne(&DepOnX{})
assert.Nil(t, err)
})
}
var afterReviveOrder = make([]int, 0)
var startOrder = make([]int, 0)
var stopOrder = make([]int, 0)
type ordered struct {
Flag
order int
}
func (o *ordered) AfterRevive() error {
afterReviveOrder = append(afterReviveOrder, o.order)
return nil
}
func (o *ordered) Start(Cemetery) error {
startOrder = append(startOrder, o.order)
return nil
}
func (o *ordered) Stop(Cemetery) error {
stopOrder = append(stopOrder, o.order)
return nil
}
func TestOrder(t *testing.T) {
Prepare(func(cemetery Cemetery) error {
cemetery.
Bury(&ordered{order: 2}, Order(2)).
Bury(&ordered{order: 1}, Order(1)).
Bury(&ordered{order: 3}, Order(3))
return nil
}).Test(func() {})
assert.Equal(t, []int{1, 2, 3}, afterReviveOrder)
assert.Equal(t, []int{3, 2, 1}, startOrder)
assert.Equal(t, []int{1, 2, 3}, stopOrder)
}