Skip to content

Commit

Permalink
feat: suggest manual step registration (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronc authored Apr 12, 2022
1 parent 9648eb6 commit db25658
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (s *suite) IHaveLeft(a int64) {
func (s *suite) IHaveCukes(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)
```

Copy these definitions into `simple_test.go`.
Expand Down
9 changes: 7 additions & 2 deletions guess.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package gocuke

import (
"fmt"
"github.com/cucumber/common/messages/go/v17"
"regexp"
"strings"
"unicode"

"github.com/cucumber/common/messages/go/v17"
)

func guessMethodSig(step *messages.PickleStep) methodSig {
Expand Down Expand Up @@ -140,12 +141,16 @@ func (m methodSig) methodSig() string {
return fmt.Sprintf(`%s(%s)`, m.name, strings.Join(paramNames, ", "))
}

func (m methodSig) suggestion(suiteTypeName string) string {
func (m methodSig) methodSuggestion(suiteTypeName string) string {
return fmt.Sprintf(`func (s %s) %s {
panic("PENDING")
}`,
suiteTypeName, m.methodSig())
}

func (m methodSig) stepSuggestion(suiteTypeName string) string {
return fmt.Sprintf("Step(`%s`, (%s).%s)", m.regex.String(), suiteTypeName, m.name)
}

var decRegex = regexp.MustCompile(`^-?\d+\.\d+$`)
var intRegex = regexp.MustCompile(`^-?\d+$`)
15 changes: 12 additions & 3 deletions run.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package gocuke

import (
gherkin "github.com/cucumber/common/gherkin/go/v22"
"gotest.tools/v3/assert"
"os"
"path/filepath"
"reflect"
"testing"

gherkin "github.com/cucumber/common/gherkin/go/v22"
"gotest.tools/v3/assert"
)

// Run runs the features registered with the runner.
Expand Down Expand Up @@ -56,8 +57,16 @@ func (r *Runner) Run() {

suggestionText := "Missing step definitions can be fixed with the following methods:\n"
for _, sig := range r.suggestions {
suggestionText += sig.suggestion(suiteTypeName) + "\n\n"
suggestionText += sig.methodSuggestion(suiteTypeName) + "\n\n"
}

suggestionText += "Steps can be manually registered with the runner for customization using this code:\n"
for _, sig := range r.suggestions {
suggestionText += " " + sig.stepSuggestion(suiteTypeName) + ".\n"
}
suggestionText += "\n\n"
suggestionText += "See https://github.com/regen-network/gocuke for further customization options."

r.topLevelT.Logf(suggestionText)
}
}

0 comments on commit db25658

Please sign in to comment.