diff --git a/features/run.feature b/features/run.feature index 9046d5a..528cd8a 100644 --- a/features/run.feature +++ b/features/run.feature @@ -262,3 +262,16 @@ Feature: run features another undefined step """ And the suite should have failed + + Scenario: should be able to convert a Doc String to a `*godog.DocString` argument + Given call func(*godog.DocString) with: + """ + text + """ + + Scenario: should be able to convert a Doc String to a `string` argument + Given call func(string) with: + """ + text + """ + diff --git a/internal/models/stepdef.go b/internal/models/stepdef.go index be23bf5..0f4a777 100644 --- a/internal/models/stepdef.go +++ b/internal/models/stepdef.go @@ -161,11 +161,16 @@ func (sd *StepDefinition) Run() interface{} { func (sd *StepDefinition) shouldBeString(idx int) (string, error) { arg := sd.Args[idx] - s, ok := arg.(string) - if !ok { + switch arg := arg.(type) { + case string: + return arg, nil + case *messages.PickleStepArgument: + return arg.GetDocString().Content, nil + case *messages.PickleStepArgument_PickleDocString: + return arg.Content, nil + default: return "", fmt.Errorf(`cannot convert argument %d: "%v" of type "%T" to string`, idx, arg, arg) } - return s, nil } // GetInternalStepDefinition ... diff --git a/internal/models/stepdef_test.go b/internal/models/stepdef_test.go index 1ff4899..5feca21 100644 --- a/internal/models/stepdef_test.go +++ b/internal/models/stepdef_test.go @@ -1,6 +1,7 @@ package models_test import ( + "fmt" "reflect" "strings" "testing" @@ -149,3 +150,26 @@ func TestUnexpectedArguments(t *testing.T) { // t.Fatalf("expected an error due to wrong argument type, but got none") // } } + +func TestShouldSupportDocStringToStringConversion(t *testing.T) { + fn := func(a string) error { + if a != "hello" { + return fmt.Errorf("did not get hello") + } + return nil + } + + def := &models.StepDefinition{ + StepDefinition: formatters.StepDefinition{ + Handler: fn, + }, + HandlerValue: reflect.ValueOf(fn), + Args: []interface{}{&messages.PickleStepArgument_PickleDocString{ + Content: "hello", + }}, + } + + if err := def.Run(); err != nil { + t.Fatalf("unexpected error: %v", err) + } +} diff --git a/run_test.go b/run_test.go index 30246ef..cc1015e 100644 --- a/run_test.go +++ b/run_test.go @@ -413,11 +413,11 @@ func Test_AllFeaturesRun(t *testing.T) { ...................................................................... 140 ...................................................................... 210 ...................................................................... 280 -.......................... 306 +............................ 308 -79 scenarios (79 passed) -306 steps (306 passed) +81 scenarios (81 passed) +308 steps (308 passed) 0s ` diff --git a/suite_context_test.go b/suite_context_test.go index eacad60..fb22157 100644 --- a/suite_context_test.go +++ b/suite_context_test.go @@ -101,6 +101,13 @@ func InitializeScenario(ctx *ScenarioContext) { return nil }) + ctx.Step(`^call func\(\*godog\.DocString\) with:$`, func(arg *DocString) error { + return nil + }) + ctx.Step(`^call func\(string\) with:$`, func(arg string) error { + return nil + }) + ctx.BeforeStep(tc.inject) }