test undefined step snippet templates
Этот коммит содержится в:
родитель
42158ff04b
коммит
56998cc5cf
5 изменённых файлов: 112 добавлений и 16 удалений
|
@ -8,7 +8,7 @@ Savybė: užkrauti savybes
|
|||
Scenarijus: savybių užkrovimas iš aplanko
|
||||
Duota savybių aplankas "features"
|
||||
Kai aš išskaitau savybes
|
||||
Tada aš turėčiau turėti 6 savybių failus:
|
||||
Tada aš turėčiau turėti 7 savybių failus:
|
||||
"""
|
||||
features/background.feature
|
||||
features/events.feature
|
||||
|
@ -16,4 +16,5 @@ Savybė: užkrauti savybes
|
|||
features/load.feature
|
||||
features/outline.feature
|
||||
features/run.feature
|
||||
features/snippets.feature
|
||||
"""
|
||||
|
|
|
@ -6,7 +6,7 @@ Feature: load features
|
|||
Scenario: load features within path
|
||||
Given a feature path "features"
|
||||
When I parse features
|
||||
Then I should have 6 feature files:
|
||||
Then I should have 7 feature files:
|
||||
"""
|
||||
features/background.feature
|
||||
features/events.feature
|
||||
|
@ -14,6 +14,7 @@ Feature: load features
|
|||
features/load.feature
|
||||
features/outline.feature
|
||||
features/run.feature
|
||||
features/snippets.feature
|
||||
"""
|
||||
|
||||
Scenario: load a specific feature file
|
||||
|
|
64
features/snippets.feature
Обычный файл
64
features/snippets.feature
Обычный файл
|
@ -0,0 +1,64 @@
|
|||
Feature: undefined step snippets
|
||||
In order to implement step definitions faster
|
||||
As a test suite user
|
||||
I need to be able to get undefined step snippets
|
||||
|
||||
Scenario: should generate snippets
|
||||
Given a feature "undefined.feature" file:
|
||||
"""
|
||||
Feature: undefined steps
|
||||
|
||||
Scenario: get version number from api
|
||||
When I send "GET" request to "/version"
|
||||
Then the response code should be 200
|
||||
"""
|
||||
When I run feature suite
|
||||
Then the following steps should be undefined:
|
||||
"""
|
||||
I send "GET" request to "/version"
|
||||
the response code should be 200
|
||||
"""
|
||||
And the undefined step snippets should be:
|
||||
"""
|
||||
func iSendrequestTo(arg1, arg2 string) error {
|
||||
return godog.ErrPending
|
||||
}
|
||||
|
||||
func theResponseCodeShouldBe(arg1 int) error {
|
||||
return godog.ErrPending
|
||||
}
|
||||
|
||||
func featureContext(s godog.Suite) {
|
||||
s.Step(`^I send "([^"]*)" request to "([^"]*)"$`, iSendrequestTo)
|
||||
s.Step(`^the response code should be (\d+)$`, theResponseCodeShouldBe)
|
||||
}
|
||||
"""
|
||||
|
||||
Scenario: should generate snippets with more arguments
|
||||
Given a feature "undefined.feature" file:
|
||||
"""
|
||||
Feature: undefined steps
|
||||
|
||||
Scenario: get version number from api
|
||||
When I send "GET" request to "/version" with:
|
||||
| col1 | val1 |
|
||||
| col2 | val2 |
|
||||
Then the response code should be 200 and header "X-Powered-By" should be "godog"
|
||||
"""
|
||||
When I run feature suite
|
||||
Then the undefined step snippets should be:
|
||||
"""
|
||||
func iSendrequestTowith(arg1, arg2 string, arg3 *gherkin.DataTable) error {
|
||||
return godog.ErrPending
|
||||
}
|
||||
|
||||
func theResponseCodeShouldBeAndHeadershouldBe(arg1 int, arg2, arg3 string) error {
|
||||
return godog.ErrPending
|
||||
}
|
||||
|
||||
func featureContext(s godog.Suite) {
|
||||
s.Step(`^I send "([^"]*)" request to "([^"]*)" with:$`, iSendrequestTowith)
|
||||
s.Step(`^the response code should be (\d+) and header "([^"]*)" should be "([^"]*)"$`, theResponseCodeShouldBeAndHeadershouldBe)
|
||||
}
|
||||
"""
|
||||
|
40
fmt.go
40
fmt.go
|
@ -3,6 +3,7 @@ package godog
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
@ -261,10 +262,13 @@ func (f *basefmt) Summary() {
|
|||
}
|
||||
fmt.Println(elapsed)
|
||||
|
||||
f.snippets()
|
||||
if text := f.snippets(); text != "" {
|
||||
fmt.Println(cl("\nYou can implement step definitions for undefined steps with these snippets:", yellow))
|
||||
fmt.Println(cl(text, yellow))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *undefinedSnippet) Args() string {
|
||||
func (s *undefinedSnippet) Args() (ret string) {
|
||||
var args []string
|
||||
var pos, idx int
|
||||
var breakLoop bool
|
||||
|
@ -278,40 +282,48 @@ func (s *undefinedSnippet) Args() string {
|
|||
case spos == -1:
|
||||
idx++
|
||||
pos += ipos + len("(\\d+)")
|
||||
args = append(args, fmt.Sprintf("arg%d int", idx))
|
||||
args = append(args, reflect.Int.String())
|
||||
case ipos == -1:
|
||||
idx++
|
||||
pos += spos + len("\"([^\"]*)\"")
|
||||
args = append(args, fmt.Sprintf("arg%d string", idx))
|
||||
args = append(args, reflect.String.String())
|
||||
case ipos < spos:
|
||||
idx++
|
||||
pos += ipos + len("(\\d+)")
|
||||
args = append(args, fmt.Sprintf("arg%d int", idx))
|
||||
args = append(args, reflect.Int.String())
|
||||
case spos < ipos:
|
||||
idx++
|
||||
pos += spos + len("\"([^\"]*)\"")
|
||||
args = append(args, fmt.Sprintf("arg%d string", idx))
|
||||
args = append(args, reflect.String.String())
|
||||
}
|
||||
}
|
||||
if s.argument != nil {
|
||||
idx++
|
||||
switch s.argument.(type) {
|
||||
case *gherkin.DocString:
|
||||
args = append(args, fmt.Sprintf("arg%d *gherkin.DocString", idx))
|
||||
args = append(args, "*gherkin.DocString")
|
||||
case *gherkin.DataTable:
|
||||
args = append(args, fmt.Sprintf("arg%d *gherkin.DataTable", idx))
|
||||
args = append(args, "*gherkin.DataTable")
|
||||
}
|
||||
}
|
||||
return strings.Join(args, ", ")
|
||||
|
||||
var last string
|
||||
for i, arg := range args {
|
||||
if last == "" || last == arg {
|
||||
ret += fmt.Sprintf("arg%d, ", i+1)
|
||||
} else {
|
||||
ret = strings.TrimRight(ret, ", ") + fmt.Sprintf(" %s, arg%d, ", last, i+1)
|
||||
}
|
||||
last = arg
|
||||
}
|
||||
return strings.TrimRight(ret, ", ") + " " + last
|
||||
}
|
||||
|
||||
func (f *basefmt) snippets() {
|
||||
func (f *basefmt) snippets() string {
|
||||
if len(f.undefined) == 0 {
|
||||
return
|
||||
return ""
|
||||
}
|
||||
|
||||
fmt.Println(cl("\nYou can implement step definitions for undefined steps with these snippets:", yellow))
|
||||
|
||||
var index int
|
||||
var snips []*undefinedSnippet
|
||||
// build snippets
|
||||
|
@ -355,5 +367,5 @@ func (f *basefmt) snippets() {
|
|||
if err := undefinedSnippetsTpl.Execute(&buf, snips); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println(cl(buf.String(), yellow))
|
||||
return buf.String()
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@ func SuiteContext(s Suite) {
|
|||
s.Step(`^a failing step`, c.aFailingStep)
|
||||
s.Step(`^this step should fail`, c.aFailingStep)
|
||||
s.Step(`^the following steps? should be (passed|failed|skipped|undefined|pending):`, c.followingStepsShouldHave)
|
||||
s.Step(`^the undefined step snippets should be:$`, c.theUndefinedStepSnippetsShouldBe)
|
||||
|
||||
// lt
|
||||
s.Step(`^savybių aplankas "([^"]*)"$`, c.featurePath)
|
||||
|
@ -64,6 +65,23 @@ func (s *suiteContext) ResetBeforeEachScenario(interface{}) {
|
|||
s.events = []*firedEvent{}
|
||||
}
|
||||
|
||||
func (s *suiteContext) cleanupSnippet(snip string) string {
|
||||
lines := strings.Split(strings.TrimSpace(snip), "\n")
|
||||
for i := 0; i < len(lines); i++ {
|
||||
lines[i] = strings.TrimSpace(lines[i])
|
||||
}
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
func (s *suiteContext) theUndefinedStepSnippetsShouldBe(body *gherkin.DocString) error {
|
||||
actual := s.cleanupSnippet(s.fmt.snippets())
|
||||
expected := s.cleanupSnippet(body.Content)
|
||||
if actual != expected {
|
||||
return fmt.Errorf("snippets do not match actual: %s", s.fmt.snippets())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *suiteContext) followingStepsShouldHave(status string, steps *gherkin.DocString) error {
|
||||
var expected = strings.Split(steps.Content, "\n")
|
||||
var actual, unmatched, matched []string
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче