From 2968ddba76aa71a649491df6c40b5a1b0a0756c0 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Tue, 13 Feb 2024 09:17:39 -0500 Subject: [PATCH] feat: output suggestions in the order steps were defined (#29) When using gocuke with new feature files, I've found that it sometimes outputs step suggestions in an illogical order. This is because under the hood suggestions are stored in a map instead of an array. This PR creates converts that code to use an array so that suggestions get outputted in the same order as they were declared in the feature file. --- README.md | 12 ++++++------ find_step.go | 5 ++++- runner.go | 11 ++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index aa6bf71..e522393 100644 --- a/README.md +++ b/README.md @@ -77,22 +77,22 @@ type suite struct { When you run the tests, they should fail and suggest that you add these step definitions: ```go -func (s *suite) IEat(a int64) { +func (s *suite) IHaveCukes(a int64) { panic("PENDING") } -func (s *suite) IHaveLeft(a int64) { +func (s *suite) IEat(a int64) { panic("PENDING") } -func (s *suite) IHaveCukes(a int64) { +func (s *suite) IHaveLeft(a int64) { panic("PENDING") } Steps can be manually registered with the runner for customization using this code: - Step(`^I\s+have\s+(-?\d+)\s+cukes$`, (*simpleSuite).IHaveCukes). - Step(`^I\s+eat\s+(-?\d+)$`, (*simpleSuite).IEat). - Step(`^I\s+have\s+(-?\d+)\s+left$`, (*simpleSuite).IHaveLeft) + Step(`^I\s+have\s+(-?\d+)\s+cukes$`, (*suite).IHaveCukes). + Step(`^I\s+eat\s+(-?\d+)$`, (*suite).IEat). + Step(`^I\s+have\s+(-?\d+)\s+left$`, (*suite).IHaveLeft) ``` Copy these definitions into `simple_test.go`. diff --git a/find_step.go b/find_step.go index 4fdf6e1..461275b 100644 --- a/find_step.go +++ b/find_step.go @@ -22,7 +22,10 @@ func (r *Runner) findStep(t *testing.T, step *messages.PickleStep) *stepDef { return r.addStepDef(t, sig.regex, method.Func) } - r.suggestions[sig.name] = sig + if !r.haveSuggestion[sig.name] { + r.haveSuggestion[sig.name] = true + r.suggestions = append(r.suggestions, sig) + } t.Errorf("can't find step definition for: %s", step.Text) return nil diff --git a/runner.go b/runner.go index 97bdcd1..c91cc16 100644 --- a/runner.go +++ b/runner.go @@ -17,7 +17,8 @@ type Runner struct { paths []string parallel bool stepDefs []*stepDef - suggestions map[string]methodSig + haveSuggestion map[string]bool + suggestions []methodSig supportedSpecialArgs map[reflect.Type]specialArgGetter suiteInjectors []*suiteInjector beforeHooks []*stepDef @@ -56,10 +57,10 @@ func NewRunner(t *testing.T, suiteType interface{}) *Runner { initGlobalTagExpr() r := &Runner{ - topLevelT: t, - incr: &messages.Incrementing{}, - parallel: false, - suggestions: map[string]methodSig{}, + topLevelT: t, + incr: &messages.Incrementing{}, + parallel: false, + haveSuggestion: map[string]bool{}, supportedSpecialArgs: map[reflect.Type]specialArgGetter{ // TestingT reflect.TypeOf((*TestingT)(nil)).Elem(): func(runner *scenarioRunner) interface{} {