Skip to content

Commit

Permalink
feat: output suggestions in the order steps were defined (#29)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aaronc authored Feb 13, 2024
1 parent 0551d14 commit 2968ddb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
5 changes: 4 additions & 1 deletion find_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{} {
Expand Down

0 comments on commit 2968ddb

Please sign in to comment.