From 4a4fd8ab3a3024fa1813083c322b289a56331e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tighearn=C3=A1n=20Carroll?= Date: Wed, 19 Mar 2025 23:04:06 +0000 Subject: [PATCH] fix(errors): Fix expected Step argument count for steps with `context.Context` (#679) * fix(errors): print correct number of expected steps when step has defined * add unit test * remove duplicate part from unit test * update changelog --- CHANGELOG.md | 1 + internal/models/stepdef.go | 2 +- internal/models/stepdef_test.go | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6be6f8d..2330ede 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt - Step text is added to "step is undefined" error - ([669](https://github.com/cucumber/godog/pull/669) - [vearutop](https://github.com/vearutop)) ### Fixed +- fix(errors): fix(errors): Fix expected Step argument count for steps with `context.Context` ([679](https://github.com/cucumber/godog/pull/679) - [tigh-latte](https://github.com/tigh-latte)) - fix(formatter): On concurrent execution, execute formatter at end of Scenario - ([645](https://github.com/cucumber/godog/pull/645) - [tigh-latte](https://github.com/tigh-latte)) ## [v0.15.0] diff --git a/internal/models/stepdef.go b/internal/models/stepdef.go index 90ffef5..eaf73e7 100644 --- a/internal/models/stepdef.go +++ b/internal/models/stepdef.go @@ -54,7 +54,7 @@ func (sd *StepDefinition) Run(ctx context.Context) (context.Context, interface{} } if len(sd.Args) < numIn { - return ctx, fmt.Errorf("%w: expected %d arguments, matched %d from step", ErrUnmatchedStepArgumentNumber, typ.NumIn(), len(sd.Args)) + return ctx, fmt.Errorf("%w: expected %d arguments, matched %d from step", ErrUnmatchedStepArgumentNumber, numIn, len(sd.Args)) } for i := 0; i < numIn; i++ { diff --git a/internal/models/stepdef_test.go b/internal/models/stepdef_test.go index 318f375..e66c533 100644 --- a/internal/models/stepdef_test.go +++ b/internal/models/stepdef_test.go @@ -259,6 +259,26 @@ func TestArgumentCountChecks(t *testing.T) { assert.Nil(t, err) } +func TestArgumentCountChecksWithContext(t *testing.T) { + wasCalled := false + fn := func(ctx context.Context, a int, b int) { + wasCalled = true + } + + def := &models.StepDefinition{ + StepDefinition: formatters.StepDefinition{ + Handler: fn, + }, + HandlerValue: reflect.ValueOf(fn), + } + + def.Args = []interface{}{"1"} + _, err := def.Run(context.Background()) + assert.False(t, wasCalled) + assert.Equal(t, `func expected more arguments than given: expected 2 arguments, matched 1 from step`, err.(error).Error()) + assert.True(t, errors.Is(err.(error), models.ErrUnmatchedStepArgumentNumber)) +} + func TestShouldSupportIntTypes(t *testing.T) { var aActual int64 var bActual int32