Код генерится с именами параметров (шаблонных примеров)

Этот коммит содержится в:
Softonik 2023-05-02 05:05:27 +03:00 коммит произвёл Nikolay Kopitonenko
родитель 4edded5e97
коммит 3c9fe54fd6
4 изменённых файлов: 90 добавлений и 11 удалений

Просмотреть файл

@ -153,22 +153,24 @@ Feature: undefined step snippets
Сценарий: делает что-то полезное Сценарий: делает что-то полезное
Дано что-то Дано что-то
Когда я делаю ещё что-то Когда я делаю ещё что-то "<param1>", 1, 2, param="<param2>", "<параметр3>", "just string", "<data>"
То получается ещё более что-то То получается ещё более что-то
Примеры:
|param1|param2|параметр3|data|
|--- |--- |--- |--- |
""" """
When I run feature suite When I run feature suite
Then the following steps should be undefined: Then the following steps should be undefined:
""" """
получается ещё более что-то получается ещё более что-то
что-то что-то
я делаю ещё что-то
""" """
And the undefined step snippets should be: And the undefined step snippets should be:
""" """
func чтото() { func чтото() {
} }
func яДелаюЕщёЧтото() { func яДелаюЕщёЧтотоParam(param1 string, arg1, arg2 int, param2, параметр3, arg3, data string) {
} }
func получаетсяЕщёБолееЧтото() { func получаетсяЕщёБолееЧтото() {
@ -176,7 +178,7 @@ Feature: undefined step snippets
func InitializeScenario(ctx *godog.ScenarioContext) { func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(`^что-то$`, чтото) ctx.Step(`^что-то$`, чтото)
ctx.Step(`^я делаю ещё что-то$`, яДелаюЕщёЧтото) ctx.Step(`^я делаю ещё что-то "([^"]*)", (\d+), (\d+), param="([^"]*)", "([^"]*)", "([^"]*)", "([^"]*)"$`, яДелаюЕщёЧтотоParam)
ctx.Step(`^получается ещё более что-то$`, получаетсяЕщёБолееЧтото) ctx.Step(`^получается ещё более что-то$`, получаетсяЕщёБолееЧтото)
} }
""" """

Просмотреть файл

@ -205,7 +205,6 @@ func (f *Base) Snippets() string {
// build snippets // build snippets
for _, u := range undefinedStepResults { for _, u := range undefinedStepResults {
pickleStep := f.Storage.MustGetPickleStep(u.PickleStepID) pickleStep := f.Storage.MustGetPickleStep(u.PickleStepID)
steps := []string{pickleStep.Text} steps := []string{pickleStep.Text}
arg := pickleStep.Argument arg := pickleStep.Argument
if u.Def != nil { if u.Def != nil {
@ -247,7 +246,12 @@ func (f *Base) Snippets() string {
} }
} }
if !found { if !found {
snips = append(snips, undefinedSnippet{Method: name, Expr: expr, argument: arg}) snips = append(snips, undefinedSnippet{
Method: name,
Expr: expr,
argument: arg,
argument_names: f.get_argument_names(&u, pickleStep),
})
} }
} }
} }
@ -259,3 +263,58 @@ func (f *Base) Snippets() string {
// there may be trailing spaces // there may be trailing spaces
return strings.Replace(buf.String(), " \n", "\n", -1) return strings.Replace(buf.String(), " \n", "\n", -1)
} }
func (f *Base) get_argument_names(undefResult *models.PickleStepResult, pickleStep *messages.PickleStep) (arg_names map[int]string) {
step := f.find_undef_step(undefResult, pickleStep)
if step == nil {
return
}
arg_names = map[int]string{}
args := snippetExprAnyParam.FindAllString(step.Text, -1)
for i, a := range args {
a = strings.TrimSpace(a)
if len(a) < 5 {
continue
}
parts := strings.Split(a, "\"")
if len(parts) < 3 {
continue
}
a = parts[1]
if a[0] != '<' {
continue
}
a = a[1 : len(a)-1]
// fmt.Printf("\ta: %v: %v\n", a, parts)
arg_names[i] = a
}
return
}
func (f *Base) find_undef_step(undefResult *models.PickleStepResult, pickleStep *messages.PickleStep) *messages.Step {
pickle := f.Storage.MustGetPickle(undefResult.PickleID)
if pickle == nil {
return nil
}
feat := f.Storage.MustGetFeature(pickle.Uri)
if feat == nil {
return nil
}
if len(pickleStep.AstNodeIds) == 0 {
return nil
}
step_id := pickleStep.AstNodeIds[0]
return feat.FindStep(step_id)
}

Просмотреть файл

@ -13,6 +13,7 @@ import (
// some snippet formatting regexps // some snippet formatting regexps
var snippetExprCleanup = regexp.MustCompile("([\\/\\[\\]\\(\\)\\\\^\\$\\.\\|\\?\\*\\+\\'])") var snippetExprCleanup = regexp.MustCompile("([\\/\\[\\]\\(\\)\\\\^\\$\\.\\|\\?\\*\\+\\'])")
var snippetExprQuoted = regexp.MustCompile("(\\W|^)\"(?:[^\"]*)\"(\\W|$)") var snippetExprQuoted = regexp.MustCompile("(\\W|^)\"(?:[^\"]*)\"(\\W|$)")
var snippetExprAnyParam = regexp.MustCompile("(\\W|^)((\\d+)|\"(?:[^\"]*)\")(\\W|$)")
var snippetMethodName = regexp.MustCompile("[^a-zA-Zа-яА-ЯёЁ\\_\\ ]") var snippetMethodName = regexp.MustCompile("[^a-zA-Zа-яА-ЯёЁ\\_\\ ]")
var snippetNumbers = regexp.MustCompile("(\\d+)") var snippetNumbers = regexp.MustCompile("(\\d+)")
@ -32,9 +33,10 @@ var undefinedSnippetsTpl = template.Must(template.New("snippets").Funcs(snippetH
`)) `))
type undefinedSnippet struct { type undefinedSnippet struct {
Method string Method string
Expr string Expr string
argument *messages.PickleStepArgument argument *messages.PickleStepArgument
argument_names map[int]string
} }
func (s undefinedSnippet) Args() (ret string) { func (s undefinedSnippet) Args() (ret string) {
@ -79,11 +81,25 @@ func (s undefinedSnippet) Args() (ret string) {
var last string var last string
// fmt.Printf(" \tArgs: names: %v\n", s.argument_names)
arg_ind := 0
for i, arg := range args { for i, arg := range args {
name, ok := s.argument_names[i]
if last == "" || last == arg { if last == "" || last == arg {
ret += fmt.Sprintf("arg%d, ", i+1) if ok {
ret += fmt.Sprintf("%v, ", name)
} else {
arg_ind++
ret += fmt.Sprintf("arg%d, ", arg_ind)
}
} else { } else {
ret = strings.TrimRight(ret, ", ") + fmt.Sprintf(" %s, arg%d, ", last, i+1) if ok {
ret = strings.TrimRight(ret, ", ") + fmt.Sprintf(" %s, %v, ", last, name)
} else {
arg_ind++
ret = strings.TrimRight(ret, ", ") + fmt.Sprintf(" %s, arg%d, ", last, arg_ind)
}
} }
last = arg last = arg

Просмотреть файл

@ -228,6 +228,8 @@ func (tc *godogFeaturesScenario) iRunFeatureSuiteWithFormatter(name string) erro
} }
func (tc *godogFeaturesScenario) iRunFeatureSuiteWithTagsAndFormatter(filter string, fmtFunc FormatterFunc) error { func (tc *godogFeaturesScenario) iRunFeatureSuiteWithTagsAndFormatter(filter string, fmtFunc FormatterFunc) error {
// Entry point for tested feature
if err := tc.parseFeatures(); err != nil { if err := tc.parseFeatures(); err != nil {
return err return err
} }