forked from regen-network/gocuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guess.go
155 lines (131 loc) · 3.28 KB
/
guess.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package gocuke
import (
"fmt"
"regexp"
"strings"
"unicode"
messages "github.com/cucumber/messages/go/v21"
)
func guessMethodSig(step *messages.PickleStep) methodSig {
parts := strings.Split(strings.TrimSpace(step.Text), " ")
var (
nameParts []string
paramTypes []string
regexParts []string
inSingleQuote bool
inDoubleQuote bool
)
for i := 0; i < len(parts); i++ {
part := parts[i]
if inSingleQuote {
if lastChar(part) == '\'' {
inSingleQuote = false
}
continue
}
if inDoubleQuote {
if lastChar(part) == '"' {
inDoubleQuote = false
}
continue
}
c := firstChar(part)
switch c {
case '\'':
if lastChar(part) != '\'' {
inSingleQuote = true
}
paramTypes = append(paramTypes, "string")
regexParts = append(regexParts, `'([^']*)'`)
continue
case '"':
if lastChar(part) != '"' {
inDoubleQuote = true
}
paramTypes = append(paramTypes, "string")
regexParts = append(regexParts, `"([^"]*)"`)
continue
default:
if decRegex.MatchString(part) {
paramTypes = append(paramTypes, "*apd.Decimal")
regexParts = append(regexParts, `(-?\d+\.\d+)`)
continue
}
if intRegex.MatchString(part) {
paramTypes = append(paramTypes, "int64")
regexParts = append(regexParts, `(-?\d+)`)
continue
}
}
nameParts = append(nameParts, part)
regexParts = append(regexParts, regexp.QuoteMeta(part))
}
regex := regexp.MustCompile(`^` + strings.Join(regexParts, `\s+`) + `$`)
if step.Argument != nil {
if step.Argument.DataTable != nil {
paramTypes = append(paramTypes, "gocuke.DataTable")
} else if step.Argument.DocString != nil {
paramTypes = append(paramTypes, "gocuke.DocString")
}
}
if len(nameParts) == 0 {
return methodSig{name: "unknown", paramTypes: paramTypes, regex: regex}
}
var name string
for i := 0; i < len(nameParts); i++ {
n := toFirstUpperIdentifier(nameParts[i])
if n == "" {
continue
}
name = name + n
}
return methodSig{name: name, paramTypes: paramTypes, regex: regex}
}
func firstChar(x string) byte {
return x[0]
}
func lastChar(x string) byte {
return x[len(x)-1]
}
func toFirstUpperIdentifier(str string) string {
var res []rune
isFirst := true
for _, r := range str {
if isFirst {
if !(unicode.IsLetter(r) || unicode.IsNumber(r)) {
continue
}
res = append(res, unicode.ToUpper(r))
isFirst = false
} else {
if !(unicode.IsLetter(r) || unicode.IsNumber(r)) {
continue
}
res = append(res, unicode.ToLower(r))
}
}
return string(res)
}
type methodSig struct {
name string
paramTypes []string
regex *regexp.Regexp
}
func (m methodSig) methodSig() string {
paramNames := make([]string, len(m.paramTypes))
for i, paramType := range m.paramTypes {
paramNames[i] = fmt.Sprintf("%s %s", string(rune('a'+i)), paramType)
}
return fmt.Sprintf(`%s(%s)`, m.name, strings.Join(paramNames, ", "))
}
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+$`)