-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcall_test.go
142 lines (138 loc) · 3.74 KB
/
call_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
package vermock
import (
"errors"
"reflect"
"testing"
)
func TestDoCall(t *testing.T) {
tests := []struct {
name string
callables Callables
in []reflect.Value
out []reflect.Value
results []reflect.Value
expectFail bool
expectPanic bool
}{
{
name: "Matching types and values",
callables: Callables{Value{Value: reflect.ValueOf(func(t testing.TB, in string) string {
if in != "input" {
t.Errorf("unexpected input: expected %q, got %q", "input", in)
}
return "result"
})}},
in: toValues("input"),
out: toValues(new(string)),
results: toValues("result"),
expectFail: false,
},
{
name: "Matching types and values, multi",
callables: Callables{multi{Value: reflect.ValueOf(func(t testing.TB, count CallCount, in string) string {
if count != 0 {
t.Errorf("unexpected count: expected %d, got %d", 0, count)
}
if in != "input" {
t.Errorf("unexpected input: expected %q, got %q", "input", in)
}
return "result"
})}},
in: toValues("input"),
out: toValues(new(string)),
results: toValues("result"),
expectFail: false,
},
{
name: "Matching types and values, variadic",
callables: Callables{Value{Value: reflect.ValueOf(func(t testing.TB, in ...string) string {
if in[0] != "input" {
t.Errorf("unexpected input: expected %q, got %q", "input", in)
}
return "result"
})}},
in: toValues([]string{"input"}),
out: toValues(new(string)),
results: toValues("result"),
expectFail: false,
},
{
name: "Type mismatch",
callables: Callables{Value{Value: reflect.ValueOf(func() string {
return "result"
})}},
in: toValues(),
out: toValues(new(int)),
results: toValues(0),
expectFail: true,
expectPanic: true,
},
{
name: "Unexpected number of results, panic",
callables: Callables{Value{Value: reflect.ValueOf(func() {})}},
in: toValues(),
out: toValues(new(int)),
results: toValues(0),
expectFail: true,
expectPanic: true,
},
{
name: "Unexpected number of results, error",
callables: Callables{Value{Value: reflect.ValueOf(func() {})}},
in: toValues(),
out: toValues(new(error)),
results: toValues(errors.New("unexpected number of results: expected 1, got 0")),
expectFail: true,
},
{
name: "Unexpected number of calls",
callables: Callables{},
in: toValues(),
out: toValues(new(error)),
results: toValues(errors.New("unexpected call to testMethod")),
expectFail: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
key := &tt.name
mockT := new(testing.T)
defer func() {
// Check for errors in test output
if tt.expectFail && !mockT.Failed() {
t.Errorf("expected a failure, got none")
} else if !tt.expectFail && mockT.Failed() {
t.Errorf("expected no failure, got fail")
}
if len(tt.out) != len(tt.results) {
t.Fatalf("expected %d results, got %d", len(tt.results), len(tt.out))
}
for i := range tt.results {
if !reflect.DeepEqual(tt.out[i].Elem().Interface(), tt.results[i].Interface()) {
t.Errorf("out[%d]: expected %q, got %q", i, tt.results[i].Interface(), tt.out[i].Elem().Interface())
}
}
}()
if tt.expectPanic {
defer func() {
if recover() == nil {
t.Errorf("Expected a panic, got none")
}
}()
}
registry[key] = &mock{
TB: mockT,
Delegates: Delegates{
"testMethod": &Delegate{
Callables: tt.callables,
},
},
}
t.Cleanup(func() {
delete(registry, key)
})
// Call doCall
doCall(key, "testMethod", tt.in, tt.out)
})
}
}