Debian Codegraph @

test/test_matchers.py

raw ◀ ▶
2 Debian patches touch this file
1 # -*- coding: UTF-8 -*-
2 from __future__ import absolute_import, with_statement
•3 from unittest.mock import Mock, patch
•4 from .tools import eq_, raises
5 import parse
6 from behave.matchers import Match, Matcher, ParseMatcher, RegexMatcher, \
7 SimplifiedRegexMatcher, CucumberRegexMatcher
8 from behave import matchers, runner
9
10
11 class DummyMatcher(Matcher):
12 desired_result = None
13
14 def check_match(self, step):
15 return DummyMatcher.desired_result
16
17 class TestMatcher(object):
18 # pylint: disable=invalid-name, no-self-use
19
20 def setUp(self):
21 DummyMatcher.desired_result = None
22
23 def test_returns_none_if_check_match_returns_none(self):
24 matcher = DummyMatcher(None, None)
25 assert matcher.match('just a random step') is None
26
27 def test_returns_match_object_if_check_match_returns_arguments(self):
28 arguments = ['some', 'random', 'objects']
29 func = lambda x: -x
30
31 DummyMatcher.desired_result = arguments
32 matcher = DummyMatcher(func, None)
33
34 match = matcher.match('just a random step')
35 assert isinstance(match, Match)
36 assert match.func is func
37 assert match.arguments == arguments
38
39 class TestParseMatcher(object):
40 # pylint: disable=invalid-name, no-self-use
41
42 def setUp(self):
43 self.recorded_args = None
44
45 def record_args(self, *args, **kwargs):
46 self.recorded_args = (args, kwargs)
47
48 def test_returns_none_if_parser_does_not_match(self):
49 # pylint: disable=redefined-outer-name
50 # REASON: parse
51 matcher = ParseMatcher(None, 'a string')
52 with patch.object(matcher.parser, 'parse') as parse:
53 parse.return_value = None
54 assert matcher.match('just a random step') is None
55
56 def test_returns_arguments_based_on_matches(self):
57 func = lambda x: -x
58 matcher = ParseMatcher(func, 'foo')
59
60 results = parse.Result([1, 2, 3], {'foo': 'bar', 'baz': -45.3},
61 {
62 0: (13, 14),
63 1: (16, 17),
64 2: (22, 23),
65 'foo': (32, 35),
66 'baz': (39, 44),
67 })
68
69 expected = [
70 (13, 14, '1', 1, None),
71 (16, 17, '2', 2, None),
72 (22, 23, '3', 3, None),
73 (32, 35, 'bar', 'bar', 'foo'),
74 (39, 44, '-45.3', -45.3, 'baz'),
75 ]
76
77 with patch.object(matcher.parser, 'parse') as p:
78 p.return_value = results
79 m = matcher.match('some numbers 1, 2 and 3 and the bar is -45.3')
80 assert m.func is func
81 args = m.arguments
82 have = [(a.start, a.end, a.original, a.value, a.name) for a in args]
83 eq_(have, expected)
84
85 def test_named_arguments(self):
86 text = "has a {string}, an {integer:d} and a {decimal:f}"
87 matcher = ParseMatcher(self.record_args, text)
88 context = runner.Context(Mock())
89
90 m = matcher.match("has a foo, an 11 and a 3.14159")
91 m.run(context)
92 eq_(self.recorded_args, ((context,), {
93 'string': 'foo',
94 'integer': 11,
95 'decimal': 3.14159
96 }))
97
98 def test_positional_arguments(self):
99 text = "has a {}, an {:d} and a {:f}"
100 matcher = ParseMatcher(self.record_args, text)
101 context = runner.Context(Mock())
102
103 m = matcher.match("has a foo, an 11 and a 3.14159")
104 m.run(context)
105 eq_(self.recorded_args, ((context, 'foo', 11, 3.14159), {}))
106
107 class TestRegexMatcher(object):
108 # pylint: disable=invalid-name, no-self-use
109 MATCHER_CLASS = RegexMatcher
110
111 def test_returns_none_if_regex_does_not_match(self):
112 RegexMatcher = self.MATCHER_CLASS
113 matcher = RegexMatcher(None, 'a string')
114 regex = Mock()
115 regex.match.return_value = None
116 matcher.regex = regex
117 assert matcher.match('just a random step') is None
118
119 def test_returns_arguments_based_on_groups(self):
120 RegexMatcher = self.MATCHER_CLASS
121 func = lambda x: -x
122 matcher = RegexMatcher(func, 'foo')
123
124 regex = Mock()
125 regex.groupindex = {'foo': 4, 'baz': 5}
126
127 match = Mock()
128 match.groups.return_value = ('1', '2', '3', 'bar', '-45.3')
129 positions = {
130 1: (13, 14),
131 2: (16, 17),
132 3: (22, 23),
133 4: (32, 35),
134 5: (39, 44),
135 }
136 match.start.side_effect = lambda idx: positions[idx][0]
137 match.end.side_effect = lambda idx: positions[idx][1]
138
139 regex.match.return_value = match
140 matcher.regex = regex
141
142 expected = [
143 (13, 14, '1', '1', None),
144 (16, 17, '2', '2', None),
145 (22, 23, '3', '3', None),
146 (32, 35, 'bar', 'bar', 'foo'),
147 (39, 44, '-45.3', '-45.3', 'baz'),
148 ]
149
150 m = matcher.match('some numbers 1, 2 and 3 and the bar is -45.3')
151 assert m.func is func
152 args = m.arguments
153 have = [(a.start, a.end, a.original, a.value, a.name) for a in args]
154 eq_(have, expected)
155
156
157
158 class TestSimplifiedRegexMatcher(TestRegexMatcher):
159 MATCHER_CLASS = SimplifiedRegexMatcher
160
161 def test_steps_with_same_prefix_are_not_ordering_sensitive(self):
162 # -- RELATED-TO: issue #280
163 # pylint: disable=unused-argument
164 def step_func1(context): pass # pylint: disable=multiple-statements
165 def step_func2(context): pass # pylint: disable=multiple-statements
166 # pylint: enable=unused-argument
167 matcher1 = SimplifiedRegexMatcher(step_func1, "I do something")
168 matcher2 = SimplifiedRegexMatcher(step_func2, "I do something more")
169
170 # -- CHECK: ORDERING SENSITIVITY
171 matched1 = matcher1.match(matcher2.pattern)
172 matched2 = matcher2.match(matcher1.pattern)
173 assert matched1 is None
174 assert matched2 is None
175
176 # -- CHECK: Can match itself (if step text is simple)
177 matched1 = matcher1.match(matcher1.pattern)
178 matched2 = matcher2.match(matcher2.pattern)
179 assert isinstance(matched1, Match)
180 assert isinstance(matched2, Match)
181
182 @raises(AssertionError)
183 def test_step_should_not_use_regex_begin_marker(self):
184 SimplifiedRegexMatcher(None, "^I do something")
185
186 @raises(AssertionError)
187 def test_step_should_not_use_regex_end_marker(self):
188 SimplifiedRegexMatcher(None, "I do something$")
189
190 @raises(AssertionError)
191 def test_step_should_not_use_regex_begin_and_end_marker(self):
192 SimplifiedRegexMatcher(None, "^I do something$")
193
194
195 class TestCucumberRegexMatcher(TestRegexMatcher):
196 MATCHER_CLASS = CucumberRegexMatcher
197
198 def test_steps_with_same_prefix_are_not_ordering_sensitive(self):
199 # -- RELATED-TO: issue #280
200 # pylint: disable=unused-argument
201 def step_func1(context): pass # pylint: disable=multiple-statements
202 def step_func2(context): pass # pylint: disable=multiple-statements
203 # pylint: enable=unused-argument
204 matcher1 = CucumberRegexMatcher(step_func1, "^I do something$")
205 matcher2 = CucumberRegexMatcher(step_func2, "^I do something more$")
206
207 # -- CHECK: ORDERING SENSITIVITY
208 matched1 = matcher1.match(matcher2.pattern[1:-1])
209 matched2 = matcher2.match(matcher1.pattern[1:-1])
210 assert matched1 is None
211 assert matched2 is None
212
213 # -- CHECK: Can match itself (if step text is simple)
214 matched1 = matcher1.match(matcher1.pattern[1:-1])
215 matched2 = matcher2.match(matcher2.pattern[1:-1])
216 assert isinstance(matched1, Match)
217 assert isinstance(matched2, Match)
218
219 def test_step_should_use_regex_begin_marker(self):
220 CucumberRegexMatcher(None, "^I do something")
221
222 def test_step_should_use_regex_end_marker(self):
223 CucumberRegexMatcher(None, "I do something$")
224
225 def test_step_should_use_regex_begin_and_end_marker(self):
226 CucumberRegexMatcher(None, "^I do something$")
227
228
229 def test_step_matcher_current_matcher():
230 current_matcher = matchers.current_matcher
231 for name, klass in list(matchers.matcher_mapping.items()):
232 matchers.use_step_matcher(name)
233 matcher = matchers.get_matcher(lambda x: -x, 'foo')
234 assert isinstance(matcher, klass)
235
236 matchers.current_matcher = current_matcher