@@ -68,48 +68,54 @@ func errorReturn() error {
68
68
func TestReturnTypeMatch (t * testing.T ) {
69
69
tests := []struct {
70
70
name string
71
- fn func () bool
72
- want bool
71
+ fn func () error
72
+ want string
73
73
}{
74
74
{
75
75
name : "int match" ,
76
- fn : func () bool {
76
+ fn : func () error {
77
77
return ReturnTypeMatch [int ](intReturn )
78
78
},
79
- want : true ,
79
+ want : "" ,
80
80
},
81
81
{
82
82
name : "string match" ,
83
- fn : func () bool {
83
+ fn : func () error {
84
84
return ReturnTypeMatch [string ](stringReturn )
85
85
},
86
- want : true ,
86
+ want : "" ,
87
87
},
88
88
{
89
89
name : "int mismatch" ,
90
- fn : func () bool {
90
+ fn : func () error {
91
91
return ReturnTypeMatch [string ](intReturn )
92
92
},
93
+ want : "function must return string, got int" ,
93
94
},
94
95
{
95
96
name : "no param" ,
96
- fn : func () bool {
97
+ fn : func () error {
97
98
return ReturnTypeMatch [any ](errorReturn )
98
99
},
99
- want : true ,
100
+ want : "" ,
100
101
},
101
102
{
102
103
name : "no param mismatch" ,
103
- fn : func () bool {
104
+ fn : func () error {
104
105
return ReturnTypeMatch [int ](errorReturn )
105
106
},
106
- want : true ,
107
+ want : "" ,
107
108
},
108
109
}
109
110
for _ , tt := range tests {
110
111
t .Run (tt .name , func (t * testing.T ) {
111
112
got := tt .fn ()
112
- require .Equal (t , tt .want , got )
113
+ if tt .want == "" {
114
+ require .NoError (t , got )
115
+ } else {
116
+ require .Error (t , got )
117
+ require .Equal (t , tt .want , got .Error ())
118
+ }
113
119
})
114
120
}
115
121
}
@@ -129,63 +135,68 @@ func mixedParams(context.Context, int, string) {
129
135
func TestParamsMatch (t * testing.T ) {
130
136
tests := []struct {
131
137
name string
132
- fn func () bool
133
- want bool
138
+ fn func () error
139
+ want string
134
140
}{
135
141
{
136
142
name : "int match" ,
137
- fn : func () bool {
143
+ fn : func () error {
138
144
return ParamsMatch (intParam , 0 , 42 )
139
145
},
140
- want : true ,
146
+ want : "" ,
141
147
},
142
148
{
143
149
name : "int mismatch" ,
144
- fn : func () bool {
150
+ fn : func () error {
145
151
return ParamsMatch (intParam , 0 , "" )
146
152
},
147
- want : false ,
153
+ want : "mismatched argument type: expected int, got string" ,
148
154
},
149
155
{
150
156
name : "string mismatch" ,
151
- fn : func () bool {
157
+ fn : func () error {
152
158
return ParamsMatch (stringParam , 0 , 42 )
153
159
},
154
- want : false ,
160
+ want : "mismatched argument type: expected string, got int" ,
155
161
},
156
162
{
157
163
name : "interface{} ignored" ,
158
- fn : func () bool {
164
+ fn : func () error {
159
165
return ParamsMatch (interfaceParam , 0 , "" , 23 , 42 )
160
166
},
161
- want : true ,
167
+ want : "" ,
162
168
},
163
169
{
164
170
name : "mixed params" ,
165
- fn : func () bool {
171
+ fn : func () error {
166
172
return ParamsMatch (mixedParams , 1 , 42 , "" )
167
173
},
168
- want : true ,
174
+ want : "" ,
169
175
},
170
176
{
171
177
name : "mixed params - no skip" ,
172
- fn : func () bool {
178
+ fn : func () error {
173
179
return ParamsMatch (mixedParams , 0 , 42 , "" )
174
180
},
175
- want : false ,
181
+ want : "mismatched argument count: expected 2, got 3" ,
176
182
},
177
183
{
178
184
name : "mixed params - wrong params" ,
179
- fn : func () bool {
185
+ fn : func () error {
180
186
return ParamsMatch (mixedParams , 1 , "" , 42 )
181
187
},
182
- want : false ,
188
+ want : "mismatched argument type: expected int, got string" ,
183
189
},
184
190
}
185
191
for _ , tt := range tests {
186
192
t .Run (tt .name , func (t * testing.T ) {
187
193
got := tt .fn ()
188
- require .Equal (t , tt .want , got )
194
+ if tt .want == "" {
195
+ require .NoError (t , got )
196
+ } else {
197
+ require .Error (t , got )
198
+ require .Equal (t , tt .want , got .Error ())
199
+ }
189
200
})
190
201
}
191
202
}
0 commit comments