Debian Codegraph @

test/test_model.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # -*- coding: utf-8 -*-
2 # pylint: disable=no-self-use, line-too-long
3
4 from __future__ import absolute_import, print_function, with_statement
5 import unittest
6 from mock import Mock, patch
7 from nose.tools import * # pylint: disable=wildcard-import, unused-wildcard-import
8 import six
9 from six.moves import range # pylint: disable=redefined-builtin
10 from six.moves import zip # pylint: disable=redefined-builtin
11 if six.PY2:
12 import traceback2 as traceback
13 traceback_modname = "traceback2"
14 else:
15 import traceback
16 traceback_modname = "traceback"
17
18 from behave.model_core import FileLocation, Status
19 from behave.model import Feature, Scenario, ScenarioOutline, Step
20 from behave.model import Table, Row
21 from behave.matchers import NoMatch
22 from behave.runner import Context
23 from behave.capture import CaptureController
24 from behave.configuration import Configuration
25 from behave.compat.collections import OrderedDict
26 from behave import step_registry
27
28
29 # -- CONVENIENCE-ALIAS:
30 _text = six.text_type
31
32
33 class TestFeatureRun(unittest.TestCase):
34 # pylint: disable=invalid-name
35
36 def setUp(self):
37 self.runner = Mock()
38 self.runner.aborted = False
39 self.runner.feature.tags = []
40 self.config = self.runner.config = Mock()
41 self.context = self.runner.context = Mock()
42 self.formatters = self.runner.formatters = [Mock()]
43 self.run_hook = self.runner.run_hook = Mock()
44
45 def test_formatter_feature_called(self):
46 feature = Feature('foo.feature', 1, u'Feature', u'foo',
47 background=Mock())
48
49 feature.run(self.runner)
50
51 self.formatters[0].feature.assert_called_with(feature)
52
53 def test_formatter_background_called_when_feature_has_background(self):
54 feature = Feature('foo.feature', 1, u'Feature', u'foo',
55 background=Mock())
56
57 feature.run(self.runner)
58
59 self.formatters[0].background.assert_called_with(feature.background)
60
61 def test_formatter_background_not_called_when_feature_has_no_background(self):
62 feature = Feature('foo.feature', 1, u'Feature', u'foo')
63
64 feature.run(self.runner)
65
66 assert not self.formatters[0].background.called
67
68 def test_run_runs_scenarios(self):
69 scenarios = [Mock(), Mock()]
70 for scenario in scenarios:
71 scenario.tags = []
72 scenario.run.return_value = False
73
74 self.config.tags.check.return_value = True # pylint: disable=no-member
75 self.config.name = []
76
77 feature = Feature('foo.feature', 1, u'Feature', u'foo',
78 scenarios=scenarios)
79
80 feature.run(self.runner)
81
82 for scenario in scenarios:
83 scenario.run.assert_called_with(self.runner)
84
85 def test_run_runs_named_scenarios(self):
86 scenarios = [Mock(Scenario), Mock(Scenario)]
87 scenarios[0].name = 'first scenario'
88 scenarios[1].name = 'second scenario'
89 scenarios[0].tags = []
90 scenarios[1].tags = []
91 # -- FAKE-CHECK:
92 scenarios[0].should_run_with_name_select.return_value = True
93 scenarios[1].should_run_with_name_select.return_value = False
94
95 for scenario in scenarios:
96 scenario.run.return_value = False
97
98 self.config.tags.check.return_value = True # pylint: disable=no-member
99 self.config.name = ['first', 'third']
100 self.config.name_re = Configuration.build_name_re(self.config.name)
101
102 feature = Feature('foo.feature', 1, u'Feature', u'foo',
103 scenarios=scenarios)
104
105 feature.run(self.runner)
106
107 scenarios[0].run.assert_called_with(self.runner)
108 assert not scenarios[1].run.called
109 scenarios[0].should_run_with_name_select.assert_called_with(self.config)
110 scenarios[1].should_run_with_name_select.assert_called_with(self.config)
111
112 def test_run_runs_named_scenarios_with_regexp(self):
113 scenarios = [Mock(), Mock()]
114 scenarios[0].name = 'first scenario'
115 scenarios[1].name = 'second scenario'
116 scenarios[0].tags = []
117 scenarios[1].tags = []
118 # -- FAKE-CHECK:
119 scenarios[0].should_run_with_name_select.return_value = False
120 scenarios[1].should_run_with_name_select.return_value = True
121
122 for scenario in scenarios:
123 scenario.run.return_value = False
124
125 self.config.tags.check.return_value = True # pylint: disable=no-member
126 self.config.name = ['third .*', 'second .*']
127 self.config.name_re = Configuration.build_name_re(self.config.name)
128
129 feature = Feature('foo.feature', 1, u'Feature', u'foo',
130 scenarios=scenarios)
131
132 feature.run(self.runner)
133
134 assert not scenarios[0].run.called
135 scenarios[1].run.assert_called_with(self.runner)
136 scenarios[0].should_run_with_name_select.assert_called_with(self.config)
137 scenarios[1].should_run_with_name_select.assert_called_with(self.config)
138
139 # @prepared
140 def test_run_exclude_named_scenarios_with_regexp(self):
141 # -- NOTE: Works here only because it is run against Mocks.
142 scenarios = [Mock(), Mock(), Mock()]
143 scenarios[0].name = "Alice in Florida"
144 scenarios[1].name = "Alice and Bob"
145 scenarios[2].name = "Bob in Paris"
146 scenarios[0].tags = []
147 scenarios[1].tags = []
148 scenarios[2].tags = []
149 # -- FAKE-CHECK:
150 scenarios[0].should_run_with_name_select.return_value = False
151 scenarios[1].should_run_with_name_select.return_value = False
152 scenarios[2].should_run_with_name_select.return_value = True
153
154 for scenario in scenarios:
155 scenario.run.return_value = False
156
157 self.config.tags.check.return_value = True # pylint: disable=no-member
158 self.config.name = ["(?!Alice)"] # Exclude all scenarios with "Alice"
159 self.config.name_re = Configuration.build_name_re(self.config.name)
160
161 feature = Feature('foo.feature', 1, u'Feature', u'foo',
162 scenarios=scenarios)
163
164 feature.run(self.runner)
165
166 assert not scenarios[0].run.called
167 scenarios[0].should_run_with_name_select.assert_called_with(self.config)
168 scenarios[1].should_run_with_name_select.assert_called_with(self.config)
169 scenarios[2].should_run_with_name_select.assert_called_with(self.config)
170 scenarios[0].run.assert_not_called()
171 scenarios[1].run.assert_not_called()
172 scenarios[2].run.assert_called_with(self.runner)
173
174 def test_feature_hooks_not_run_if_feature_not_being_run(self):
175 self.config.tags.check.return_value = False # pylint: disable=no-member
176
177 feature = Feature('foo.feature', 1, u'Feature', u'foo')
178 feature.run(self.runner)
179 assert not self.run_hook.called
180
181
182 class TestScenarioRun(unittest.TestCase):
183 # pylint: disable=invalid-name
184
185 def setUp(self):
186 self.runner = Mock()
187 self.runner.aborted = False
188 self.runner.feature.tags = []
189 self.config = self.runner.config = Mock()
190 self.config.dry_run = False
191 self.context = self.runner.context = Mock()
192 self.formatters = self.runner.formatters = [Mock()]
193 self.run_hook = self.runner.run_hook = Mock()
194
195 def test_run_invokes_formatter_scenario_and_steps_correctly(self):
196 self.config.stdout_capture = False
197 self.config.log_capture = False
198 self.config.tags.check.return_value = True # pylint: disable=no-member
199 steps = [Mock(), Mock()]
200 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo',
201 steps=steps)
202
203 scenario.run(self.runner)
204
205 self.formatters[0].scenario.assert_called_with(scenario)
206 for step in steps:
207 step.run.assert_called_with(self.runner)
208
209 if six.PY3:
210 stringio_target = 'io.StringIO'
211 else:
212 stringio_target = 'StringIO.StringIO'
213
214 def test_handles_stdout_and_log_capture(self):
215 self.config.stdout_capture = True
216 self.config.log_capture = True
217 self.config.tags.check.return_value = True # pylint: disable=no-member
218
219 steps = [Mock(), Mock()]
220 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo',
221 steps=steps)
222
223 scenario.run(self.runner)
224
225 self.runner.setup_capture.assert_called_with()
226 self.runner.teardown_capture.assert_called_with()
227
228 def test_failed_step_causes_remaining_steps_to_be_skipped(self):
229 self.config.stdout_capture = False
230 self.config.log_capture = False
231 self.config.tags.check.return_value = True # pylint: disable=no-member
232
233 steps = [Mock(), Mock()]
234 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo',
235 steps=steps)
236 steps[0].run.return_value = False
237 steps[1].step_type = "when"
238 steps[1].name = "step1"
239
240 def step1_function(context): # pylint: disable=unused-argument
241 pass
242 my_step_registry = step_registry.StepRegistry()
243 my_step_registry.add_step_definition("when", "step1", step1_function)
244
245 with patch("behave.step_registry.registry", my_step_registry):
246 assert scenario.run(self.runner)
247 eq_(steps[1].status, Status.skipped)
248
249 def test_failed_step_causes_context_failure_to_be_set(self):
250 self.config.stdout_capture = False
251 self.config.log_capture = False
252 self.config.tags.check.return_value = True # pylint: disable=no-member
253
254 steps = [
255 Mock(step_type="given", name="step0"),
256 Mock(step_type="then", name="step1"),
257 ]
258 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo',
259 steps=steps)
260 steps[0].run.return_value = False
261
262 assert scenario.run(self.runner)
263 # pylint: disable=protected-access
264 self.context._set_root_attribute.assert_called_with("failed", True)
265
266 def test_undefined_step_causes_failed_scenario_status(self):
267 self.config.stdout_capture = False
268 self.config.log_capture = False
269 self.config.tags.check.return_value = True # pylint: disable=no-member
270
271 passed_step = Mock()
272 undefined_step = Mock()
273
274 steps = [passed_step, undefined_step]
275 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo',
276 steps=steps)
277 passed_step.run.return_value = True
278 passed_step.status = Status.passed
279 undefined_step.run.return_value = False
280 undefined_step.status = Status.undefined
281
282 assert scenario.run(self.runner)
283 eq_(undefined_step.status, Status.undefined)
284 eq_(scenario.status, Status.failed)
285 # pylint: disable=protected-access
286 self.context._set_root_attribute.assert_called_with("failed", True)
287
288 def test_skipped_steps_set_step_status_and_scenario_status_if_not_set(self):
289 self.config.stdout_capture = False
290 self.config.log_capture = False
291 self.config.tags.check.return_value = False # pylint: disable=no-member
292
293 steps = [Mock(), Mock()]
294 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo',
295 steps=steps)
296
297 scenario.run(self.runner)
298
299 assert False not in [s.status == Status.skipped for s in steps]
300 eq_(scenario.status, Status.skipped)
301
302 def test_scenario_hooks_not_run_if_scenario_not_being_run(self):
303 self.config.tags.check.return_value = False # pylint: disable=no-member
304
305 scenario = Scenario('foo.feature', 17, u'Scenario', u'foo')
306
307 scenario.run(self.runner)
308
309 assert not self.run_hook.called
310
311 def test_should_run_with_name_select(self):
312 scenario_name = u"first scenario"
313 scenario = Scenario("foo.feature", 17, u"Scenario", scenario_name)
314 self.config.name = ['first .*', 'second .*']
315 self.config.name_re = Configuration.build_name_re(self.config.name)
316
317 assert scenario.should_run_with_name_select(self.config)
318
319 class TestScenarioOutline(unittest.TestCase):
320 # pylint: disable=invalid-name
321
322 def test_run_calls_run_on_each_generated_scenario(self):
323 # pylint: disable=protected-access
324 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
325 u'foo')
326 outline._scenarios = [Mock(), Mock()]
327 for scenario in outline._scenarios:
328 scenario.run.return_value = False
329
330 runner = Mock()
331 runner.context = Mock()
332
333 outline.run(runner)
334
335 for s in outline._scenarios:
336 s.run.assert_called_with(runner)
337
338 def test_run_stops_on_first_failure_if_requested(self):
339 # pylint: disable=protected-access
340 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
341 u'foo')
342 outline._scenarios = [Mock(), Mock()]
343 outline._scenarios[0].run.return_value = True
344
345 runner = Mock()
346 runner.context = Mock()
347 config = runner.config = Mock()
348 config.stop = True
349
350 outline.run(runner)
351
352 outline._scenarios[0].run.assert_called_with(runner)
353 assert not outline._scenarios[1].run.called
354
355 def test_run_sets_context_variable_for_outline(self):
356 # pylint: disable=protected-access
357 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
358 u'foo')
359 outline._scenarios = [Mock(), Mock(), Mock()]
360 for scenario in outline._scenarios:
361 scenario.run.return_value = False
362
363 runner = Mock()
364 context = runner.context = Mock()
365 config = runner.config = Mock()
366 config.stop = True
367
368 outline.run(runner)
369
370 eq_(context._set_root_attribute.call_args_list, [
371 (('active_outline', outline._scenarios[0]._row), {}),
372 (('active_outline', outline._scenarios[1]._row), {}),
373 (('active_outline', outline._scenarios[2]._row), {}),
374 (('active_outline', None), {}),
375 ])
376
377 def test_run_should_pass_when_all_examples_pass(self):
378 # pylint: disable=protected-access
379 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
380 u'foo')
381 outline._scenarios = [Mock(), Mock(), Mock()]
382 for scenario in outline._scenarios:
383 scenario.run.return_value = False
384
385 runner = Mock()
386 context = runner.context = Mock()
387 config = runner.config = Mock()
388 config.stop = True
389
390 resultFailed = outline.run(runner)
391 eq_(resultFailed, False)
392
393 def test_run_should_fail_when_first_examples_fails(self):
394 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
395 u'foo')
396 failed = True
397 # pylint: disable=protected-access
398 outline._scenarios = [Mock(), Mock()]
399 outline._scenarios[0].run.return_value = failed
400 outline._scenarios[1].run.return_value = not failed
401
402 runner = Mock()
403 context = runner.context = Mock()
404 config = runner.config = Mock()
405 config.stop = True
406
407 resultFailed = outline.run(runner)
408 eq_(resultFailed, True)
409
410 def test_run_should_fail_when_last_examples_fails(self):
411 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
412 u'foo')
413 failed = True
414 # pylint: disable=protected-access
415 outline._scenarios = [Mock(), Mock()]
416 outline._scenarios[0].run.return_value = not failed
417 outline._scenarios[1].run.return_value = failed
418
419 runner = Mock()
420 context = runner.context = Mock()
421 config = runner.config = Mock()
422 config.stop = True
423
424 resultFailed = outline.run(runner)
425 eq_(resultFailed, True)
426
427 def test_run_should_fail_when_middle_examples_fails(self):
428 outline = ScenarioOutline('foo.feature', 17, u'Scenario Outline',
429 u'foo')
430 failed = True
431 # pylint: disable=protected-access
432 outline._scenarios = [Mock(), Mock(), Mock()]
433 outline._scenarios[0].run.return_value = not failed
434 outline._scenarios[1].run.return_value = failed
435 outline._scenarios[2].run.return_value = not failed
436
437 runner = Mock()
438 context = runner.context = Mock()
439 config = runner.config = Mock()
440 config.stop = True
441
442 resultFailed = outline.run(runner)
443 eq_(resultFailed, True)
444
445
446 def raiser(exception):
447 def func(*args, **kwargs): # pylint: disable=unused-argument
448 raise exception
449 return func
450
451
452 class TestStepRun(unittest.TestCase):
453 # pylint: disable=invalid-name
454
455 def setUp(self):
456 self.step_registry = Mock()
457 self.runner = Mock()
458 # self.capture_controller = self.runner.capture_controller = Mock()
459 self.capture_controller = CaptureController(self.runner.config)
460 self.runner.capture_controller = self.capture_controller
461 self.runner.step_registry = self.step_registry
462 self.config = self.runner.config = Mock()
463 self.config.outputs = [None]
464 self.context = self.runner.context = Mock()
465 print('context is %s' % self.context)
466 self.formatters = self.runner.formatters = [Mock()]
467 self.stdout_capture = self.capture_controller.stdout_capture = Mock()
468 self.stdout_capture.getvalue.return_value = ''
469 self.stderr_capture = self.capture_controller.stderr_capture = Mock()
470 self.stderr_capture.getvalue.return_value = ''
471 self.log_capture = self.capture_controller.log_capture = Mock()
472 self.log_capture.getvalue.return_value = ''
473 self.run_hook = self.runner.run_hook = Mock()
474
475 def test_run_appends_step_to_undefined_when_no_match_found(self):
476 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
477 self.runner.step_registry.find_match.return_value = None
478 self.runner.undefined_steps = []
479 assert not step.run(self.runner)
480
481 assert step in self.runner.undefined_steps
482 eq_(step.status, Status.undefined)
483
484 def test_run_reports_undefined_step_via_formatter_when_not_quiet(self):
485 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
486 self.runner.step_registry.find_match.return_value = None
487 assert not step.run(self.runner)
488
489 self.formatters[0].match.assert_called_with(NoMatch())
490 self.formatters[0].result.assert_called_with(step)
491
492 def test_run_with_no_match_does_not_touch_formatter_when_quiet(self):
493 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
494 self.runner.step_registry.find_match.return_value = None
495 assert not step.run(self.runner, quiet=True)
496
497 assert not self.formatters[0].match.called
498 assert not self.formatters[0].result.called
499
500 def test_run_when_not_quiet_reports_match_and_result(self):
501 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
502 match = Mock()
503 self.runner.step_registry.find_match.return_value = match
504
505 side_effects = (None, raiser(AssertionError('whee')),
506 raiser(Exception('whee')))
507 for side_effect in side_effects:
508 match.run.side_effect = side_effect
509 step.run(self.runner)
510 self.formatters[0].match.assert_called_with(match)
511 self.formatters[0].result.assert_called_with(step)
512
513 def test_run_when_quiet_reports_nothing(self):
514 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
515 match = Mock()
516 self.runner.step_registry.find_match.return_value = match
517
518 side_effects = (None, raiser(AssertionError('whee')),
519 raiser(Exception('whee')))
520 for side_effect in side_effects:
521 match.run.side_effect = side_effect
522 step.run(self.runner, quiet=True)
523 assert not self.formatters[0].match.called
524 assert not self.formatters[0].result.called
525
526 def test_run_runs_before_hook_then_match_then_after_hook(self):
527 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
528 match = Mock()
529 self.runner.step_registry.find_match.return_value = match
530
531 side_effects = (None, AssertionError('whee'), Exception('whee'))
532 for side_effect in side_effects:
533 # Make match.run() and runner.run_hook() the same mock so
534 # we can make sure things happen in the right order.
535 self.runner.run_hook = match.run = Mock()
536
537 def effect(thing):
538 # pylint: disable=unused-argument
539 def raiser_(*args, **kwargs):
540 match.run.side_effect = None
541 if thing:
542 raise thing
543
544 def nonraiser(*args, **kwargs):
545 match.run.side_effect = raiser_
546
547 return nonraiser
548
549 match.run.side_effect = effect(side_effect)
550 step.run(self.runner)
551
552 eq_(match.run.call_args_list, [
553 (('before_step', self.context, step), {}),
554 ((self.context,), {}),
555 (('after_step', self.context, step), {}),
556 ])
557
558
559 def test_run_sets_table_if_present(self):
560 step = Step('foo.feature', 17, u'Given', 'given', u'foo',
561 table=Mock())
562 self.runner.step_registry.find_match.return_value = Mock()
563 step.run(self.runner)
564 eq_(self.context.table, step.table)
565
566 def test_run_sets_text_if_present(self):
567 step = Step('foo.feature', 17, u'Given', 'given', u'foo',
568 text=Mock(name='text'))
569 self.runner.step_registry.find_match.return_value = Mock()
570 step.run(self.runner)
571
572 eq_(self.context.text, step.text)
573
574 def test_run_sets_status_to_passed_if_nothing_goes_wrong(self):
575 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
576 step.error_message = None
577 self.runner.step_registry.find_match.return_value = Mock()
578 step.run(self.runner)
579
580 eq_(step.status, Status.passed)
581 eq_(step.error_message, None)
582
583 def test_run_sets_status_to_failed_on_assertion_error(self):
584 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
585 self.runner.context = Context(self.runner)
586 self.runner.config.stdout_capture = True
587 self.runner.config.log_capture = False
588 self.runner.capture_controller = CaptureController(self.runner.config)
589 self.runner.capture_controller.setup_capture(self.runner.context)
590 step.error_message = None
591 match = Mock()
592 match.run.side_effect = raiser(AssertionError('whee'))
593 self.runner.step_registry.find_match.return_value = match
594 step.run(self.runner)
595
596 eq_(step.status, Status.failed)
597 assert step.error_message.startswith('Assertion Failed')
598
599 @patch('%s.format_exc' % traceback_modname)
600 def test_run_sets_status_to_failed_on_exception(self, format_exc):
601 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
602 step.error_message = None
603 match = Mock()
604 match.run.side_effect = raiser(Exception('whee'))
605 self.runner.step_registry.find_match.return_value = match
606 format_exc.return_value = 'something to do with an exception'
607
608 step.run(self.runner)
609 eq_(step.status, Status.failed)
610 eq_(step.error_message, format_exc.return_value)
611
612 @patch('time.time')
613 def test_run_calculates_duration(self, time_time):
614 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
615 match = Mock()
616 self.runner.step_registry.find_match.return_value = match
617
618 def time_time_1():
619 def time_time_2():
620 return 23
621 time_time.side_effect = time_time_2
622 return 17
623
624 side_effects = (None, raiser(AssertionError('whee')),
625 raiser(Exception('whee')))
626 for side_effect in side_effects:
627 match.run.side_effect = side_effect
628 time_time.side_effect = time_time_1
629
630 step.run(self.runner)
631 eq_(step.duration, 23 - 17)
632
633 def test_run_captures_stdout_and_logging(self):
634 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
635 match = Mock()
636 self.runner.step_registry.find_match.return_value = match
637
638 assert step.run(self.runner)
639
640 self.runner.start_capture.assert_called_with()
641 self.runner.stop_capture.assert_called_with()
642
643 def test_run_appends_any_captured_stdout_on_failure(self):
644 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
645 match = Mock()
646 self.runner.step_registry.find_match.return_value = match
647 self.stdout_capture.getvalue.return_value = 'frogs'
648 match.run.side_effect = raiser(Exception('halibut'))
649
650 assert not step.run(self.runner)
651 assert 'Captured stdout:' in step.error_message
652 assert 'frogs' in step.error_message
653
654 def test_run_appends_any_captured_logging_on_failure(self):
655 step = Step('foo.feature', 17, u'Given', 'given', u'foo')
656 match = Mock()
657 self.runner.step_registry.find_match.return_value = match
658 self.log_capture.getvalue.return_value = 'toads'
659 match.run.side_effect = raiser(AssertionError('kipper'))
660
661 assert not step.run(self.runner)
662 assert 'Captured logging:' in step.error_message
663 assert 'toads' in step.error_message
664
665
666 class TestTableModel(unittest.TestCase):
667 # pylint: disable=invalid-name
668 HEAD = [u'type of stuff', u'awesomeness', u'ridiculousness']
669 DATA = [
670 [u'fluffy', u'large', u'frequent'],
671 [u'lint', u'low', u'high'],
672 [u'green', u'variable', u'awkward'],
673 ]
674
675 def setUp(self):
676 self.table = Table(self.HEAD, 0, self.DATA)
677
678 def test_equivalence(self):
679 t1 = self.table
680 self.setUp()
681 eq_(t1, self.table)
682
683 def test_table_iteration(self):
684 for i, row in enumerate(self.table):
685 for j, cell in enumerate(row):
686 eq_(cell, self.DATA[i][j])
687
688 def test_table_row_by_index(self):
689 for i in range(3):
690 eq_(self.table[i], Row(self.HEAD, self.DATA[i], 0))
691
692 def test_table_row_name(self):
693 eq_(self.table[0]['type of stuff'], 'fluffy')
694 eq_(self.table[1]['awesomeness'], 'low')
695 eq_(self.table[2]['ridiculousness'], 'awkward')
696
697 def test_table_row_index(self):
698 eq_(self.table[0][0], 'fluffy')
699 eq_(self.table[1][1], 'low')
700 eq_(self.table[2][2], 'awkward')
701
702 @raises(KeyError)
703 def test_table_row_keyerror(self):
704 self.table[0]['spam'] # pylint: disable=pointless-statement
705
706 def test_table_row_items(self):
707 eq_(list(self.table[0].items()), list(zip(self.HEAD, self.DATA[0])))
708
709
710 class TestModelRow(unittest.TestCase):
711 # pylint: disable=invalid-name, bad-whitespace
712 HEAD = [u'name', u'sex', u'age']
713 DATA = [u'Alice', u'female', u'12']
714
715 def setUp(self):
716 self.row = Row(self.HEAD, self.DATA, 0)
717
718 def test_len(self):
719 eq_(len(self.row), 3)
720
721 def test_getitem_with_valid_colname(self):
722 # pylint: disable=bad-whitespace
723 eq_(self.row['name'], u'Alice')
724 eq_(self.row['sex'], u'female')
725 eq_(self.row['age'], u'12')
726
727 @raises(KeyError)
728 def test_getitem_with_unknown_colname(self):
729 self.row['__UNKNOWN_COLUMN__'] # pylint: disable=pointless-statement
730
731 def test_getitem_with_valid_index(self):
732 eq_(self.row[0], u'Alice')
733 eq_(self.row[1], u'female')
734 eq_(self.row[2], u'12')
735
736 @raises(IndexError)
737 def test_getitem_with_invalid_index(self):
738 colsize = len(self.row)
739 eq_(colsize, 3)
740 self.row[colsize] # pylint: disable=pointless-statement
741
742 def test_get_with_valid_colname(self):
743 # pylint: disable=bad-whitespace
744 eq_(self.row.get('name'), u'Alice')
745 eq_(self.row.get('sex'), u'female')
746 eq_(self.row.get('age'), u'12')
747
748 def test_getitem_with_unknown_colname_should_return_default(self):
749 eq_(self.row.get('__UNKNOWN_COLUMN__', 'XXX'), u'XXX')
750
751 def test_as_dict(self):
752 data1 = self.row.as_dict()
753 data2 = dict(self.row.as_dict())
754 assert isinstance(data1, dict)
755 assert isinstance(data2, dict)
756 assert isinstance(data1, OrderedDict)
757 # -- REQUIRES: Python2.7 or ordereddict installed.
758 # assert not isinstance(data2, OrderedDict)
759 eq_(data1, data2)
760 # pylint: disable=bad-whitespace
761 eq_(data1['name'], u'Alice')
762 eq_(data1['sex'], u'female')
763 eq_(data1['age'], u'12')
764
765
766 class TestFileLocation(unittest.TestCase):
767 # pylint: disable=invalid-name
768 ordered_locations1 = [
769 FileLocation("features/alice.feature", 1),
770 FileLocation("features/alice.feature", 5),
771 FileLocation("features/alice.feature", 10),
772 FileLocation("features/alice.feature", 11),
773 FileLocation("features/alice.feature", 100),
774 ]
775 ordered_locations2 = [
776 FileLocation("features/alice.feature", 1),
777 FileLocation("features/alice.feature", 10),
778 FileLocation("features/bob.feature", 5),
779 FileLocation("features/charly.feature", None),
780 FileLocation("features/charly.feature", 0),
781 FileLocation("features/charly.feature", 100),
782 ]
783 same_locations = [
784 (FileLocation("alice.feature"),
785 FileLocation("alice.feature", None),
786 ),
787 (FileLocation("alice.feature", 10),
788 FileLocation("alice.feature", 10),
789 ),
790 (FileLocation("features/bob.feature", 11),
791 FileLocation("features/bob.feature", 11),
792 ),
793 ]
794
795 def test_compare_equal(self):
796 for value1, value2 in self.same_locations:
797 eq_(value1, value2)
798
799 def test_compare_equal_with_string(self):
800 for location in self.ordered_locations2:
801 eq_(location, location.filename)
802 eq_(location.filename, location)
803
804 def test_compare_not_equal(self):
805 for value1, value2 in self.same_locations:
806 assert not(value1 != value2) # pylint: disable=unneeded-not, superfluous-parens
807
808 for locations in [self.ordered_locations1, self.ordered_locations2]:
809 for value1, value2 in zip(locations, locations[1:]):
810 assert value1 != value2
811
812 def test_compare_less_than(self):
813 for locations in [self.ordered_locations1, self.ordered_locations2]:
814 for value1, value2 in zip(locations, locations[1:]):
815 assert value1 < value2, "FAILED: %s < %s" % (_text(value1), _text(value2))
816 assert value1 != value2
817
818 def test_compare_less_than_with_string(self):
819 locations = self.ordered_locations2
820 for value1, value2 in zip(locations, locations[1:]):
821 if value1.filename == value2.filename:
822 continue
823 assert value1 < value2.filename, \
824 "FAILED: %s < %s" % (_text(value1), _text(value2.filename))
825 assert value1.filename < value2, \
826 "FAILED: %s < %s" % (_text(value1.filename), _text(value2))
827
828 def test_compare_greater_than(self):
829 for locations in [self.ordered_locations1, self.ordered_locations2]:
830 for value1, value2 in zip(locations, locations[1:]):
831 assert value2 > value1, "FAILED: %s > %s" % (_text(value2), _text(value1))
832 assert value2 != value1
833
834 def test_compare_less_or_equal(self):
835 for value1, value2 in self.same_locations:
836 assert value1 <= value2, "FAILED: %s <= %s" % (_text(value1), _text(value2))
837 assert value1 == value2
838
839 for locations in [self.ordered_locations1, self.ordered_locations2]:
840 for value1, value2 in zip(locations, locations[1:]):
841 assert value1 <= value2, "FAILED: %s <= %s" % (_text(value1), _text(value2))
842 assert value1 != value2
843
844 def test_compare_greater_or_equal(self):
845 for value1, value2 in self.same_locations:
846 assert value2 >= value1, "FAILED: %s >= %s" % (_text(value2), _text(value1))
847 assert value2 == value1
848
849 for locations in [self.ordered_locations1, self.ordered_locations2]:
850 for value1, value2 in zip(locations, locations[1:]):
851 assert value2 >= value1, "FAILED: %s >= %s" % (_text(value2), _text(value1))
852 assert value2 != value1
853
854 def test_filename_should_be_same_as_self(self):
855 for location in self.ordered_locations2:
856 assert location == location.filename
857 assert location.filename == location
858
859 def test_string_conversion(self):
860 for location in self.ordered_locations2:
861 expected = u"%s:%s" % (location.filename, location.line)
862 if location.line is None:
863 expected = location.filename
864 assert six.text_type(location) == expected
865
866 def test_repr_conversion(self):
867 for location in self.ordered_locations2:
868 expected = u'<FileLocation: filename="%s", line=%s>' % \
869 (location.filename, location.line)
870 actual = repr(location)
871 assert actual == expected, "FAILED: %s == %s" % (actual, expected)