From baa1e33e604e50c92f81342d95ec569c9948f90d Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 5 Aug 2022 18:02:16 +0200 Subject: [PATCH] Add a test for BeforeStep in multisteps This demonstrates the bug reported in #486 --- features/multistep.feature | 13 +++++++++++++ run_test.go | 12 ++++++------ suite_context_test.go | 23 +++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/features/multistep.feature b/features/multistep.feature index a9726788..a27afa7e 100644 --- a/features/multistep.feature +++ b/features/multistep.feature @@ -161,3 +161,16 @@ Feature: run features with nested steps """ When I run feature suite Then the suite should have passed + + Scenario: BeforeStep used for multisteps + Given a feature "normal.feature" file: + """ + Feature: normal feature + + Scenario: invoke BeforeStep in multistep + Given I allow variable injection + And I run multisteps that set a value + Then the stored value is "someverylonginjectionsoweacanbesureitsurpasstheinitiallongeststeplenghtanditwillhelptestsmethodsafety" + """ + When I run feature suite + Then the suite should have passed diff --git a/run_test.go b/run_test.go index 9c07a6dd..f740fd2a 100644 --- a/run_test.go +++ b/run_test.go @@ -518,11 +518,11 @@ func Test_AllFeaturesRun(t *testing.T) { ...................................................................... 140 ...................................................................... 210 ...................................................................... 280 -....................................................... 335 +.......................................................... 338 -88 scenarios (88 passed) -335 steps (335 passed) +89 scenarios (89 passed) +338 steps (338 passed) 0s ` @@ -545,11 +545,11 @@ func Test_AllFeaturesRunAsSubtests(t *testing.T) { ...................................................................... 140 ...................................................................... 210 ...................................................................... 280 -....................................................... 335 +.......................................................... 338 -88 scenarios (88 passed) -335 steps (335 passed) +89 scenarios (89 passed) +338 steps (338 passed) 0s ` diff --git a/suite_context_test.go b/suite_context_test.go index 7a2fc7fa..7ee66527 100644 --- a/suite_context_test.go +++ b/suite_context_test.go @@ -141,6 +141,12 @@ func InitializeScenario(ctx *ScenarioContext) { } }) + ctx.Step(`I store value "([^"]*)"$`, tc.iStoreValue) + ctx.Step(`^the stored value is "([^"]*)"$`, tc.theStoredValueIs) + ctx.Step(`^I run multisteps that set a value$`, func() Steps { + return Steps{`I store value "{{X}}"`} + }) + ctx.StepContext().Before(tc.inject) } @@ -365,6 +371,23 @@ func (tc *godogFeaturesScenario) iShouldSeeTheContextInTheNextStep(ctx context.C return nil } +type valueContextKey struct{} + +func (tc *godogFeaturesScenario) iStoreValue(ctx context.Context, value string) context.Context { + return context.WithValue(ctx, valueContextKey{}, value) +} + +func (tc *godogFeaturesScenario) theStoredValueIs(ctx context.Context, want string) error { + got, ok := ctx.Value(valueContextKey{}).(string) + if !ok { + return errors.New("context does not contain our key") + } + if got != want { + return fmt.Errorf("context has the wrong value for our key: got %s, want %s", got, want) + } + return nil +} + func (tc *godogFeaturesScenario) followingStepsShouldHave(status string, steps *DocString) error { var expected = strings.Split(steps.Content, "\n") var actual, unmatched, matched []string