diff --git a/internal/formatters/fmt_ast.go b/internal/formatters/fmt_ast.go index bd5174d..ab06e23 100644 --- a/internal/formatters/fmt_ast.go +++ b/internal/formatters/fmt_ast.go @@ -1,12 +1,15 @@ package formatters import ( + "bytes" "fmt" "io" "os" "sort" "strconv" "strings" + "text/template" + "unicode" "unicode/utf8" messages "github.com/cucumber/messages/go/v21" @@ -15,6 +18,7 @@ import ( "git.golang1.ru/softonik/godog/formatters" "git.golang1.ru/softonik/godog/internal/models" "git.golang1.ru/softonik/godog/internal/utils" + "git.golang1.ru/softonik/godog/pkg/formatters/ast" ) func ASTRegister() { @@ -348,6 +352,85 @@ func (f *AST) SummaryBottom() { } } +func (f *AST) Snippets() string { + undefinedStepResults := f.Storage.MustGetPickleStepResultsByStatus(undefined) + if len(undefinedStepResults) == 0 { + return "" + } + + var index int + var snips []undefinedSnippet + // build snippets + for _, u := range undefinedStepResults { + pickleStep := f.Storage.MustGetPickleStep(u.PickleStepID) + + steps := []string{pickleStep.Text} + arg := pickleStep.Argument + if u.Def != nil { + steps = u.Def.Undefined + arg = nil + } + for _, step := range steps { + expr := snippetExprCleanup.ReplaceAllString(step, "\\$1") + expr = snippetNumbers.ReplaceAllString(expr, "(\\d+)") + expr = snippetExprQuoted.ReplaceAllString(expr, "$1\"([^\"]*)\"$2") + expr = "^" + strings.TrimSpace(expr) + "$" + + name := snippetNumbers.ReplaceAllString(step, " ") + name = snippetExprQuoted.ReplaceAllString(name, " ") + name = strings.TrimSpace(snippetMethodName.ReplaceAllString(name, "")) + var words []string + for i, w := range strings.Split(name, " ") { + switch { + case i != 0: + w = strings.Title(w) + case len(w) > 0: + r := []rune(w) + w = string(unicode.ToLower(r[0])) + string(r[1:]) + } + words = append(words, w) + } + name = strings.Join(words, "") + if len(name) == 0 { + index++ + name = fmt.Sprintf("StepDefinitioninition%d", index) + } + + var found bool + for _, snip := range snips { + if snip.Expr == expr { + found = true + break + } + } + if !found { + s := undefinedSnippet{Method: name, Expr: expr, argument: arg} + snips = append(snips, s) + ast.ДобавитьШаг("`"+expr+"`", name, s.Args()) + } + } + } + + var buf bytes.Buffer + if err := AST_undefinedSnippetsTpl.Execute(&buf, snips); err != nil { + panic(err) + } + // there may be trailing spaces + return strings.Replace(buf.String(), " \n", "\n", -1) +} + +var AST_undefinedSnippetsTpl = template.Must(template.New("snippets").Funcs(snippetHelperFuncs).Parse(` + func InitializeScenario(ctx *godog.ScenarioContext) {} + // --- + + +{{ range . }}func {{ .Method }}({{ .Args }}) { +} +{{end}}{{ range . }} + ctx.Step({{ backticked .Expr }}, {{ .Method }}){{end}} + +`)) + func (f *AST) printOutlineExample(pickle *messages.Pickle, step *messages.PickleStep, backgroundSteps int) { var errorMsg string var clr = green