Commit 70d06603 by Grégory Salvan

ensure callable are functions or methods for load_steps

parent 27e0db17
...@@ -59,6 +59,7 @@ class StepDict(dict): ...@@ -59,6 +59,7 @@ class StepDict(dict):
return obj return obj
def _extract_sentence(self, func): def _extract_sentence(self, func):
func = getattr(func, '__func__', func)
sentence = getattr(func, '__doc__', None) sentence = getattr(func, '__doc__', None)
if sentence is None: if sentence is None:
sentence = func.func_name.replace('_', ' ') sentence = func.func_name.replace('_', ' ')
...@@ -75,7 +76,11 @@ class StepDict(dict): ...@@ -75,7 +76,11 @@ class StepDict(dict):
" error: %s" % (step, func, e)) " error: %s" % (step, func, e))
def _attr_is_step(self, attr, obj): def _attr_is_step(self, attr, obj):
return attr[0] != '_' and callable(getattr(obj, attr)) return attr[0] != '_' and self._is_func_or_method(getattr(obj, attr))
def _is_func_or_method(self, func):
func_dir = dir(func)
return callable(func) and ("func_name" in func_dir or "__func__" in func_dir)
STEP_REGISTRY = StepDict() STEP_REGISTRY = StepDict()
......
...@@ -105,7 +105,7 @@ def test_StepDict_can_load_steps_from_an_object(): ...@@ -105,7 +105,7 @@ def test_StepDict_can_load_steps_from_an_object():
assert_equal(steps[expected_sentence2], step_list.step_2) assert_equal(steps[expected_sentence2], step_list.step_2)
def test_StepDict_can_exclude_methods_when_load_steps(): def test_StepDict_can_exclude_methods_when_load_steps():
u"lettuce.STEP_REGISTRY.load_steps(obj) append all obj methods to STEP_REGISTRY except ones in attr 'exclude'" u"lettuce.STEP_REGISTRY.load_steps(obj) don't load exluded attr in STEP_REGISTRY"
steps = StepDict() steps = StepDict()
class LotsOfSteps: class LotsOfSteps:
exclude = ["step_1"] exclude = ["step_1"]
...@@ -122,3 +122,16 @@ def test_StepDict_can_exclude_methods_when_load_steps(): ...@@ -122,3 +122,16 @@ def test_StepDict_can_exclude_methods_when_load_steps():
expected_sentence2 = "Doing something" expected_sentence2 = "Doing something"
assert (expected_sentence1 not in steps) assert (expected_sentence1 not in steps)
assert (expected_sentence2 in steps) assert (expected_sentence2 in steps)
def test_StepDict_can_exclude_callable_object_when_load_steps():
u"lettuce.STEP_REGISTRY.load_steps(obj) don't load callable objets in STEP_REGISTRY"
steps = StepDict()
class NoStep:
class NotAStep(object):
def __call__(self):
pass
no_step = NoStep()
steps.load_steps(no_step)
assert len(steps) == 0
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