| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
# pylint: disable=unused-wildcard-import |
| 3 |
from __future__ import absolute_import, with_statement |
| 4 |
from mock import Mock, patch |
| 5 |
from .tools import eq_ |
| 6 |
from six.moves import range # pylint: disable=redefined-builtin |
| 7 |
from behave import step_registry |
| 8 |
|
| 9 |
|
| 10 |
class TestStepRegistry(object): |
| 11 |
# pylint: disable=invalid-name, no-self-use |
| 12 |
|
| 13 |
def test_add_step_definition_adds_to_lowercased_keyword(self): |
| 14 |
registry = step_registry.StepRegistry() |
| 15 |
# -- MONKEYPATCH-PROBLEM: |
| 16 |
# with patch('behave.matchers.get_matcher') as get_matcher: |
| 17 |
with patch('behave.step_registry.get_matcher') as get_matcher: |
| 18 |
func = lambda x: -x |
| 19 |
pattern = 'just a test string' |
| 20 |
magic_object = object() |
| 21 |
get_matcher.return_value = magic_object |
| 22 |
|
| 23 |
for step_type in list(registry.steps.keys()): |
| 24 |
l = [] |
| 25 |
registry.steps[step_type] = l |
| 26 |
|
| 27 |
registry.add_step_definition(step_type.upper(), pattern, func) |
| 28 |
get_matcher.assert_called_with(func, pattern) |
| 29 |
eq_(l, [magic_object]) |
| 30 |
|
| 31 |
def test_find_match_with_specific_step_type_also_searches_generic(self): |
| 32 |
registry = step_registry.StepRegistry() |
| 33 |
|
| 34 |
given_mock = Mock() |
| 35 |
given_mock.match.return_value = None |
| 36 |
step_mock = Mock() |
| 37 |
step_mock.match.return_value = None |
| 38 |
|
| 39 |
registry.steps['given'].append(given_mock) |
| 40 |
registry.steps['step'].append(step_mock) |
| 41 |
|
| 42 |
step = Mock() |
| 43 |
step.step_type = 'given' |
| 44 |
step.name = 'just a test step' |
| 45 |
|
| 46 |
assert registry.find_match(step) is None |
| 47 |
|
| 48 |
given_mock.match.assert_called_with(step.name) |
| 49 |
step_mock.match.assert_called_with(step.name) |
| 50 |
|
| 51 |
def test_find_match_with_no_match_returns_none(self): |
| 52 |
registry = step_registry.StepRegistry() |
| 53 |
|
| 54 |
step_defs = [Mock() for x in range(0, 10)] |
| 55 |
for mock in step_defs: |
| 56 |
mock.match.return_value = None |
| 57 |
|
| 58 |
registry.steps['when'] = step_defs |
| 59 |
|
| 60 |
step = Mock() |
| 61 |
step.step_type = 'when' |
| 62 |
step.name = 'just a test step' |
| 63 |
|
| 64 |
assert registry.find_match(step) is None |
| 65 |
|
| 66 |
def test_find_match_with_a_match_returns_match(self): |
| 67 |
registry = step_registry.StepRegistry() |
| 68 |
|
| 69 |
step_defs = [Mock() for x in range(0, 10)] |
| 70 |
for mock in step_defs: |
| 71 |
mock.match.return_value = None |
| 72 |
magic_object = object() |
| 73 |
step_defs[5].match.return_value = magic_object |
| 74 |
|
| 75 |
registry.steps['then'] = step_defs |
| 76 |
|
| 77 |
step = Mock() |
| 78 |
step.step_type = 'then' |
| 79 |
step.name = 'just a test step' |
| 80 |
|
| 81 |
assert registry.find_match(step) is magic_object |
| 82 |
for mock in step_defs[6:]: |
| 83 |
eq_(mock.match.call_count, 0) |
| 84 |
|
| 85 |
# pylint: disable=line-too-long |
| 86 |
@patch.object(step_registry.registry, 'add_step_definition') |
| 87 |
def test_make_step_decorator_ends_up_adding_a_step_definition(self, add_step_definition): |
| 88 |
step_type = object() |
| 89 |
step_pattern = object() |
| 90 |
func = object() |
| 91 |
|
| 92 |
decorator = step_registry.registry.make_decorator(step_type) |
| 93 |
wrapper = decorator(step_pattern) |
| 94 |
assert wrapper(func) is func |
| 95 |
add_step_definition.assert_called_with(step_type, step_pattern, func) |
| 96 |
|