You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix some type checks on the signatures of nested step handlers (#647)
* at some point someone changed the return type for nested steps from []string to godog.Steps but they forgot to adjust the type checks. The existing type checks were lax and unable to distinguish []string from godog.Steps but in a couple of places in the code the value is coerced to godog.Steps and so if someone returned []string then the code would blow up. Additionally there were some tests aroudn these types but they also had not been updated but the test was passing for the wrong reason - the particular test expected an error but the cause of the error wasn't the one the code expected.
* CHANGELOG.md
* use chatgpt to regen the top of the code based on the new tests
* use chatgpt to regen the top of the code based on the new tests
* corrected the error messages of the param checks to indicate that the problem is the function signature and not the args being passed to the function, also added numerous extra assertions on the precise error messages returned. Now that the precise error is being verified in the test I have improved certain error messages to that more accurate detail is included in the errors
* added further constraints to the step arg mapping tests
* removed redundant test
* include a step error result in the reported error even when the ctx is nil
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
8
8
9
9
## Unreleased
10
10
11
+
- Improved the type checking of step return types and improved the error messages - ([647](https://github.com/cucumber/godog/pull/647) - [johnlon](https://github.com/johnlon))
11
12
- Ambiguous step definitions will now be detected when strict mode is activated - ([636](https://github.com/cucumber/godog/pull/636) - [johnlon](https://github.com/johnlon))
12
13
- Provide support for attachments / embeddings including a new example in the examples dir - ([623](https://github.com/cucumber/godog/pull/623) - [johnlon](https://github.com/johnlon))
// Note that the step fn return types were validated at Initialise in test_context.go stepWithKeyword()
195
+
196
+
// single return value may be one of ...
197
+
// error
198
+
// context.Context
199
+
// godog.Steps
200
+
result0:=res[0].Interface()
187
201
iflen(res) ==1 {
188
-
r:=res[0].Interface()
189
202
190
-
ifctx, ok:=r.(context.Context); ok {
203
+
// if the single return value is a context then just return it
204
+
ifctx, ok:=result0.(context.Context); ok {
191
205
returnctx, nil
192
206
}
193
207
194
-
returnctx, res[0].Interface()
208
+
// return type is presumably one of nil, "error" or "Steps" so place it into second return position
209
+
returnctx, result0
210
+
}
211
+
212
+
// multi-value value return must be
213
+
// (context, error) and the context value must not be nil
214
+
ifctx, ok:=result0.(context.Context); ok {
215
+
returnctx, res[1].Interface()
216
+
}
217
+
218
+
result1:=res[1].Interface()
219
+
errMsg:=""
220
+
ifresult1!=nil {
221
+
errMsg=fmt.Sprintf(", step def also returned an error: %v", result1)
222
+
}
223
+
224
+
text:=sd.StepDefinition.Expr.String()
225
+
226
+
ifresult0==nil {
227
+
panic(fmt.Sprintf("step definition '%v' with return type (context.Context, error) must not return <nil> for the context.Context value%s", text, errMsg))
panic(fmt.Errorf("step definition '%v' has return type (context.Context, error), but found %v rather than a context.Context value%s", text, result0, errMsg))
0 commit comments