コード例 #1
0
 def test_positional_arguments(self, args):
     """
     The function given to ``txapply`` is called with the values from the
     Deferreds it is given.
     """
     deferred_args = map(succeed, args)
     d = txapply(lambda *a: a, *deferred_args)
     self.assertThat(d, succeeded(Equals(tuple(args))))
コード例 #2
0
    def test_binary_function(self, f, x, y):
        """
        The function given to ``txapply`` is called with the values from the
        Deferreds it is given.

        ``txapply(f, d1, d2)`` will return a Deferred with the result of
        ``f(x, y)``, where ``x`` is the result of ``d1`` and ``y`` is the
        result of ``d2``.
        """
        assume(f not in (operator.div, operator.mod) or y != 0)
        d = txapply(f, succeed(x), succeed(y))
        self.assertThat(d, succeeded(Equals(f(x, y))))
コード例 #3
0
 def test_exception_in_args(self, args, exception, choice):
     """
     If one of the arguments is a failing Deferred, then "reraise" that
     failing Deferred.
     """
     i = choice(range(len(args)))
     deferred_args = map(succeed, args)
     deferred_args[i] = maybeDeferred(throw, exception)
     d = txapply(lambda *a: a, *deferred_args)
     self.assertThat(
         d,
         failed(
             AfterPreprocessing(lambda failure: failure.value,
                                Equals(exception))))
コード例 #4
0
    def test_combination_arguments(self, args, kwargs):
        """
        If ``txapply`` is given a combination of positional and keyword
        arguments, these are passed through to the function.
        """
        deferred_args = map(succeed, args)
        deferred_kwargs = {
            key: succeed(value)
            for key, value in kwargs.items()
        }

        def capture(*a, **kw):
            return a, kw

        d = txapply(capture, *deferred_args, **deferred_kwargs)
        self.assertThat(d, succeeded(Equals((tuple(args), kwargs))))
コード例 #5
0
    def test_keyword_arguments(self, kwargs):
        """
        The function given to ``txapply`` is called with keyword arguments from
        the values of the Deferreds it is given as keyword arguments.

        That is, keyword arguments are passed through.

        ``txapply(f, foo=d1, bar=d2)`` is equivalent to ``f(foo=x, bar=y)``
        where ``x`` is the result of ``d1`` and ``y`` is the result of ``d2``.

        """
        deferred_kwargs = {
            key: succeed(value)
            for key, value in kwargs.items()
        }
        d = txapply(dict, **deferred_kwargs)
        self.assertThat(d, succeeded(Equals(kwargs)))
コード例 #6
0
    def test_combination_arguments_deferred_function(self, args, kwargs):
        """
        If ``txapply`` is called with a function ``f`` that itself returns a
        ``Deferred`` then the result of that ``Deferred`` is the result of
        calling ``f`` with the results of all of the ``Deferred`` objects
        passed to ``txapply``.
        """
        deferred_args = map(succeed, args)
        deferred_kwargs = {
            key: succeed(value)
            for key, value in kwargs.items()
        }

        def capture(*a, **kw):
            return succeed((a, kw))

        d = txapply(capture, *deferred_args, **deferred_kwargs)
        self.assertThat(d, succeeded(Equals((tuple(args), kwargs))))
コード例 #7
0
 def test_unary_function(self, f, x):
     """
     ``txapply(f, d)`` is equivalent to ``d.addCallback(f)``.
     """
     d = txapply(f, succeed(x))
     self.assertThat(d, succeeded(Equals(f(x))))
コード例 #8
0
 def test_identity(self, anything):
     """
     ``txapply(identity, deferred)`` is equivalent to ``deferred``.
     """
     d = txapply(identity, succeed(anything))
     self.assertThat(d, succeeded(Is(anything)))