@@ -142,7 +142,7 @@ var testCases []testCase = []testCase{
142
142
doFunc : func (x int ) {},
143
143
callFunc : func (x int , y int ) {},
144
144
args : []interface {}{0 , 1 },
145
- expectPanic : true ,
145
+ expectPanic : false ,
146
146
}, {
147
147
description : "number of args for Do func don't match Call func" ,
148
148
doFunc : func (x int ) bool {
@@ -152,7 +152,7 @@ var testCases []testCase = []testCase{
152
152
return true
153
153
},
154
154
args : []interface {}{0 , 1 },
155
- expectPanic : true ,
155
+ expectPanic : false ,
156
156
}, {
157
157
description : "arg type for Do func incompatible with Call func" ,
158
158
doFunc : func (x int ) {},
@@ -481,6 +481,104 @@ func TestCall_Do(t *testing.T) {
481
481
}
482
482
}
483
483
484
+ func TestCall_Do_NumArgValidation (t * testing.T ) {
485
+ tests := []struct {
486
+ name string
487
+ methodType reflect.Type
488
+ doFn interface {}
489
+ args []interface {}
490
+ wantErr bool
491
+ }{
492
+ {
493
+ name : "too few" ,
494
+ methodType : reflect .TypeOf (func (one , two string ) {}),
495
+ doFn : func (one string ) {},
496
+ args : []interface {}{"too" , "few" },
497
+ wantErr : true ,
498
+ },
499
+ {
500
+ name : "too many" ,
501
+ methodType : reflect .TypeOf (func (one , two string ) {}),
502
+ doFn : func (one , two , three string ) {},
503
+ args : []interface {}{"too" , "few" },
504
+ wantErr : true ,
505
+ },
506
+ {
507
+ name : "just right" ,
508
+ methodType : reflect .TypeOf (func (one , two string ) {}),
509
+ doFn : func (one string , two string ) {},
510
+ args : []interface {}{"just" , "right" },
511
+ wantErr : false ,
512
+ },
513
+ }
514
+ for _ , tt := range tests {
515
+ t .Run (tt .name , func (t * testing.T ) {
516
+ tr := & mockTestReporter {}
517
+ call := & Call {
518
+ t : tr ,
519
+ methodType : tt .methodType ,
520
+ }
521
+ call .Do (tt .doFn )
522
+ call .actions [0 ](tt .args )
523
+ if tt .wantErr && tr .fatalCalls != 1 {
524
+ t .Fatalf ("expected call to fail" )
525
+ }
526
+ if ! tt .wantErr && tr .fatalCalls != 0 {
527
+ t .Fatalf ("expected call to pass" )
528
+ }
529
+ })
530
+ }
531
+ }
532
+
533
+ func TestCall_DoAndReturn_NumArgValidation (t * testing.T ) {
534
+ tests := []struct {
535
+ name string
536
+ methodType reflect.Type
537
+ doFn interface {}
538
+ args []interface {}
539
+ wantErr bool
540
+ }{
541
+ {
542
+ name : "too few" ,
543
+ methodType : reflect .TypeOf (func (one , two string ) string { return "" }),
544
+ doFn : func (one string ) {},
545
+ args : []interface {}{"too" , "few" },
546
+ wantErr : true ,
547
+ },
548
+ {
549
+ name : "too many" ,
550
+ methodType : reflect .TypeOf (func (one , two string ) string { return "" }),
551
+ doFn : func (one , two , three string ) string { return "" },
552
+ args : []interface {}{"too" , "few" },
553
+ wantErr : true ,
554
+ },
555
+ {
556
+ name : "just right" ,
557
+ methodType : reflect .TypeOf (func (one , two string ) string { return "" }),
558
+ doFn : func (one string , two string ) string { return "" },
559
+ args : []interface {}{"just" , "right" },
560
+ wantErr : false ,
561
+ },
562
+ }
563
+ for _ , tt := range tests {
564
+ t .Run (tt .name , func (t * testing.T ) {
565
+ tr := & mockTestReporter {}
566
+ call := & Call {
567
+ t : tr ,
568
+ methodType : tt .methodType ,
569
+ }
570
+ call .DoAndReturn (tt .doFn )
571
+ call .actions [0 ](tt .args )
572
+ if tt .wantErr && tr .fatalCalls != 1 {
573
+ t .Fatalf ("expected call to fail" )
574
+ }
575
+ if ! tt .wantErr && tr .fatalCalls != 0 {
576
+ t .Fatalf ("expected call to pass" )
577
+ }
578
+ })
579
+ }
580
+ }
581
+
484
582
func TestCall_DoAndReturn (t * testing.T ) {
485
583
for _ , tc := range testCases {
486
584
t .Run (tc .description , func (t * testing.T ) {
0 commit comments