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
While investigating data for the poll #542, it became clear that godog (as the de-facto BDD library for go) seems to be the only one that splits the step regex and the step function. It seems that every other language has a nice way of adding metadata to functions and this metadata then can contains the regex.
Why not do it in go?
We can't add metadata to functions like other languages but we can wrap functions in other functions that can then add that metadata. We could use "magic comments", but let's not even start on that…
I'm talking about this:
funciEat(ctx context.Context, numint) (context.Context, error) {
// … get the available value from context etc.available-=numreturncontext.WithValue(ctx, godogsCtxKey{}, available), nil
}
// … many lines betweenfuncInitializeScenario(ctx*godog.ScenarioContext) {
ctx.Step(`^I eat (\d+)$`, iEat)
}
I'm sure there were good design decisions made at the time that caused this split. But can we do better now?
Would it make maintenance and discoverability easier and probably some other software quality metrics much better too? In fact, I have already done it as part of PoC to see if/how this would be possible. It's not difficult, but there is some boilerplate which (in the ideal world…) maybe could make its way into godog directly.
I'd love to discuss this approach and "if it has legs" then perhaps start looking into if it could become the default pattern for godog. This seems to be like a long shot and quite ambitious goal, but why not?
As you can see, not only the regex is right next to the func implementation, but this approach also allows me to remove a lot of code complexity and boilerplate that does getting/setting context values. All with roughly the same amount of lines of code.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
While investigating data for the poll #542, it became clear that
godog
(as the de-facto BDD library forgo
) seems to be the only one that splits the step regex and the step function. It seems that every other language has a nice way of adding metadata to functions and this metadata then can contains the regex.Why not do it in
go
?We can't add metadata to functions like other languages but we can wrap functions in other functions that can then add that metadata. We could use "magic comments", but let's not even start on that…
I'm talking about this:
I'm sure there were good design decisions made at the time that caused this split. But can we do better now?
Would it make maintenance and discoverability easier and probably some other software quality metrics much better too? In fact, I have already done it as part of PoC to see if/how this would be possible. It's not difficult, but there is some boilerplate which (in the ideal world…) maybe could make its way into godog directly.
I'd love to discuss this approach and "if it has legs" then perhaps start looking into if it could become the default pattern for
godog
. This seems to be like a long shot and quite ambitious goal, but why not?My example step definition now looks like this:
As you can see, not only the regex is right next to the func implementation, but this approach also allows me to remove a lot of code complexity and boilerplate that does getting/setting context values. All with roughly the same amount of lines of code.
WDYT?
Beta Was this translation helpful? Give feedback.
All reactions