def test_false(self): """ Returns ``False`` if any predicate returns a false value. """ false = lambda: False true = lambda: True self.assertThat(every_pred(true, false)(), Is(False))
def _route_matcher(route): """ Create a route matching function. :param Route route: Route to build the matcher for. :return: Callable taking a ``REQUEST`` value from a context, returning ``None`` if no match or a pmap of matched path parameters. """ def _base_matchers(route): method = route.method if method != ANY: yield lambda req: req['request_method'] == method def _path_matcher(route): def _path_matcher_inner(request): match = path_re.match(request['path_info']) if match: return pmap(zip(path_params, match.groups())) path_re = route.path_re path_params = route.path_params return _path_matcher_inner base_match = every_pred(*_base_matchers(route)) path_match = _path_matcher(route) return lambda req: (base_match(req) or None) and path_match(req)
def test_args(self): """ Pass arguments through to the predicates. """ def _capture(result): def _capture_inner(*a, **kw): called.append((a, kw)) return result called = [] return _capture_inner, called capture, called = _capture(True) self.assertThat(every_pred(capture, capture)(1, b=3), Is(True)) self.assertThat( called, MatchesListwise( [Equals(((1, ), dict(b=3))), Equals(((1, ), dict(b=3)))]))
def test_true(self): """ Returns ``True`` if all predicates return true values. """ true = lambda: True self.assertThat(every_pred(true, true)(), Is(True))
def test_empty(self): """ Default to a ``True`` result when there are no predicates. """ self.assertThat(every_pred()(), Is(True))