From 13acd2d219165507ecb8867d765ea901d88666f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6nnblad?= Date: Sat, 23 May 2020 08:36:55 +0200 Subject: [PATCH] Readded some legacy doc for FeatureContext --- README.md | 57 ++++++++++++++++++++++---- _examples/api/README.md | 4 +- _examples/api/api_test.go | 10 ++--- _examples/assert-godogs/godogs_test.go | 16 ++++---- _examples/db/api_test.go | 12 +++--- _examples/godogs/godogs_test.go | 21 ++++++---- 6 files changed, 82 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 8d6a6ce..6717a30 100644 --- a/README.md +++ b/README.md @@ -182,14 +182,32 @@ func thereShouldBeRemaining(remaining int) error { return nil } -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) +// godog v0.9.0 (latest) and earlier +func FeatureContext(s *godog.Suite) { + s.BeforeSuite(func() { Godogs = 0 }) s.BeforeScenario(func(*godog.Scenario) { Godogs = 0 // clean the state before every scenario }) + + s.Step(`^there are (\d+) godogs$`, thereAreGodogs) + s.Step(`^I eat (\d+)$`, iEat) + s.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining) +} + +// godog v0.10.0 (coming release) +func InitializeTestSuite(ctx *godog.TestSuiteContext) { + ctx.BeforeSuite(func() { Godogs = 0 }) +} + +func InitializeScenario(ctx *godog.ScenarioContext) { + ctx.BeforeScenario(func(*godog.Scenario) { + Godogs = 0 // clean the state before every scenario + }) + + ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs) + ctx.Step(`^I eat (\d+)$`, iEat) + ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining) } ``` @@ -262,9 +280,16 @@ func TestMain(m *testing.M) { flag.Parse() opts.Paths = flag.Args() + // godog v0.9.0 (latest) and earlier + status := godog.RunWithOptions("godogs", func(s *godog.Suite) { + FeatureContext(s) + }, opts) + + // godog v0.10.0 (coming release) status := godog.TestSuite{ Name: "godogs", - ScenarioInitializer: ScenarioContext, + TestSuiteInitializer: InitializeTestSuite, + ScenarioInitializer: InitializeScenario, Options: &opts, }.Run() @@ -294,10 +319,17 @@ func TestMain(m *testing.M) { Randomize: time.Now().UTC().UnixNano(), // randomize scenario execution order } + // godog v0.9.0 (latest) and earlier + status := godog.RunWithOptions("godogs", func(s *godog.Suite) { + FeatureContext(s) + }, opts) + + // godog v0.10.0 (coming release) status := godog.TestSuite{ Name: "godogs", - ScenarioInitializer: ScenarioContext, - Options: opts, + TestSuiteInitializer: InitializeTestSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, }.Run() if st := m.Run(); st > status { @@ -326,10 +358,17 @@ func TestMain(m *testing.M) { Paths: []string{"features"}, } + // godog v0.9.0 (latest) and earlier + status := godog.RunWithOptions("godogs", func(s *godog.Suite) { + FeatureContext(s) + }, opts) + + // godog v0.10.0 (coming release) status := godog.TestSuite{ Name: "godogs", - ScenarioInitializer: ScenarioContext, - Options: opts, + TestSuiteInitializer: InitializeTestSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, }.Run() if st := m.Run(); st > status { diff --git a/_examples/api/README.md b/_examples/api/README.md index b5821bf..d4781ee 100644 --- a/_examples/api/README.md +++ b/_examples/api/README.md @@ -74,7 +74,7 @@ func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) error { return godog.ErrPending } -func ScenarioContext(s *godog.ScenarioContext) { +func InitializeScenario(s *godog.ScenarioContext) { api := &apiFeature{} s.Step(`^I send "([^"]*)" request to "([^"]*)"$`, api.iSendrequestTo) s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe) @@ -156,7 +156,7 @@ func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) error { return } -func ScenarioContext(s *godog.ScenarioContext) { +func InitializeScenario(s *godog.ScenarioContext) { api := &apiFeature{} s.BeforeScenario(api.resetResponse) diff --git a/_examples/api/api_test.go b/_examples/api/api_test.go index 1c292c2..19bce64 100644 --- a/_examples/api/api_test.go +++ b/_examples/api/api_test.go @@ -70,12 +70,12 @@ func (a *apiFeature) theResponseShouldMatchJSON(body *godog.DocString) (err erro return nil } -func ScenarioContext(s *godog.ScenarioContext) { +func InitializeScenario(ctx *godog.ScenarioContext) { api := &apiFeature{} - s.BeforeScenario(api.resetResponse) + ctx.BeforeScenario(api.resetResponse) - s.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo) - s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe) - s.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON) + ctx.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo) + ctx.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe) + ctx.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON) } diff --git a/_examples/assert-godogs/godogs_test.go b/_examples/assert-godogs/godogs_test.go index 559aee9..ea12883 100644 --- a/_examples/assert-godogs/godogs_test.go +++ b/_examples/assert-godogs/godogs_test.go @@ -23,7 +23,7 @@ func TestMain(m *testing.M) { status := godog.TestSuite{ Name: "godogs", - ScenarioInitializer: ScenarioContext, + ScenarioInitializer: InitializeScenario, Options: &opts, }.Run() @@ -65,15 +65,15 @@ func thereShouldBeNoneRemaining() error { ) } -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(*godog.Scenario) { +func InitializeScenario(ctx *godog.ScenarioContext) { + ctx.BeforeScenario(func(*godog.Scenario) { Godogs = 0 // clean the state before every scenario }) + + ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs) + ctx.Step(`^I eat (\d+)$`, iEat) + ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining) + ctx.Step(`^there should be none remaining$`, thereShouldBeNoneRemaining) } // assertExpectedAndActual is a helper function to allow the step function to call diff --git a/_examples/db/api_test.go b/_examples/db/api_test.go index ec76584..202e9b3 100644 --- a/_examples/db/api_test.go +++ b/_examples/db/api_test.go @@ -122,13 +122,13 @@ func (a *apiFeature) thereAreUsers(users *godog.Table) error { return nil } -func ScenarioContext(s *godog.ScenarioContext) { +func InitializeScenario(ctx *godog.ScenarioContext) { api := &apiFeature{} - s.BeforeScenario(api.resetResponse) + ctx.BeforeScenario(api.resetResponse) - s.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo) - s.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe) - s.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON) - s.Step(`^there are users:$`, api.thereAreUsers) + ctx.Step(`^I send "(GET|POST|PUT|DELETE)" request to "([^"]*)"$`, api.iSendrequestTo) + ctx.Step(`^the response code should be (\d+)$`, api.theResponseCodeShouldBe) + ctx.Step(`^the response should match json:$`, api.theResponseShouldMatchJSON) + ctx.Step(`^there are users:$`, api.thereAreUsers) } diff --git a/_examples/godogs/godogs_test.go b/_examples/godogs/godogs_test.go index 4e5cea5..b78e49e 100644 --- a/_examples/godogs/godogs_test.go +++ b/_examples/godogs/godogs_test.go @@ -21,9 +21,10 @@ func TestMain(m *testing.M) { opts.Paths = flag.Args() status := godog.TestSuite{ - Name: "godogs", - ScenarioInitializer: ScenarioContext, - Options: &opts, + Name: "godogs", + TestSuiteInitializer: InitializeTestSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, }.Run() if st := m.Run(); st > status { @@ -52,12 +53,16 @@ func thereShouldBeRemaining(remaining int) error { return nil } -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) +func InitializeTestSuite(ctx *godog.TestSuiteContext) { + ctx.BeforeSuite(func() { Godogs = 0 }) +} - s.BeforeScenario(func(*godog.Scenario) { +func InitializeScenario(ctx *godog.ScenarioContext) { + ctx.BeforeScenario(func(*godog.Scenario) { Godogs = 0 // clean the state before every scenario }) + + ctx.Step(`^there are (\d+) godogs$`, thereAreGodogs) + ctx.Step(`^I eat (\d+)$`, iEat) + ctx.Step(`^there should be (\d+) remaining$`, thereShouldBeRemaining) }