Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support auto converting doc strings to plain strings #380

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions features/run.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

11 changes: 8 additions & 3 deletions internal/models/stepdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
24 changes: 24 additions & 0 deletions internal/models/stepdef_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models_test

import (
"fmt"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
}
6 changes: 3 additions & 3 deletions run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`

Expand Down
7 changes: 7 additions & 0 deletions suite_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down