feat: support auto converting doc strings to plain strings
Этот коммит содержится в:
родитель
3bdc35e28e
коммит
2b6c9dc82e
5 изменённых файлов: 55 добавлений и 6 удалений
|
@ -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
|
||||
"""
|
||||
|
||||
|
|
|
@ -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 ...
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
`
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче