Commit ccb95004 by Gabriel Falcao

implementing basic output of backgrounds

parent b7ea4049
......@@ -15,13 +15,16 @@
# 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 random as rand
import re
import codecs
from fuzzywuzzy import fuzz
import unicodedata
from itertools import chain
from copy import deepcopy
from fuzzywuzzy import fuzz
from itertools import chain
from random import shuffle
from lettuce import strings
from lettuce import languages
from lettuce.fs import FileSystem
......@@ -181,6 +184,7 @@ class FeatureDescription(object):
self.line = None
described_at = []
description_lines = strings.get_stripped_lines(feature.description)
for pline, part in enumerate(lines):
part = part.strip()
line = pline + 1
......@@ -205,6 +209,8 @@ class Step(object):
passed = None
failed = None
related_outline = None
scenario = None
background = None
def __init__(self, sentence, remaining_lines, line=None, filename=None):
self.sentence = sentence
......@@ -301,12 +307,17 @@ class Step(object):
return max_length
@property
def parent(self):
return self.scenario or self.background
def represent_string(self, string):
head = ' ' * self.indentation + string
where = self.described_at
if self.defined_at:
where = self.defined_at
return strings.rfill(head, self.scenario.feature.max_length + 1, append=u'# %s:%d\n' % (where.file, where.line))
return strings.rfill(head, self.parent.feature.max_length + 1, append=u'# %s:%d\n' % (where.file, where.line))
def represent_hashes(self):
lines = strings.dicts_to_string(self.hashes, self.keys).splitlines()
......@@ -834,17 +845,54 @@ class Scenario(object):
class Background(object):
indentation = 2
def __init__(self, lines, feature,
with_file=None,
original_string=None,
language=None):
self.steps = Step.many_from_lines(lines, with_file, original_string)
self.steps = map(self.add_self_to_step, Step.many_from_lines(
lines, with_file, original_string))
self.feature = feature
self.original_string = original_string
self.language = language
def add_self_to_step(self, step):
step.background = self
return step
def run(self, ignore_case):
return [s.run(ignore_case) for s in self.steps]
call_hook('before_each', 'background', self)
results = []
for step in self.steps:
call_hook('before_each', 'step', step)
try:
results.append(step.run(ignore_case))
except Exception, e:
print e
pass
call_hook('after_each', 'step', step)
call_hook('after_each', 'background', self, results)
return results
def __repr__(self):
return '<Background for feature: {0}>'.format(self.feature.name)
@property
def max_length(self):
max_length = 0
for step in self.steps:
if step.max_length > max_length:
max_length = step.max_length
return max_length
def represented(self):
return ((' ' * self.indentation) + 'Background:')
@classmethod
def from_string(new_background,
......@@ -1065,7 +1113,7 @@ class Feature(object):
scenarios_ran = []
if random:
rand.shuffle(self.scenarios)
shuffle(self.scenarios)
if isinstance(scenarios, (tuple, list)):
if all(map(lambda x: isinstance(x, int), scenarios)):
......
......@@ -52,6 +52,13 @@ def print_scenario_running(scenario):
wrt(scenario.represented())
@before.each_background
def print_background_running(background):
wrt('\n')
wrt(background.represented())
wrt('\n')
@after.outline
def print_outline(scenario, order, outline, reasons_to_fail):
table = strings.dicts_to_string(scenario.outlines, scenario.keys)
......
......@@ -54,6 +54,10 @@ CALLBACK_REGISTRY = CallbackDict(
'after_each': [],
'outline': [],
},
'background': {
'before_each': [],
'after_each': [],
},
'feature': {
'before_each': [],
'after_each': [],
......
......@@ -96,7 +96,7 @@ def getlen(string):
def dicts_to_string(dicts, order):
escape = "#{%s}" % str(time.time())
escape = "#{%s}" % unicode(time.time())
def enline(line):
return unicode(line).replace("|", escape)
......@@ -133,7 +133,7 @@ def dicts_to_string(dicts, order):
def parse_hashes(lines):
escape = "#{%s}" % str(time.time())
escape = "#{%s}" % unicode(time.time())
def enline(line):
return unicode(line.replace("\\|", escape)).strip()
......
......@@ -54,6 +54,7 @@ for name, where, when in (
('all', 'all', '%(0)s'),
('each_step', 'step', '%(0)s_each'),
('each_scenario', 'scenario', '%(0)s_each'),
('each_background', 'background', '%(0)s_each'),
('each_feature', 'feature', '%(0)s_each'),
('harvest', 'harvest', '%(0)s'),
('each_app', 'app', '%(0)s_each'),
......
Feature: Simple and successful
As the Lettuce maintainer
In order to make sure the output is pretty
I want to automate its test
Background:
Given the variable "X" holds 2
Scenario: multiplication changing the value
Given the variable "X" is equal to 2
# -*- coding: utf-8 -*-
from lettuce import step
@step(u'Given the variable "([^"]*)" is equal to 2')
def given_the_variable_group1_is_equal_to_2(step, group1):
pass
@step(u'When the variable "([^"]*)" holds 10')
def when_the_variable_group1_holds_10(step, group1):
pass
@step(u'Then the variable "([^"]*)" times 5 is equal to 50')
def then_the_variable_group1_times_5_is_equal_to_50(step, group1):
pass
@step(u'And the variable "([^"]*)" is equal to 10')
def and_the_variable_group1_is_equal_to_10(step, group1):
pass
@step(u'Then the variable "([^"]*)" times 5 is equal to 10')
def then_the_variable_group1_times_5_is_equal_to_10(step, group1):
pass
@step(u'And the variable "([^"]*)" is equal to 2')
def and_the_variable_group1_is_equal_to_2(step, group1):
pass
......@@ -14,16 +14,16 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from lettuce import core
from sure import expect
from nose.tools import assert_equals
from nose.tools import assert_not_equals
STEP_WITH_TABLE = u'''
Given I have the following items in my shelf:
| name | description |
| Glass | a nice glass to drink grape juice |
| Pasta | a pasta to cook and eat with grape juice in the glass |
| name | description |
| Glass | a nice glass to drink grape juice |
| Pasta | a pasta to cook and eat with grape juice in the glass |
'''
......
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