nested multi line steps with table or content body argument #89

Этот коммит содержится в:
gedi 2017-06-09 11:42:28 +03:00
родитель b10470da31
коммит aac216500e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556
2 изменённых файлов: 120 добавлений и 8 удалений

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

@ -3,6 +3,7 @@ package godog
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -118,7 +119,7 @@ func TestProgressFormatterWhenStepPanics(t *testing.T) {
} }
out := buf.String() out := buf.String()
if idx := strings.Index(out, "github.com/DATA-DOG/godog/fmt_progress_test.go:116"); idx == -1 { if idx := strings.Index(out, "github.com/DATA-DOG/godog/fmt_progress_test.go:113"); idx == -1 {
t.Fatal("expected to find panic stacktrace") t.Fatal("expected to find panic stacktrace")
} }
} }
@ -261,3 +262,96 @@ func FeatureContext(s *godog.Suite) {
t.Fatalf("expected output does not match: %s", actual) t.Fatalf("expected output does not match: %s", actual)
} }
} }
func TestProgressFormatterWhenMultiStepHasArgument(t *testing.T) {
var featureSource = `
Feature: basic
Scenario: passing scenario
When one
Then two:
"""
text
"""
`
feat, err := gherkin.ParseFeature(strings.NewReader(featureSource))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
r := runner{
fmt: progressFunc("progress", ioutil.Discard),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two:$`, func(doc *gherkin.DocString) Steps { return Steps{"one"} })
},
}
if r.run() {
t.Fatal("the suite should have passed")
}
}
func TestProgressFormatterWhenMultiStepHasStepWithArgument(t *testing.T) {
var featureSource = `
Feature: basic
Scenario: passing scenario
When one
Then two`
feat, err := gherkin.ParseFeature(strings.NewReader(featureSource))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
var subStep = `three:
"""
content
"""`
var buf bytes.Buffer
w := colors.Uncolored(&buf)
r := runner{
fmt: progressFunc("progress", w),
features: []*feature{&feature{Feature: feat}},
initializer: func(s *Suite) {
s.Step(`^one$`, func() error { return nil })
s.Step(`^two$`, func() Steps { return Steps{subStep} })
s.Step(`^three:$`, func(doc *gherkin.DocString) error { return nil })
},
}
if !r.run() {
t.Fatal("the suite should have failed")
}
expected := `
.F 2
--- Failed steps:
Then two # :6
Error: nested steps cannot be multiline and have table or content body argument
1 scenarios (1 failed)
2 steps (1 passed, 1 failed)
%s
Randomized with seed: %s
`
var zeroDuration time.Duration
expected = fmt.Sprintf(expected, zeroDuration.String(), os.Getenv("GODOG_SEED"))
expected = trimAllLines(expected)
actual := trimAllLines(buf.String())
if actual != expected {
t.Fatalf("expected output does not match: %s", actual)
}
}

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

@ -244,7 +244,9 @@ func (s *Suite) runStep(step *gherkin.Step, prevStepErr error) (err error) {
} }
}() }()
if undef := s.maybeUndefined(step.Text); len(undef) > 0 { if undef, err := s.maybeUndefined(step.Text, step.Argument); err != nil {
return err
} else if len(undef) > 0 {
if match != nil { if match != nil {
match = &StepDef{ match = &StepDef{
args: match.args, args: match.args,
@ -273,21 +275,37 @@ func (s *Suite) runStep(step *gherkin.Step, prevStepErr error) (err error) {
return return
} }
func (s *Suite) maybeUndefined(text string) (undefined []string) { func (s *Suite) maybeUndefined(text string, arg interface{}) ([]string, error) {
step := s.matchStepText(text) step := s.matchStepText(text)
if nil == step { if nil == step {
undefined = append(undefined, text) return []string{text}, nil
return
} }
var undefined []string
if !step.nested { if !step.nested {
return return undefined, nil
}
if arg != nil {
step.args = append(step.args, arg)
} }
for _, next := range step.run().(Steps) { for _, next := range step.run().(Steps) {
undefined = append(undefined, s.maybeUndefined(next)...) lines := strings.Split(next, "\n")
// @TODO: we cannot currently parse table or content body from nested steps
if len(lines) > 1 {
return undefined, fmt.Errorf("nested steps cannot be multiline and have table or content body argument")
}
if len(lines[0]) > 0 && lines[0][len(lines[0])-1] == ':' {
return undefined, fmt.Errorf("nested steps cannot be multiline and have table or content body argument")
}
undef, err := s.maybeUndefined(next, nil)
if err != nil {
return undefined, err
}
undefined = append(undefined, undef...)
} }
return return undefined, nil
} }
func (s *Suite) maybeSubSteps(result interface{}) error { func (s *Suite) maybeSubSteps(result interface{}) error {