| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
""" |
| 3 |
Test support functionality for other tests. |
| 4 |
""" |
| 5 |
|
| 6 |
# -- IMPORTS: |
| 7 |
from __future__ import absolute_import |
| 8 |
from behave.step_registry import StepRegistry |
| 9 |
from behave.matchers import ParseMatcher, CFParseMatcher, RegexMatcher |
| 10 |
import time |
| 11 |
|
| 12 |
|
| 13 |
# ----------------------------------------------------------------------------- |
| 14 |
# TEST SUPPORT: |
| 15 |
# ----------------------------------------------------------------------------- |
| 16 |
class StopWatch(object): |
| 17 |
def __init__(self): |
| 18 |
self.start_time = None |
| 19 |
self.duration = None |
| 20 |
|
| 21 |
def start(self): |
| 22 |
self.start_time = self.now() |
| 23 |
self.duration = None |
| 24 |
|
| 25 |
def stop(self): |
| 26 |
self.duration = self.now() - self.start_time |
| 27 |
|
| 28 |
@staticmethod |
| 29 |
def now(): |
| 30 |
return time.time() |
| 31 |
|
| 32 |
@property |
| 33 |
def elapsed(self): |
| 34 |
assert self.start_time is not None |
| 35 |
return self.now() - self.start_time |
| 36 |
|
| 37 |
def __enter__(self): |
| 38 |
self.start() |
| 39 |
return self |
| 40 |
|
| 41 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 42 |
self.stop() |
| 43 |
|
| 44 |
# -- NEEDED-UNTIL: stepimport functionality is completly provided. |
| 45 |
class MatcherFactory(object): |
| 46 |
matcher_mapping = { |
| 47 |
"parse": ParseMatcher, |
| 48 |
"cfparse": CFParseMatcher, |
| 49 |
"re": RegexMatcher, |
| 50 |
} |
| 51 |
default_matcher = ParseMatcher |
| 52 |
|
| 53 |
def __init__(self, matcher_mapping=None, default_matcher=None): |
| 54 |
self.matcher_mapping = matcher_mapping or self.matcher_mapping |
| 55 |
self.default_matcher = default_matcher or self.default_matcher |
| 56 |
self.current_matcher = self.default_matcher |
| 57 |
self.type_registry = {} |
| 58 |
# self.type_registry = ParseMatcher.custom_types.copy() |
| 59 |
|
| 60 |
def register_type(self, **kwargs): |
| 61 |
self.type_registry.update(**kwargs) |
| 62 |
|
| 63 |
def use_step_matcher(self, name): |
| 64 |
self.current_matcher = self.matcher_mapping[name] |
| 65 |
|
| 66 |
def use_default_step_matcher(self, name=None): |
| 67 |
if name: |
| 68 |
self.default_matcher = self.matcher_mapping[name] |
| 69 |
self.current_matcher = self.default_matcher |
| 70 |
|
| 71 |
def make_matcher(self, func, step_text, step_type=None): |
| 72 |
return self.current_matcher(func, step_text, step_type=step_type, |
| 73 |
custom_types=self.type_registry) |
| 74 |
|
| 75 |
def step_matcher(self, name): |
| 76 |
""" |
| 77 |
DEPRECATED, use :method:`~MatcherFactory.use_step_matcher()` instead. |
| 78 |
""" |
| 79 |
# -- BACKWARD-COMPATIBLE NAME: Mark as deprecated. |
| 80 |
import warnings |
| 81 |
warnings.warn("Use 'use_step_matcher()' instead", |
| 82 |
PendingDeprecationWarning, stacklevel=2) |
| 83 |
self.use_step_matcher(name) |
| 84 |
|
| 85 |
|
| 86 |
class SimpleStepContainer(object): |
| 87 |
def __init__(self, step_registry=None): |
| 88 |
if step_registry is None: |
| 89 |
step_registry = StepRegistry() |
| 90 |
matcher_factory = MatcherFactory() |
| 91 |
|
| 92 |
self.step_registry = step_registry |
| 93 |
self.step_registry.matcher_factory = matcher_factory |
| 94 |
self.matcher_factory = matcher_factory |