@@ -68,48 +68,135 @@ 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
+ }
119
+ })
120
+ }
121
+ }
122
+
123
+ func intParam (int ) {
124
+ }
125
+
126
+ func stringParam (string ) {
127
+ }
128
+
129
+ func interfaceParam (string , interface {}, int ) {
130
+ }
131
+
132
+ func mixedParams (context.Context , int , string ) {
133
+ }
134
+
135
+ func TestParamsMatch (t * testing.T ) {
136
+ tests := []struct {
137
+ name string
138
+ fn func () error
139
+ want string
140
+ }{
141
+ {
142
+ name : "int match" ,
143
+ fn : func () error {
144
+ return ParamsMatch (intParam , 0 , 42 )
145
+ },
146
+ want : "" ,
147
+ },
148
+ {
149
+ name : "int mismatch" ,
150
+ fn : func () error {
151
+ return ParamsMatch (intParam , 0 , "" )
152
+ },
153
+ want : "mismatched argument type: expected int, got string" ,
154
+ },
155
+ {
156
+ name : "string mismatch" ,
157
+ fn : func () error {
158
+ return ParamsMatch (stringParam , 0 , 42 )
159
+ },
160
+ want : "mismatched argument type: expected string, got int" ,
161
+ },
162
+ {
163
+ name : "interface{} ignored" ,
164
+ fn : func () error {
165
+ return ParamsMatch (interfaceParam , 0 , "" , 23 , 42 )
166
+ },
167
+ want : "" ,
168
+ },
169
+ {
170
+ name : "mixed params" ,
171
+ fn : func () error {
172
+ return ParamsMatch (mixedParams , 1 , 42 , "" )
173
+ },
174
+ want : "" ,
175
+ },
176
+ {
177
+ name : "mixed params - no skip" ,
178
+ fn : func () error {
179
+ return ParamsMatch (mixedParams , 0 , 42 , "" )
180
+ },
181
+ want : "mismatched argument count: expected 3, got 2" ,
182
+ },
183
+ {
184
+ name : "mixed params - wrong params" ,
185
+ fn : func () error {
186
+ return ParamsMatch (mixedParams , 1 , "" , 42 )
187
+ },
188
+ want : "mismatched argument type: expected int, got string" ,
189
+ },
190
+ }
191
+ for _ , tt := range tests {
192
+ t .Run (tt .name , func (t * testing.T ) {
193
+ got := tt .fn ()
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
+ }
113
200
})
114
201
}
115
202
}
0 commit comments