From 0d1fb60ba05759583e84064ea50e0b5d297c9b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6nnblad?= Date: Sun, 15 Mar 2020 20:22:49 -0300 Subject: [PATCH] Added sorting for undefined snippets on method name --- features/snippets.feature | 20 ++++++++++---------- fmt.go | 7 +++++-- fmt_progress_test.go | 18 +++++++++--------- undefined_snippets_gen.go | 16 +++++++++++++++- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/features/snippets.feature b/features/snippets.feature index f2bc607..4550c05 100644 --- a/features/snippets.feature +++ b/features/snippets.feature @@ -105,17 +105,17 @@ Feature: undefined step snippets When I run feature suite And the undefined step snippets should be: """ - func thereIsAWhichCosts(arg1 string, arg2 int) error { - return godog.ErrPending - } - func iAddTheToTheBasket(arg1 string) error { return godog.ErrPending } + func thereIsAWhichCosts(arg1 string, arg2 int) error { + return godog.ErrPending + } + func FeatureContext(s *godog.Suite) { - s.Step(`^there is a "([^"]*)", which costs £(\d+)$`, thereIsAWhichCosts) s.Step(`^I add the "([^"]*)" to the basket$`, iAddTheToTheBasket) + s.Step(`^there is a "([^"]*)", which costs £(\d+)$`, thereIsAWhichCosts) } """ @@ -131,16 +131,16 @@ Feature: undefined step snippets When I run feature suite And the undefined step snippets should be: """ - func whichCosts(arg1 string, arg2 int) error { - return godog.ErrPending - } - func godogs(arg1 int) error { return godog.ErrPending } + func whichCosts(arg1 string, arg2 int) error { + return godog.ErrPending + } + func FeatureContext(s *godog.Suite) { - s.Step(`^"([^"]*)", which costs £(\d+)$`, whichCosts) s.Step(`^(\d+) godogs$`, godogs) + s.Step(`^"([^"]*)", which costs £(\d+)$`, whichCosts) } """ diff --git a/fmt.go b/fmt.go index aa60ed1..9779636 100644 --- a/fmt.go +++ b/fmt.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "sort" "strconv" "strings" "sync" @@ -428,7 +429,7 @@ func (f *basefmt) snippets() string { } var index int - var snips []*undefinedSnippet + var snips []undefinedSnippet // build snippets for _, u := range undefinedStepResults { steps := []string{u.step.Text} @@ -470,11 +471,13 @@ func (f *basefmt) snippets() string { } } if !found { - snips = append(snips, &undefinedSnippet{Method: name, Expr: expr, argument: arg}) + snips = append(snips, undefinedSnippet{Method: name, Expr: expr, argument: arg}) } } } + sort.Sort(snippetSortByMethod(snips)) + var buf bytes.Buffer if err := undefinedSnippetsTpl.Execute(&buf, snips); err != nil { panic(err) diff --git a/fmt_progress_test.go b/fmt_progress_test.go index 54dfc81..53780be 100644 --- a/fmt_progress_test.go +++ b/fmt_progress_test.go @@ -67,17 +67,17 @@ func TestProgressFormatterOutput(t *testing.T) { You can implement step definitions for undefined steps with these snippets: -func undefined() error { - return godog.ErrPending -} - func nextUndefined() error { return godog.ErrPending } +func undefined() error { + return godog.ErrPending +} + func FeatureContext(s *godog.Suite) { - s.Step(` + "`^undefined$`" + `, undefined) s.Step(` + "`^next undefined$`" + `, nextUndefined) + s.Step(` + "`^undefined$`" + `, undefined) } ` @@ -237,7 +237,7 @@ func TestProgressFormatterMultistepTemplates(t *testing.T) { You can implement step definitions for undefined steps with these snippets: -func undef() error { +func three() error { return godog.ErrPending } @@ -245,14 +245,14 @@ func unavailableCost(arg1 string, arg2 int) error { return godog.ErrPending } -func three() error { +func undef() error { return godog.ErrPending } func FeatureContext(s *godog.Suite) { - s.Step(` + "`^undef$`" + `, undef) - s.Step(` + "`^unavailable \"([^\"]*)\" cost (\\d+)$`" + `, unavailableCost) s.Step(` + "`^three$`" + `, three) + s.Step(` + "`^unavailable \"([^\"]*)\" cost (\\d+)$`" + `, unavailableCost) + s.Step(` + "`^undef$`" + `, undef) } ` diff --git a/undefined_snippets_gen.go b/undefined_snippets_gen.go index 947047b..456b50d 100644 --- a/undefined_snippets_gen.go +++ b/undefined_snippets_gen.go @@ -38,7 +38,7 @@ type undefinedSnippet struct { argument *messages.PickleStepArgument } -func (s *undefinedSnippet) Args() (ret string) { +func (s undefinedSnippet) Args() (ret string) { var ( args []string pos int @@ -92,3 +92,17 @@ func (s *undefinedSnippet) Args() (ret string) { return strings.TrimSpace(strings.TrimRight(ret, ", ") + " " + last) } + +type snippetSortByMethod []undefinedSnippet + +func (s snippetSortByMethod) Len() int { + return len(s) +} + +func (s snippetSortByMethod) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func (s snippetSortByMethod) Less(i, j int) bool { + return s[i].Method < s[j].Method +}