Skip to content

Commit

Permalink
release 0.1.11, fixing escaped pipes (closes gabrielfalcao#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfalcao committed Aug 17, 2010
1 parent 431417a commit 25ed10f
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,5 +1,5 @@
# lettuce
> Version 0.1.10 - barium
> Version 0.1.11 - barium
## On release names

Expand Down
2 changes: 1 addition & 1 deletion lettuce/__init__.py
Expand Up @@ -15,7 +15,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

version = '0.1.10'
version = '0.1.11'
release = 'barium'

import os
Expand Down
18 changes: 2 additions & 16 deletions lettuce/core.py
Expand Up @@ -30,20 +30,6 @@

fs = FileSystem()

def parse_hashes(lines):
lines = map(unicode, lines)
keys = []
hashes = []
if lines:
first_line = lines.pop(0)
keys = strings.split_wisely(first_line, u"|", True)

for line in lines:
values = strings.split_wisely(line, u"|", True)
hashes.append(dict(zip(keys, values)))

return keys, hashes

class Language(object):
code = 'en'
name = 'English'
Expand Down Expand Up @@ -264,7 +250,7 @@ def __repr__(self):
return u'<Step: "%s">' % self.sentence

def _parse_remaining_lines(self, lines):
return parse_hashes(lines)
return strings.parse_hashes(lines)

def _get_match(self, ignore_case):
matched, func = None, lambda: None
Expand Down Expand Up @@ -531,7 +517,7 @@ def from_string(new_scenario, string, with_file=None, original_string=None, lang
outlines = []
if len(splitted) > 1:
part = splitted[-1]
keys, outlines = parse_hashes(strings.get_stripped_lines(part))
keys, outlines = strings.parse_hashes(strings.get_stripped_lines(part))

lines = strings.get_stripped_lines(string)
scenario_line = lines.pop(0)
Expand Down
37 changes: 34 additions & 3 deletions lettuce/strings.py
Expand Up @@ -14,7 +14,9 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import re
import time

def escape_if_necessary(what):
what = unicode(what)
Expand Down Expand Up @@ -63,6 +65,12 @@ def getlen(string):
return len(string) + 1

def dicts_to_string(dicts, order):
escape = "#{%s}" % str(time.time())
def enline(line):
return unicode(line).replace("|", escape)
def deline(line):
return line.replace(escape, '\\|')

keys_and_sizes = dict([(k, getlen(k)) for k in dicts[0].keys()])
for key in keys_and_sizes:
for data in dicts:
Expand All @@ -76,16 +84,39 @@ def dicts_to_string(dicts, order):
for key in order:
size = keys_and_sizes[key]
name = u" %s" % rfill(key, size)
names.append(name)
names.append(enline(name))

table = [u"|%s|" % "|".join(names)]
for data in dicts:
names = []
for key in order:
value = data[key]
size = keys_and_sizes[key]
names.append(u" %s" % rfill(unicode(value), size))
names.append(enline(u" %s" % rfill(value, size)))

table.append(u"|%s|" % "|".join(names))

return u"\n".join(table) + u"\n"
return deline(u"\n".join(table) + u"\n")

def parse_hashes(lines):
escape = "#{%s}" % str(time.time())
def enline(line):
return unicode(line.replace("\\|", escape)).strip()
def deline(line):
return line.replace(escape, '|')

lines = map(enline, lines)

keys = []
hashes = []
if lines:
first_line = lines.pop(0)
keys = split_wisely(first_line, u"|", True)
keys = map(deline, keys)

for line in lines:
values = split_wisely(line, u"|", True)
values = map(deline, values)
hashes.append(dict(zip(keys, values)))

return keys, hashes
Expand Up @@ -11,10 +11,10 @@ Feature: Successful Scenario Outline
And I fill the field "password-confirm" with "<password>"
And I fill the field "email" with "<email>"
And I click "done"
Then I see the message "<message>"
Then I see the title of the page is "<title>"

Examples:
| username | password | email | message |
| john | doe-1234 | john@gmail.org | Welcome, John |
| mary | wee-9876 | mary@email.com | Welcome, Mary |
| foo | foo-bar | foo@bar.com | Welcome, Foo |
| username | password | email | title |
| john | doe-1234 | john@gmail.org | John \| My Website |
| mary | wee-9876 | mary@email.com | Mary \| My Website |
| foo | foo-bar | foo@bar.com | Foo \| My Website |
Expand Up @@ -33,6 +33,13 @@ def when_i_fill_the_field_x_with_y(step, field, value):
def and_i_click_done(step):
pass

@step(r'Then I see the message "(.*)"')
def then_i_see_the_message_message(step, message):
pass
@step(r'I see the title of the page is "(.*)"')
def then_i_see_the_message_message(step, title):
possible_titles = [
u'John | My Website',
u'Mary | My Website',
u'Foo | My Website',
]

assert title in possible_titles, \
'"%s" should be between the options [%s]' % (title, ", ".join(possible_titles))
20 changes: 10 additions & 10 deletions tests/functional/test_runner.py
Expand Up @@ -464,13 +464,13 @@ def test_output_with_successful_outline_colorless():
' And I fill the field "password-confirm" with "<password>" # tests/functional/output_features/success_outline/success_outline_steps.py:29\n'
' And I fill the field "email" with "<email>" # tests/functional/output_features/success_outline/success_outline_steps.py:29\n'
' And I click "done" # tests/functional/output_features/success_outline/success_outline_steps.py:33\n'
' Then I see the message "<message>" # tests/functional/output_features/success_outline/success_outline_steps.py:37\n'
' Then I see the title of the page is "<title>" # tests/functional/output_features/success_outline/success_outline_steps.py:37\n'
'\n'
' Examples:\n'
' | username | password | email | message |\n'
' | john | doe-1234 | john@gmail.org | Welcome, John |\n'
' | mary | wee-9876 | mary@email.com | Welcome, Mary |\n'
' | foo | foo-bar | foo@bar.com | Welcome, Foo |\n'
' | username | password | email | title |\n'
' | john | doe-1234 | john@gmail.org | John \| My Website |\n'
' | mary | wee-9876 | mary@email.com | Mary \| My Website |\n'
' | foo | foo-bar | foo@bar.com | Foo \| My Website |\n'
'\n'
'1 feature (1 passed)\n'
'3 scenarios (3 passed)\n'
Expand Down Expand Up @@ -499,13 +499,13 @@ def test_output_with_successful_outline_colorful():
'\033[0;36m And I fill the field "password-confirm" with "<password>" \033[1;30m# tests/functional/output_features/success_outline/success_outline_steps.py:29\033[0m\n'
'\033[0;36m And I fill the field "email" with "<email>" \033[1;30m# tests/functional/output_features/success_outline/success_outline_steps.py:29\033[0m\n'
'\033[0;36m And I click "done" \033[1;30m# tests/functional/output_features/success_outline/success_outline_steps.py:33\033[0m\n'
'\033[0;36m Then I see the message "<message>" \033[1;30m# tests/functional/output_features/success_outline/success_outline_steps.py:37\033[0m\n'
'\033[0;36m Then I see the title of the page is "<title>" \033[1;30m# tests/functional/output_features/success_outline/success_outline_steps.py:37\033[0m\n'
'\n'
'\033[1;37m Examples:\033[0m\n'
'\033[0;36m \033[1;37m |\033[0;36m username\033[1;37m |\033[0;36m password\033[1;37m |\033[0;36m email \033[1;37m |\033[0;36m message \033[1;37m |\033[0;36m\033[0m\n'
'\033[1;32m \033[1;37m |\033[1;32m john \033[1;37m |\033[1;32m doe-1234\033[1;37m |\033[1;32m john@gmail.org\033[1;37m |\033[1;32m Welcome, John\033[1;37m |\033[1;32m\033[0m\n'
'\033[1;32m \033[1;37m |\033[1;32m mary \033[1;37m |\033[1;32m wee-9876\033[1;37m |\033[1;32m mary@email.com\033[1;37m |\033[1;32m Welcome, Mary\033[1;37m |\033[1;32m\033[0m\n'
'\033[1;32m \033[1;37m |\033[1;32m foo \033[1;37m |\033[1;32m foo-bar \033[1;37m |\033[1;32m foo@bar.com \033[1;37m |\033[1;32m Welcome, Foo \033[1;37m |\033[1;32m\033[0m\n'
'\033[0;36m \033[1;37m |\033[0;36m username\033[1;37m |\033[0;36m password\033[1;37m |\033[0;36m email \033[1;37m |\033[0;36m title \033[1;37m |\033[0;36m\033[0m\n'
'\033[1;32m \033[1;37m |\033[1;32m john \033[1;37m |\033[1;32m doe-1234\033[1;37m |\033[1;32m john@gmail.org\033[1;37m |\033[1;32m John \| My Website\033[1;37m |\033[1;32m\033[0m\n'
'\033[1;32m \033[1;37m |\033[1;32m mary \033[1;37m |\033[1;32m wee-9876\033[1;37m |\033[1;32m mary@email.com\033[1;37m |\033[1;32m Mary \| My Website\033[1;37m |\033[1;32m\033[0m\n'
'\033[1;32m \033[1;37m |\033[1;32m foo \033[1;37m |\033[1;32m foo-bar \033[1;37m |\033[1;32m foo@bar.com \033[1;37m |\033[1;32m Foo \| My Website \033[1;37m |\033[1;32m\033[0m\n'
'\n'
"\033[1;37m1 feature (\033[1;32m1 passed\033[1;37m)\033[0m\n" \
"\033[1;37m3 scenarios (\033[1;32m3 passed\033[1;37m)\033[0m\n" \
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_main.py
Expand Up @@ -21,7 +21,7 @@

def test_has_version():
"A nice python module is supposed to have a version"
assert_equals(lettuce.version, '0.1.10')
assert_equals(lettuce.version, '0.1.11')

def test_import():
"lettuce importer does import"
Expand Down
80 changes: 78 additions & 2 deletions tests/unit/test_strings.py
Expand Up @@ -148,7 +148,7 @@ def test_dicts_to_string():
'age': 22
},
{
'name': 'Laryssa',
'name': 'Miguel',
'age': 19
}

Expand All @@ -158,6 +158,82 @@ def test_dicts_to_string():
strings.dicts_to_string(dicts, ['name', 'age']),
u"| name | age |\n"
u"| Gabriel Falcão | 22 |\n"
u"| Laryssa | 19 |\n"
u"| Miguel | 19 |\n"
)

def test_dicts_to_string_escapes_pipe():
"strings.dicts_to_string escapes pipe"

dicts = [
{
'name': u'Gabriel | Falcão',
'age': 22
},
{
'name': 'Miguel | Arcanjo',
'age': 19
}

]

assert_equals(
strings.dicts_to_string(dicts, ['name', 'age']),
u"| name | age |\n"
u"| Gabriel \\| Falcão | 22 |\n"
u"| Miguel \\| Arcanjo | 19 |\n"
)

def test_parse_hashes():
"strings.parse_hashes"

keys = [u'name', u'age']
dicts = [
{
u'name': u'Gabriel Falcão',
u'age': u'22'
},
{
u'name': u'Miguel',
u'age': u'33'
}

]

table = [
u"| name | age |\n",
u"| Gabriel Falcão | 22 |\n",
u"| Miguel | 33 |\n",
]

got_keys, got_dicts = strings.parse_hashes(table)

assert_equals(keys, got_keys)
assert_equals(dicts, got_dicts)

def test_parse_hashes_escapes_pipes():
"strings.parse_hashes escapes pipe"

keys = [u'name', u'age']
dicts = [
{
u'name': u'Gabriel | Falcão',
u'age': u'22'
},
{
u'name': u'Miguel | Silva',
u'age': u'33'
}

]

table = [
u"| name | age |\n",
u"| Gabriel \| Falcão | 22 |\n",
u"| Miguel \| Silva | 33 |\n",
]

got_keys, got_dicts = strings.parse_hashes(table)

assert_equals(keys, got_keys)
assert_equals(dicts, got_dicts)

0 comments on commit 25ed10f

Please sign in to comment.