Skip to content

Commit 100a11c

Browse files
author
joao.nunes
committed
feat(python): support for pypi parse() format and string concatenation
1 parent 9bb66a1 commit 100a11c

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

src/language/pythonLanguage.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export const pythonLanguage: Language = {
3434
}
3535
},
3636
toStepDefinitionExpression(node: TreeSitterSyntaxNode): StringOrRegExp {
37+
if (node.type === 'binary_operator') {
38+
return collectStringFragments(node).join('')
39+
}
3740
return toStringOrRegExp(node.text)
3841
},
3942
defineParameterTypeQueries: [
@@ -95,10 +98,33 @@ export const pythonLanguage: Language = {
9598
(decorator
9699
(call
97100
function: (identifier) @method
98-
arguments: (argument_list (string) @expression)
101+
arguments: (argument_list [
102+
(string) @expression
103+
(binary_operator) @expression
104+
])
105+
)
106+
)
107+
(#match? @method "(given|when|then|step)")
108+
) @root`,
109+
// pypi parse
110+
`(decorator
111+
(call
112+
function: (identifier) @matcher
113+
arguments: (argument_list
114+
(call
115+
function: [
116+
(identifier) @parser
117+
(attribute
118+
attribute: (identifier) @parser)
119+
]
120+
arguments: (argument_list
121+
((_)+ @expression)
122+
)
123+
)
99124
)
100125
)
101-
(#match? @method "(given|when|then|step|Given|When|Then|Step)")
126+
(#match? @matcher "given|when|then|step")
127+
(#match? @parser "parse")
102128
) @root`,
103129
],
104130
snippetParameters: {
@@ -157,3 +183,13 @@ export function isRegExp(cleanWord: string): boolean {
157183
function removePrefix(text: string, prefix: string): string {
158184
return text.startsWith(prefix) ? text.slice(1) : text
159185
}
186+
187+
function collectStringFragments(node: TreeSitterSyntaxNode): string[] {
188+
if (node.type === 'string') {
189+
return [stringLiteral(node.text)]
190+
}
191+
if (node.type === 'binary_operator') {
192+
return node.children.flatMap(collectStringFragments)
193+
}
194+
return []
195+
}
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
1-
"""Port of givens for testdata."""
2-
31
from behave import step, given, when, then
2+
from pytest_bdd import parsers
3+
from pytest_bdd.parsers import parse
44

55

6-
@step("a {uuid}")
6+
@step(parse("a {uuid}"))
77
def step_given(context, uuid):
8+
"""Test PyPi parser syntax"""
89
assert uuid
910

1011

11-
@given("a {date}")
12+
custom_types = {}
13+
@given(parsers.parse("a {date}", custom_types))
1214
def step_date(context, date):
15+
"""Test extra parameter types"""
1316
assert date
1417

1518

16-
@when("a {planet}")
19+
@when(parse("a " + "{planet}"))
1720
def step_planet(context, planet):
21+
"""Test string concatenation"""
1822
assert planet
1923

2024

21-
@then("an {undefined-parameter}")
25+
@then(parsers.parse("an {undefined-parameter}"))
2226
def step_undef(context, planet):
27+
"""Test undefined parameter type"""
2328
assert planet
2429

2530

26-
@Step("/^a regexp$/")
31+
@when("/^a regexp$/")
2732
def step_re(context, expression):
28-
"""Test Re."""
33+
"""Test regular expression"""
2934
assert expression
3035

3136

32-
@Given("the bee's knees")
37+
@then("the " + "bee's knees")
3338
def step_bees(context, expression):
34-
"""Test Re."""
39+
"""Test string concatenation"""
3540
assert expression

0 commit comments

Comments
 (0)