Commit d80542cd by Gabriel Falcão

supporting comments

parent 7218ea0a
...@@ -602,6 +602,8 @@ class Scenario(object): ...@@ -602,6 +602,8 @@ class Scenario(object):
@classmethod @classmethod
def from_string(new_scenario, string, with_file=None, original_string=None, language=None): def from_string(new_scenario, string, with_file=None, original_string=None, language=None):
""" Creates a new scenario from string""" """ Creates a new scenario from string"""
# ignoring comments
string = "\n".join(strings.get_stripped_lines(string, ignore_lines_starting_with='#'))
if not language: if not language:
language = Language() language = Language()
...@@ -700,11 +702,11 @@ class Feature(object): ...@@ -700,11 +702,11 @@ class Feature(object):
@classmethod @classmethod
def from_string(new_feature, string, with_file=None, language=None): def from_string(new_feature, string, with_file=None, language=None):
"""Creates a new feature from string""" """Creates a new feature from string"""
lines = strings.get_stripped_lines(string) lines = strings.get_stripped_lines(string, ignore_lines_starting_with='#')
if not language: if not language:
language = Language() language = Language()
found = len(re.findall(r'%s:[ ]*\w+' % language.feature, string)) found = len(re.findall(r'%s:[ ]*\w+' % language.feature, "\n".join(lines)))
if found > 1: if found > 1:
raise LettuceSyntaxError( raise LettuceSyntaxError(
......
...@@ -25,10 +25,17 @@ def escape_if_necessary(what): ...@@ -25,10 +25,17 @@ def escape_if_necessary(what):
return what return what
def get_stripped_lines(string): def get_stripped_lines(string, ignore_lines_starting_with=''):
string = unicode(string) string = unicode(string)
lines = [unicode(l.strip()) for l in string.splitlines()] lines = [unicode(l.strip()) for l in string.splitlines()]
return filter(lambda x:x, lines) if ignore_lines_starting_with:
filter_func = lambda x: x and not x.startswith(ignore_lines_starting_with)
else:
filter_func = lambda x: x
lines = filter(filter_func, lines)
return lines
def split_wisely(string, sep, strip=False): def split_wisely(string, sep, strip=False):
string = unicode(string) string = unicode(string)
......
Feature: one commented scenario
Scenario: Do nothing
Given I do nothing
# Scenario: Do something
# Given I do something
# Then I see that the test passes
# -*- coding: utf-8 -*-
from lettuce import step
@step(u'Given I do nothing')
def given_i_do_nothing(step):
pass
@step(u'Then I see that the test passes')
def then_i_see_that_the_test_passes(step):
pass
...@@ -1028,3 +1028,18 @@ def test_output_level_1_error(): ...@@ -1028,3 +1028,18 @@ def test_output_level_1_error():
} }
) )
@with_setup(prepare_stdout)
def test_commented_scenario():
'Test one commented scenario'
runner = Runner(feature_name('commented_feature'), verbosity=1)
runner.run()
assert_stdout_lines(
"."
"\n"
"1 feature (1 passed)\n"
"1 scenario (1 passed)\n"
"1 step (1 passed)\n"
)
...@@ -130,6 +130,24 @@ Feature: Big scenario outline ...@@ -130,6 +130,24 @@ Feature: Big scenario outline
| 3 | | 3 |
""" """
FEATURE10 = """
Feature: Big sentence
As a clever guy
I want to describe this Feature
So that I can take care of my Scenario
Scenario: Regular numbers
Given a huge sentence, that have so many characters
And another one, very tiny
# Feature: Big sentence
# As a clever guy
# I want to describe this Feature
# So that I can take care of my Scenario
# Scenario: Regular numbers
# Given a huge sentence, that have so many characters
# And another one, very tiny
"""
def test_feature_has_repr(): def test_feature_has_repr():
"Feature implements __repr__ nicely" "Feature implements __repr__ nicely"
...@@ -283,3 +301,8 @@ def test_description_on_big_sentenced_steps(): ...@@ -283,3 +301,8 @@ def test_description_on_big_sentenced_steps():
"So that I can take care of my Scenario" "So that I can take care of my Scenario"
) )
def test_comments():
"It should ignore lines that start with #, despite white spaces"
feature = Feature.from_string(FEATURE10)
assert_equals(feature.max_length, 55)
...@@ -139,6 +139,27 @@ Scenario Outline: Add two numbers ...@@ -139,6 +139,27 @@ Scenario Outline: Add two numbers
| 12 | 40 | add | 52 | | 12 | 40 | add | 52 |
""" """
COMMENTED_SCENARIO = """
Scenario: Adding some students to my university database
Given I have the following courses in my university:
| Name | Duration |
| Computer Science | 5 years |
| Nutrition | 4 years |
When I consolidate the database into 'courses.txt'
Then I see the 1st line of 'courses.txt' has 'Computer Science:5'
And I see the 2nd line of 'courses.txt' has 'Nutrition:4'
# Scenario: Adding some students to my university database
# Given I have the following courses in my university:
# | Name | Duration |
# | Computer Science | 5 years |
# | Nutrition | 4 years |
# When I consolidate the database into 'courses.txt'
# Then I see the 1st line of 'courses.txt' has 'Computer Science:5'
# And I see the 2nd line of 'courses.txt' has 'Nutrition:4'
"""
from lettuce.core import Step from lettuce.core import Step
from lettuce.core import Scenario from lettuce.core import Scenario
from lettuce.core import Feature from lettuce.core import Feature
...@@ -370,3 +391,9 @@ def test_scenario_aggregate_all_examples_blocks(): ...@@ -370,3 +391,9 @@ def test_scenario_aggregate_all_examples_blocks():
{'input_1': '12', 'input_2': '40', 'button': 'add', 'output': '52'}, {'input_1': '12', 'input_2': '40', 'button': 'add', 'output': '52'},
] ]
) )
def test_commented_scenarios():
"A scenario string that contains lines starting with '#' will be commented"
scenario = Scenario.from_string(COMMENTED_SCENARIO)
assert_equals(scenario.name, u'Adding some students to my university database')
assert_equals(len(scenario.steps), 4)
...@@ -49,6 +49,22 @@ def test_get_stripped_lines(): ...@@ -49,6 +49,22 @@ def test_get_stripped_lines():
] ]
) )
def test_get_stripped_lines_ignore_comments():
"strings.get_stripped_lines ignore lines that start with some char"
my_string = '''
first line
# second line
'''
assert_equals(
strings.get_stripped_lines(my_string, ignore_lines_starting_with="#"),
[
'first line',
]
)
def test_split_wisely_splits_ignoring_case(): def test_split_wisely_splits_ignoring_case():
"strings.split_wisely splits ignoring case" "strings.split_wisely splits ignoring case"
my_string = 'first line\n' \ my_string = 'first line\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