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

Added the new TestSuiteInitializer and ScenarioInitializer #294

Merged
merged 1 commit into from
May 22, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ import (
"fmt"

"github.com/cucumber/godog"
messages "github.com/cucumber/messages-go/v10"
)

func thereAreGodogs(available int) error {
Expand All @@ -183,12 +182,12 @@ func thereShouldBeRemaining(remaining int) error {
return nil
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
s.Step(`^I eat (\d+)$`, iEat)
s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)

s.BeforeScenario(func(*messages.Pickle) {
s.BeforeScenario(func(*godog.Scenario) {
Godogs = 0 // clean the state before every scenario
})
}
Expand Down Expand Up @@ -250,22 +249,24 @@ The following example binds **godog** flags with specified prefix `godog`
in order to prevent flag collisions.

``` go
var opt = godog.Options{
var opts = godog.Options{
Output: colors.Colored(os.Stdout),
Format: "progress", // can define default values
}

func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
godog.BindFlags("godog.", flag.CommandLine, &opts)
}

func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()
opts.Paths = flag.Args()

status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: ScenarioContext,
Options: &opts,
}.Run()

if st := m.Run(); st > status {
status = st
Expand All @@ -287,13 +288,17 @@ configuring needed options.

``` go
func TestMain(m *testing.M) {
status := godog.RunWithOptions("godog", func(s *godog.Suite) {
FeatureContext(s)
}, godog.Options{
opts := godog.Options{
Format: "progress",
Paths: []string{"features"},
Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order
})
}

status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: ScenarioContext,
Options: opts,
}.Run()

if st := m.Run(); st > status {
status = st
Expand All @@ -315,12 +320,17 @@ func TestMain(m *testing.M) {
break
}
}
status := godog.RunWithOptions("godog", func(s *godog.Suite) {
godog.SuiteContext(s)
}, godog.Options{

opts := godog.Options{
Format: format,
Paths: []string{"features"},
})
}

status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: ScenarioContext,
Options: opts,
}.Run()

if st := m.Run(); st > status {
status = st
Expand Down
10 changes: 4 additions & 6 deletions _examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ need to store state within steps (a response), we should introduce a structure w
package main

import (
"github.com/cucumber/gherkin-go/v11"
"github.com/cucumber/godog"
)

Expand All @@ -71,11 +70,11 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return godog.ErrPending
}

func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgument_PickleDocString) error {
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) error {
return godog.ErrPending
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
api := &apiFeature{}
s.Step(`^I send "([^"]*)" request to "([^"]*)"$`, api.iSendrequestTo)
s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe)
Expand All @@ -98,7 +97,6 @@ import (
"net/http"
"net/http/httptest"

"github.com/cucumber/gherkin-go/v11"
"github.com/cucumber/godog"
)

Expand Down Expand Up @@ -142,7 +140,7 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return nil
}

func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgument_PickleDocString) error {
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) error {
var expected, actual []byte
var data interface{}
if err = json.Unmarshal([]byte(body.Content), &data); err != nil {
Expand All @@ -158,7 +156,7 @@ func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgumen
return
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
api := &apiFeature{}

s.BeforeScenario(api.resetResponse)
Expand Down
7 changes: 3 additions & 4 deletions _examples/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"reflect"

"github.com/cucumber/godog"
"github.com/cucumber/messages-go/v10"
)

type apiFeature struct {
resp *httptest.ResponseRecorder
}

func (a *apiFeature) resetResponse(*messages.Pickle) {
func (a *apiFeature) resetResponse(*godog.Scenario) {
a.resp = httptest.NewRecorder()
}

Expand Down Expand Up @@ -51,7 +50,7 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return nil
}

func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgument_PickleDocString) (err error) {
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err error) {
var expected, actual interface{}

// re-encode expected response
Expand All @@ -71,7 +70,7 @@ func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgumen
return nil
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
api := &apiFeature{}

s.BeforeScenario(api.resetResponse)
Expand Down
4 changes: 2 additions & 2 deletions _examples/assert-godogs/features/godogs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Feature: eat godogs

Scenario: Eat 5 out of 12
Given there are 12 godogs
When I eat 4
When I eat 5
Then there should be 7 remaining

Scenario: Eat 12 out of 12
Given there are 12 godogs
When I eat 11
When I eat 12
Then there should be none remaining
20 changes: 10 additions & 10 deletions _examples/assert-godogs/godogs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* file: $GOPATH/src/assert-godogs/godogs_test.go */
package main

import (
Expand All @@ -9,23 +8,24 @@ import (

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
messages "github.com/cucumber/messages-go/v10"
"github.com/stretchr/testify/assert"
)

var opt = godog.Options{Output: colors.Colored(os.Stdout)}
var opts = godog.Options{Output: colors.Colored(os.Stdout)}

func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
godog.BindFlags("godog.", flag.CommandLine, &opts)
}

func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()
opts.Paths = flag.Args()

status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: ScenarioContext,
Options: &opts,
}.Run()

if st := m.Run(); st > status {
status = st
Expand Down Expand Up @@ -65,13 +65,13 @@ func thereShouldBeNoneRemaining() error {
)
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
s.Step(`^I eat (\d+)$`, iEat)
s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)
s.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining)

s.BeforeScenario(func(*messages.Pickle) {
s.BeforeScenario(func(*godog.Scenario) {
Godogs = 0 // clean the state before every scenario
})
}
Expand Down
9 changes: 4 additions & 5 deletions _examples/db/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

txdb "github.com/DATA-DOG/go-txdb"
"github.com/cucumber/godog"
"github.com/cucumber/messages-go/v10"
)

func init() {
Expand All @@ -24,7 +23,7 @@ type apiFeature struct {
resp *httptest.ResponseRecorder
}

func (a *apiFeature) resetResponse(*messages.Pickle) {
func (a *apiFeature) resetResponse(*godog.Scenario) {
a.resp = httptest.NewRecorder()
if a.db != nil {
a.db.Close()
Expand Down Expand Up @@ -71,7 +70,7 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
return nil
}

func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgument_PickleDocString) (err error) {
func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err error) {
var expected, actual interface{}

// re-encode expected response
Expand All @@ -91,7 +90,7 @@ func (a *apiFeature) theResponseShouldMatchJSON(body *messages.PickleStepArgumen
return nil
}

func (a *apiFeature) thereAreUsers(users *messages.PickleStepArgument_PickleTable) error {
func (a *apiFeature) thereAreUsers(users *godog.Table) error {
var fields []string
var marks []string
head := users.Rows[0].Cells
Expand Down Expand Up @@ -123,7 +122,7 @@ func (a *apiFeature) thereAreUsers(users *messages.PickleStepArgument_PickleTabl
return nil
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
api := &apiFeature{}

s.BeforeScenario(api.resetResponse)
Expand Down
20 changes: 10 additions & 10 deletions _examples/godogs/godogs_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* file: $GOPATH/src/godogs/godogs_test.go */
package main

import (
Expand All @@ -9,22 +8,23 @@ import (

"github.com/cucumber/godog"
"github.com/cucumber/godog/colors"
messages "github.com/cucumber/messages-go/v10"
)

var opt = godog.Options{Output: colors.Colored(os.Stdout)}
var opts = godog.Options{Output: colors.Colored(os.Stdout)}

func init() {
godog.BindFlags("godog.", flag.CommandLine, &opt)
godog.BindFlags("godog.", flag.CommandLine, &opts)
}

func TestMain(m *testing.M) {
flag.Parse()
opt.Paths = flag.Args()
opts.Paths = flag.Args()

status := godog.RunWithOptions("godogs", func(s *godog.Suite) {
FeatureContext(s)
}, opt)
status := godog.TestSuite{
Name: "godogs",
ScenarioInitializer: ScenarioContext,
Options: &opts,
}.Run()

if st := m.Run(); st > status {
status = st
Expand Down Expand Up @@ -52,12 +52,12 @@ func thereShouldBeRemaining(remaining int) error {
return nil
}

func FeatureContext(s *godog.Suite) {
func ScenarioContext(s *godog.ScenarioContext) {
s.Step(`^there are (\d+) godogs$`, thereAreGodogs)
s.Step(`^I eat (\d+)$`, iEat)
s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining)

s.BeforeScenario(func(*messages.Pickle) {
s.BeforeScenario(func(*godog.Scenario) {
Godogs = 0 // clean the state before every scenario
})
}
6 changes: 3 additions & 3 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package godog

import "go/ast"

func astContexts(f *ast.File) []string {
func astContexts(f *ast.File, selectName string) []string {
var contexts []string
for _, d := range f.Decls {
switch fun := d.(type) {
Expand All @@ -12,13 +12,13 @@ func astContexts(f *ast.File) []string {
case *ast.StarExpr:
switch x := expr.X.(type) {
case *ast.Ident:
if x.Name == "Suite" {
if x.Name == selectName {
contexts = append(contexts, fun.Name.Name)
}
case *ast.SelectorExpr:
switch t := x.X.(type) {
case *ast.Ident:
if t.Name == "godog" && x.Sel.Name == "Suite" {
if t.Name == "godog" && x.Sel.Name == selectName {
contexts = append(contexts, fun.Name.Name)
}
}
Expand Down
2 changes: 1 addition & 1 deletion ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func astContextParse(src string, t *testing.T) []string {
t.Fatalf("unexpected error while parsing ast: %v", err)
}

return astContexts(f)
return astContexts(f, "Suite")
}

func TestShouldGetSingleContextFromSource(t *testing.T) {
Expand Down
Loading