Commit f998ffbf by Gabriel Falcao

non-greedy regexes on step definition snippets. closes #155

parents 91c48025 1138d09f
......@@ -194,30 +194,23 @@ class Step(object):
self.keys = tuple(keys)
self.hashes = HashList(self, hashes)
self.described_at = StepDescription(line, filename)
self.proposed_method_name, self.proposed_sentence = self.propose_definition()
def propose_definition(self):
sentence = unicode(self.original_sentence)
method_name = sentence
groups = [
('"', re.compile(r'("[^"]+")')), # double quotes
("'", re.compile(r"('[^']+')")), # single quotes
('"', re.compile(r'("[^"]+")'), r'"([^"]*)"'),
("'", re.compile(r"('[^']+')"), r"\'([^\']*)\'"),
]
attribute_names = []
for char, group in groups:
for char, group, template in groups:
match_groups = group.search(self.original_sentence)
if match_groups:
for index, match in enumerate(group.findall(sentence)):
if char == "'":
char = re.escape(char)
sentence = sentence.replace(match, u'%s(.*)%s' % (char, char))
sentence = sentence.replace(match, template)
group_name = u"group%d" % (index + 1)
method_name = method_name.replace(match, group_name)
attribute_names.append(group_name)
......
......@@ -714,7 +714,7 @@ def test_output_snippets_with_groups_within_double_quotes_colorless():
u"# -*- coding: utf-8 -*-\n"
u'from lettuce import step\n'
u'\n'
u'@step(u\'Given I have "(.*)" and "(.*)"\')\n'
u'@step(u\'Given I have "([^\"]*)" and "([^\"]*)"\')\n'
u'def given_i_have_group1_and_group2(step, group1, group2):\n'
u' assert False, \'This step must be implemented\'\n'
)
......@@ -742,7 +742,7 @@ def test_output_snippets_with_groups_within_double_quotes_colorful():
u"# -*- coding: utf-8 -*-\n"
u'from lettuce import step\n'
u'\n'
u'@step(u\'Given I have "(.*)" and "(.*)"\')\n'
u'@step(u\'Given I have "([^"]*)" and "([^"]*)"\')\n'
u'def given_i_have_group1_and_group2(step, group1, group2):\n'
u' assert False, \'This step must be implemented\'\033[0m\n'
)
......@@ -771,7 +771,7 @@ def test_output_snippets_with_groups_within_single_quotes_colorless():
u"# -*- coding: utf-8 -*-\n"
u'from lettuce import step\n'
u'\n'
u'@step(u\'Given I have \\\'(.*)\\\' and \\\'(.*)\\\'\')\n'
u'@step(u\'Given I have \\\'([^\\\']*)\\\' and \\\'([^\\\']*)\\\'\')\n'
u'def given_i_have_group1_and_group2(step, group1, group2):\n'
u' assert False, \'This step must be implemented\'\n'
)
......@@ -799,7 +799,7 @@ def test_output_snippets_with_groups_within_single_quotes_colorful():
u"# -*- coding: utf-8 -*-\n"
u'from lettuce import step\n'
u'\n'
u'@step(u\'Given I have \\\'(.*)\\\' and \\\'(.*)\\\'\')\n'
u'@step(u\'Given I have \\\'([^\\\']*)\\\' and \\\'([^\\\']*)\\\'\')\n'
u'def given_i_have_group1_and_group2(step, group1, group2):\n'
u' assert False, \'This step must be implemented\'\033[0m\n'
)
......@@ -828,7 +828,7 @@ def test_output_snippets_with_groups_within_redundant_quotes():
u"# -*- coding: utf-8 -*-\n"
u'from lettuce import step\n'
u'\n'
u'@step(u\'Given I have "(.*)" and "(.*)"\')\n'
u'@step(u\'Given I have "([^"]*)" and "([^"]*)"\')\n'
u'def given_i_have_group1_and_group2(step, group1, group2):\n'
u' assert False, \'This step must be implemented\'\n'
)
......@@ -864,7 +864,7 @@ def test_output_snippets_with_normalized_unicode_names():
u"@step(u'Dado que eu tenho palavrões e outras situações')\n"
u"def dado_que_eu_tenho_palavroes_e_outras_situacoes(step):\n"
u" assert False, 'This step must be implemented'\n"
u"@step(u'E várias palavras acentuadas são úteis, tais como: \"(.*)\"')\n"
u"@step(u'E várias palavras acentuadas são úteis, tais como: \"([^\"]*)\"')\n"
u"def e_varias_palavras_acentuadas_sao_uteis_tais_como_group1(step, group1):\n"
u" assert False, 'This step must be implemented'\n"
u"@step(u'Então eu fico felizão')\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment