Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow keywords starting with a variable in bdd style #5096

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions atest/robot/keywords/optional_given_when_then.robot
Expand Up @@ -46,6 +46,12 @@ Keyword can be used with and without prefix
Should Be Equal ${tc.kws[5].full_name} Then we are in Berlin city
Should Be Equal ${tc.kws[6].full_name} we are in Berlin city

First word in a keyword can be an argument
${tc} = Check Test Case ${TEST NAME}
Should Be Equal ${tc.kws[0].full_name} Given we don't drink too many beers
Should Be Equal ${tc.kws[1].full_name} Then Pekka drinks lonkero instead
Should Be Equal ${tc.kws[2].full_name} and Miikka drinks water instead

Localized prefixes
${tc} = Check Test Case ${TEST NAME}
Should Be Equal ${tc.kws[0].full_name} Oletetaan we don't drink too many beers
Expand Down
9 changes: 9 additions & 0 deletions atest/testdata/keywords/optional_given_when_then.robot
Expand Up @@ -48,6 +48,11 @@ Keyword can be used with and without prefix
Then we are in Berlin city
we are in Berlin city

First word in a keyword can be an argument
Given we don't drink too many beers
Then Pekka drinks lonkero instead
and Miikka drinks water instead

Localized prefixes
Oletetaan we don't drink too many beers
Kun we are in museum cafe
Expand Down Expand Up @@ -98,5 +103,9 @@ We ${x} This ${thing} Implemented
We Go To ${somewhere}
Should Be Equal ${somewhere} walking tour

${person} drinks ${beverage} instead
Should not start with ${person} Then
Should not start with ${person} and

Multipart prefixes didn't work with RF 6.0
No Operation
25 changes: 14 additions & 11 deletions src/robot/running/namespace.py
Expand Up @@ -23,6 +23,7 @@
from robot.output import LOGGER, Message
from robot.utils import (eq, find_file, is_string, normalize, RecommendationFinder,
seq2str2)
from robot.variables.search import search_variable

from .context import EXECUTION_CONTEXTS
from .importer import ImportCache, Importer
Expand Down Expand Up @@ -297,21 +298,23 @@ def _get_runner(self, name):
runner = self._get_explicit_runner(name)
if not runner:
runner = self._get_implicit_runner(name)
if not runner:
runner = self._get_bdd_style_runner(name, self.languages.bdd_prefixes)
# handle bdd prefixes, even when swallowed by a leading variable
if not runner or not search_variable(runner.keyword.name).before:
prefix, kw_name = self._detect_bdd_prefix(name)
if prefix:
bdd_runner = self._get_runner(kw_name)
if bdd_runner:
runner = copy.copy(bdd_runner)
runner.name = f"{prefix} {kw_name}"
return runner

def _get_bdd_style_runner(self, name, prefixes):
def _detect_bdd_prefix(self, name):
parts = name.split()
for index in range(1, len(parts)):
prefix = ' '.join(parts[:index]).title()
if prefix in prefixes:
runner = self._get_runner(' '.join(parts[index:]))
if runner:
runner = copy.copy(runner)
runner.name = name
return runner
return None
prefix = ' '.join(parts[:index])
if prefix.title() in self.languages.bdd_prefixes:
return (prefix, ' '.join(parts[index:]))
return ('', name)

def _get_implicit_runner(self, name):
return (self._get_runner_from_resource_files(name) or
Expand Down