feat: support auto converting doc strings to plain strings

Этот коммит содержится в:
Hiram Chirino 2021-03-09 13:33:18 -05:00 коммит произвёл Fredrik Lönnblad
родитель 3bdc35e28e
коммит 2b6c9dc82e
5 изменённых файлов: 55 добавлений и 6 удалений

Просмотреть файл

@ -262,3 +262,16 @@ Feature: run features
another undefined step another undefined step
""" """
And the suite should have failed 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
"""

Просмотреть файл

@ -161,11 +161,16 @@ func (sd *StepDefinition) Run() interface{} {
func (sd *StepDefinition) shouldBeString(idx int) (string, error) { func (sd *StepDefinition) shouldBeString(idx int) (string, error) {
arg := sd.Args[idx] arg := sd.Args[idx]
s, ok := arg.(string) switch arg := arg.(type) {
if !ok { 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 "", fmt.Errorf(`cannot convert argument %d: "%v" of type "%T" to string`, idx, arg, arg)
} }
return s, nil
} }
// GetInternalStepDefinition ... // GetInternalStepDefinition ...

Просмотреть файл

@ -1,6 +1,7 @@
package models_test package models_test
import ( import (
"fmt"
"reflect" "reflect"
"strings" "strings"
"testing" "testing"
@ -149,3 +150,26 @@ func TestUnexpectedArguments(t *testing.T) {
// t.Fatalf("expected an error due to wrong argument type, but got none") // 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)
}
}

Просмотреть файл

@ -413,11 +413,11 @@ func Test_AllFeaturesRun(t *testing.T) {
...................................................................... 140 ...................................................................... 140
...................................................................... 210 ...................................................................... 210
...................................................................... 280 ...................................................................... 280
.......................... 306 ............................ 308
79 scenarios (79 passed) 81 scenarios (81 passed)
306 steps (306 passed) 308 steps (308 passed)
0s 0s
` `

Просмотреть файл

@ -101,6 +101,13 @@ func InitializeScenario(ctx *ScenarioContext) {
return nil 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) ctx.BeforeStep(tc.inject)
} }