thirdparty/mocker/mocker.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 14 Feb 2009 15:16:02 +0000
changeset 1313 ec79c190f5ca
parent 104 5a2786fd5048
permissions -rw-r--r--
Force-check the 'agreed to admin agreement' field if applicable If the current user is the applicant of the relevant organization, they already agreed to the agreement once. As such the box should be pre-checked. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
104
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
"""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
Copyright (c) 2007  Gustavo Niemeyer <gustavo@niemeyer.net>
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
Graceful platform for test doubles in Python (mocks, stubs, fakes, and dummies).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
"""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
import __builtin__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
import tempfile
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
import unittest
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
import inspect
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
import shutil
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
import types
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
import sys
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
import os
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
import gc
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
if sys.version_info < (2, 4):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
    from sets import Set as set # pragma: nocover
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
__all__ = ["Mocker", "expect", "IS", "CONTAINS", "IN", "MATCH",
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
           "ANY", "ARGS", "KWARGS"]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
__author__ = "Gustavo Niemeyer <gustavo@niemeyer.net>"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
__license__ = "PSF License"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
__version__ = "0.10.1"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
ERROR_PREFIX = "[Mocker] "
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
# Exceptions
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
class MatchError(AssertionError):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
    """Raised when an unknown expression is seen in playback mode."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
# Helper for chained-style calling.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
class expect(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
    """This is a simple helper that allows a different call-style.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
    With this class one can comfortably do chaining of calls to the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    47
    mocker object responsible by the object being handled. For instance::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    48
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    49
        expect(obj.attr).result(3).count(1, 2)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    50
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    51
    Is the same as::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    52
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    53
        obj.attr
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    54
        mocker.result(3)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    55
        mocker.count(1, 2)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    56
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    57
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    58
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    59
    def __init__(self, mock, attr=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    60
        self._mock = mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    61
        self._attr = attr
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    62
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    63
    def __getattr__(self, attr):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    64
        return self.__class__(self._mock, attr)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    65
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    66
    def __call__(self, *args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    67
        getattr(self._mock.__mocker__, self._attr)(*args, **kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    68
        return self
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    69
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    70
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    71
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    72
# Extensions to Python's unittest.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    73
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    74
class MockerTestCase(unittest.TestCase):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    75
    """unittest.TestCase subclass with Mocker support.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    76
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    77
    @ivar mocker: The mocker instance.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    78
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    79
    This is a convenience only.  Mocker may easily be used with the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    80
    standard C{unittest.TestCase} class if wanted.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    81
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    82
    Test methods have a Mocker instance available on C{self.mocker}.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    83
    At the end of each test method, expectations of the mocker will
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    84
    be verified, and any requested changes made to the environment
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    85
    will be restored.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    86
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    87
    In addition to the integration with Mocker, this class provides
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    88
    a few additional helper methods.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    89
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    90
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    91
    expect = expect
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    92
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    93
    def __init__(self, methodName="runTest"):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    94
        # So here is the trick: we take the real test method, wrap it on
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    95
        # a function that do the job we have to do, and insert it in the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    96
        # *instance* dictionary, so that getattr() will return our
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    97
        # replacement rather than the class method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    98
        test_method = getattr(self, methodName, None)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    99
        if test_method is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   100
            def test_method_wrapper():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   101
                try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   102
                    result = test_method()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   103
                except:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   104
                    raise
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   105
                else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   106
                    if (self.mocker.is_recording() and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   107
                        self.mocker.get_events()):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   108
                        raise RuntimeError("Mocker must be put in replay "
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   109
                                           "mode with self.mocker.replay()")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   110
                    if (hasattr(result, "addCallback") and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   111
                        hasattr(result, "addErrback")):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   112
                        def verify(result):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   113
                            self.mocker.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   114
                            return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   115
                        result.addCallback(verify)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   116
                    else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   117
                        self.mocker.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   118
                    return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   119
            # Copy all attributes from the original method..
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   120
            for attr in dir(test_method):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   121
                # .. unless they're present in our wrapper already.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   122
                if not hasattr(test_method_wrapper, attr) or attr == "__doc__":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   123
                    setattr(test_method_wrapper, attr,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   124
                            getattr(test_method, attr))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   125
            setattr(self, methodName, test_method_wrapper)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   126
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   127
        # We could overload run() normally, but other well-known testing
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   128
        # frameworks do it as well, and some of them won't call the super,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   129
        # which might mean that cleanup wouldn't happen.  With that in mind,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   130
        # we make integration easier by using the following trick.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   131
        run_method = self.run
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   132
        def run_wrapper(*args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   133
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   134
                return run_method(*args, **kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   135
            finally:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   136
                self.__cleanup()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   137
        self.run = run_wrapper
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   138
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   139
        self.mocker = Mocker()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   140
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   141
        self.__cleanup_funcs = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   142
        self.__cleanup_paths = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   143
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   144
        super(MockerTestCase, self).__init__(methodName)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   145
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   146
    def __cleanup(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   147
        for path in self.__cleanup_paths:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   148
            if os.path.isfile(path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   149
                os.unlink(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   150
            elif os.path.isdir(path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   151
                shutil.rmtree(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   152
        self.mocker.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   153
        for func, args, kwargs in self.__cleanup_funcs:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   154
            func(*args, **kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   155
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   156
    def addCleanup(self, func, *args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   157
        self.__cleanup_funcs.append((func, args, kwargs))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   158
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   159
    def makeFile(self, content=None, suffix="", prefix="tmp", basename=None,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   160
                 dirname=None, path=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   161
        """Create a temporary file and return the path to it.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   162
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   163
        @param content: Initial content for the file.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   164
        @param suffix: Suffix to be given to the file's basename.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   165
        @param prefix: Prefix to be given to the file's basename.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   166
        @param basename: Full basename for the file.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   167
        @param dirname: Put file inside this directory.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   168
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   169
        The file is removed after the test runs.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   170
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   171
        if path is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   172
            self.__cleanup_paths.append(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   173
        elif basename is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   174
            if dirname is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   175
                dirname = tempfile.mkdtemp()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   176
                self.__cleanup_paths.append(dirname)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   177
            path = os.path.join(dirname, basename)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   178
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   179
            fd, path = tempfile.mkstemp(suffix, prefix, dirname)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   180
            self.__cleanup_paths.append(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   181
            os.close(fd)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   182
            if content is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   183
                os.unlink(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   184
        if content is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   185
            file = open(path, "w")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   186
            file.write(content)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   187
            file.close()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   188
        return path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   189
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   190
    def makeDir(self, suffix="", prefix="tmp", dirname=None, path=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   191
        """Create a temporary directory and return the path to it.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   192
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   193
        @param suffix: Suffix to be given to the file's basename.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   194
        @param prefix: Prefix to be given to the file's basename.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   195
        @param dirname: Put directory inside this parent directory.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   196
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   197
        The directory is removed after the test runs.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   198
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   199
        if path is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   200
            os.makedirs(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   201
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   202
            path = tempfile.mkdtemp(suffix, prefix, dirname)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   203
        self.__cleanup_paths.append(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   204
        return path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   205
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   206
    def failUnlessIs(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   207
        """Assert that C{first} is the same object as C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   208
        if first is not second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   209
            raise self.failureException(msg or "%r is not %r" % (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   210
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   211
    def failIfIs(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   212
        """Assert that C{first} is not the same object as C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   213
        if first is second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   214
            raise self.failureException(msg or "%r is %r" % (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   215
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   216
    def failUnlessIn(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   217
        """Assert that C{first} is contained in C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   218
        if first not in second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   219
            raise self.failureException(msg or "%r not in %r" % (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   220
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   221
    def failUnlessStartsWith(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   222
        """Assert that C{first} starts with C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   223
        if first[:len(second)] != second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   224
            raise self.failureException(msg or "%r doesn't start with %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   225
                                               (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   226
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   227
    def failIfStartsWith(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   228
        """Assert that C{first} doesn't start with C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   229
        if first[:len(second)] == second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   230
            raise self.failureException(msg or "%r starts with %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   231
                                               (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   232
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   233
    def failUnlessEndsWith(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   234
        """Assert that C{first} starts with C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   235
        if first[len(first)-len(second):] != second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   236
            raise self.failureException(msg or "%r doesn't end with %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   237
                                               (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   238
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   239
    def failIfEndsWith(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   240
        """Assert that C{first} doesn't start with C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   241
        if first[len(first)-len(second):] == second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   242
            raise self.failureException(msg or "%r ends with %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   243
                                               (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   244
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   245
    def failIfIn(self, first, second, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   246
        """Assert that C{first} is not contained in C{second}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   247
        if first in second:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   248
            raise self.failureException(msg or "%r in %r" % (first, second))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   249
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   250
    def failUnlessApproximates(self, first, second, tolerance, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   251
        """Assert that C{first} is near C{second} by at most C{tolerance}."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   252
        if abs(first - second) > tolerance:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   253
            raise self.failureException(msg or "abs(%r - %r) > %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   254
                                        (first, second, tolerance))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   255
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   256
    def failIfApproximates(self, first, second, tolerance, msg=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   257
        """Assert that C{first} is far from C{second} by at least C{tolerance}.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   258
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   259
        if abs(first - second) <= tolerance:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   260
            raise self.failureException(msg or "abs(%r - %r) <= %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   261
                                        (first, second, tolerance))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   262
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   263
    def failUnlessMethodsMatch(self, first, second):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   264
        """Assert that public methods in C{first} are present in C{second}.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   265
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   266
        This method asserts that all public methods found in C{first} are also
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   267
        present in C{second} and accept the same arguments.  C{first} may
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   268
        have its own private methods, though, and may not have all methods
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   269
        found in C{second}.  Note that if a private method in C{first} matches
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   270
        the name of one in C{second}, their specification is still compared.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   271
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   272
        This is useful to verify if a fake or stub class have the same API as
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   273
        the real class being simulated.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   274
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   275
        first_methods = dict(inspect.getmembers(first, inspect.ismethod))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   276
        second_methods = dict(inspect.getmembers(second, inspect.ismethod))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   277
        for name, first_method in first_methods.items():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   278
            first_argspec = inspect.getargspec(first_method)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   279
            first_formatted = inspect.formatargspec(*first_argspec)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   280
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   281
            second_method = second_methods.get(name)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   282
            if second_method is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   283
                if name[:1] == "_":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   284
                    continue # First may have its own private methods.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   285
                raise self.failureException("%s.%s%s not present in %s" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   286
                    (first.__name__, name, first_formatted, second.__name__))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   287
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   288
            second_argspec = inspect.getargspec(second_method)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   289
            if first_argspec != second_argspec:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   290
                second_formatted = inspect.formatargspec(*second_argspec)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   291
                raise self.failureException("%s.%s%s != %s.%s%s" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   292
                    (first.__name__, name, first_formatted,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   293
                     second.__name__, name, second_formatted))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   294
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   295
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   296
    assertIs = failUnlessIs
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   297
    assertIsNot = failIfIs
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   298
    assertIn = failUnlessIn
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   299
    assertNotIn = failIfIn
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   300
    assertStartsWith = failUnlessStartsWith
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   301
    assertNotStartsWith = failIfStartsWith
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   302
    assertEndsWith = failUnlessEndsWith
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   303
    assertNotEndsWith = failIfEndsWith
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   304
    assertApproximates = failUnlessApproximates
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   305
    assertNotApproximates = failIfApproximates
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   306
    assertMethodsMatch = failUnlessMethodsMatch
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   307
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   308
    # The following are missing in Python < 2.4.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   309
    assertTrue = unittest.TestCase.failUnless
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   310
    assertFalse = unittest.TestCase.failIf
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   311
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   312
    # The following is provided for compatibility with Twisted's trial.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   313
    assertIdentical = assertIs
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   314
    assertNotIdentical = assertIsNot
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   315
    failUnlessIdentical = failUnlessIs
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   316
    failIfIdentical = failIfIs
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   317
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   318
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   319
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   320
# Mocker.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   321
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   322
class classinstancemethod(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   323
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   324
    def __init__(self, method):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   325
        self.method = method
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   326
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   327
    def __get__(self, obj, cls=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   328
        def bound_method(*args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   329
            return self.method(cls, obj, *args, **kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   330
        return bound_method
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   331
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   332
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   333
class MockerBase(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   334
    """Controller of mock objects.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   335
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   336
    A mocker instance is used to command recording and replay of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   337
    expectations on any number of mock objects.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   338
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   339
    Expectations should be expressed for the mock object while in
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   340
    record mode (the initial one) by using the mock object itself,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   341
    and using the mocker (and/or C{expect()} as a helper) to define
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   342
    additional behavior for each event.  For instance::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   343
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   344
        mock = mocker.mock()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   345
        mock.hello()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   346
        mocker.result("Hi!")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   347
        mocker.replay()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   348
        assert mock.hello() == "Hi!"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   349
        mock.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   350
        mock.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   351
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   352
    In this short excerpt a mock object is being created, then an
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   353
    expectation of a call to the C{hello()} method was recorded, and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   354
    when called the method should return the value C{10}.  Then, the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   355
    mocker is put in replay mode, and the expectation is satisfied by
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   356
    calling the C{hello()} method, which indeed returns 10.  Finally,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   357
    a call to the L{restore()} method is performed to undo any needed
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   358
    changes made in the environment, and the L{verify()} method is
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   359
    called to ensure that all defined expectations were met.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   360
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   361
    The same logic can be expressed more elegantly using the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   362
    C{with mocker:} statement, as follows::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   363
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   364
        mock = mocker.mock()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   365
        mock.hello()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   366
        mocker.result("Hi!")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   367
        with mocker:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   368
            assert mock.hello() == "Hi!"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   369
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   370
    Also, the MockerTestCase class, which integrates the mocker on
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   371
    a unittest.TestCase subclass, may be used to reduce the overhead
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   372
    of controlling the mocker.  A test could be written as follows::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   373
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   374
        class SampleTest(MockerTestCase):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   375
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   376
            def test_hello(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   377
                mock = self.mocker.mock()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   378
                mock.hello()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   379
                self.mocker.result("Hi!")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   380
                self.mocker.replay()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   381
                self.assertEquals(mock.hello(), "Hi!")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   382
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   383
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   384
    _recorders = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   385
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   386
    # For convenience only.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   387
    on = expect
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   388
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   389
    class __metaclass__(type):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   390
        def __init__(self, name, bases, dict):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   391
            # Make independent lists on each subclass, inheriting from parent.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   392
            self._recorders = list(getattr(self, "_recorders", ()))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   393
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   394
    def __init__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   395
        self._recorders = self._recorders[:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   396
        self._events = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   397
        self._recording = True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   398
        self._ordering = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   399
        self._last_orderer = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   400
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   401
    def is_recording(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   402
        """Return True if in recording mode, False if in replay mode.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   403
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   404
        Recording is the initial state.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   405
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   406
        return self._recording
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   407
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   408
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   409
        """Change to replay mode, where recorded events are reproduced.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   410
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   411
        If already in replay mode, the mocker will be restored, with all
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   412
        expectations reset, and then put again in replay mode.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   413
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   414
        An alternative and more comfortable way to replay changes is
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   415
        using the 'with' statement, as follows::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   416
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   417
            mocker = Mocker()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   418
            <record events>
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   419
            with mocker:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   420
                <reproduce events>
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   421
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   422
        The 'with' statement will automatically put mocker in replay
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   423
        mode, and will also verify if all events were correctly reproduced
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   424
        at the end (using L{verify()}), and also restore any changes done
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   425
        in the environment (with L{restore()}).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   426
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   427
        Also check the MockerTestCase class, which integrates the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   428
        unittest.TestCase class with mocker.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   429
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   430
        if not self._recording:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   431
            for event in self._events:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   432
                event.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   433
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   434
            self._recording = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   435
        for event in self._events:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   436
            event.replay()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   437
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   438
    def restore(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   439
        """Restore changes in the environment, and return to recording mode.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   440
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   441
        This should always be called after the test is complete (succeeding
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   442
        or not).  There are ways to call this method automatically on
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   443
        completion (e.g. using a C{with mocker:} statement, or using the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   444
        L{MockerTestCase} class.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   445
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   446
        if not self._recording:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   447
            self._recording = True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   448
            for event in self._events:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   449
                event.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   450
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   451
    def reset(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   452
        """Reset the mocker state.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   453
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   454
        This will restore environment changes, if currently in replay
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   455
        mode, and then remove all events previously recorded.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   456
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   457
        if not self._recording:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   458
            self.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   459
        self.unorder()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   460
        del self._events[:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   461
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   462
    def get_events(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   463
        """Return all recorded events."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   464
        return self._events[:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   465
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   466
    def add_event(self, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   467
        """Add an event.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   468
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   469
        This method is used internally by the implementation, and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   470
        shouldn't be needed on normal mocker usage.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   471
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   472
        self._events.append(event)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   473
        if self._ordering:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   474
            orderer = event.add_task(Orderer(event.path))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   475
            if self._last_orderer:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   476
                orderer.add_dependency(self._last_orderer)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   477
            self._last_orderer = orderer
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   478
        return event
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   479
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   480
    def verify(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   481
        """Check if all expectations were met, and raise AssertionError if not.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   482
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   483
        The exception message will include a nice description of which
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   484
        expectations were not met, and why.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   485
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   486
        errors = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   487
        for event in self._events:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   488
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   489
                event.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   490
            except AssertionError, e:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   491
                error = str(e)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   492
                if not error:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   493
                    raise RuntimeError("Empty error message from %r"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   494
                                       % event)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   495
                errors.append(error)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   496
        if errors:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   497
            message = [ERROR_PREFIX + "Unmet expectations:", ""]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   498
            for error in errors:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   499
                lines = error.splitlines()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   500
                message.append("=> " + lines.pop(0))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   501
                message.extend([" " + line for line in lines])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   502
                message.append("")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   503
            raise AssertionError(os.linesep.join(message))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   504
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   505
    def mock(self, spec_and_type=None, spec=None, type=None,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   506
             name=None, count=True):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   507
        """Return a new mock object.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   508
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   509
        @param spec_and_type: Handy positional argument which sets both
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   510
                     spec and type.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   511
        @param spec: Method calls will be checked for correctness against
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   512
                     the given class.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   513
        @param type: If set, the Mock's __class__ attribute will return
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   514
                     the given type.  This will make C{isinstance()} calls
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   515
                     on the object work.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   516
        @param name: Name for the mock object, used in the representation of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   517
                     expressions.  The name is rarely needed, as it's usually
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   518
                     guessed correctly from the variable name used.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   519
        @param count: If set to false, expressions may be executed any number
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   520
                     of times, unless an expectation is explicitly set using
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   521
                     the L{count()} method.  By default, expressions are
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   522
                     expected once.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   523
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   524
        if spec_and_type is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   525
            spec = type = spec_and_type
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   526
        return Mock(self, spec=spec, type=type, name=name, count=count)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   527
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   528
    def proxy(self, object, spec=True, type=True, name=None, count=True,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   529
              passthrough=True):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   530
        """Return a new mock object which proxies to the given object.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   531
 
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   532
        Proxies are useful when only part of the behavior of an object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   533
        is to be mocked.  Unknown expressions may be passed through to
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   534
        the real implementation implicitly (if the C{passthrough} argument
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   535
        is True), or explicitly (using the L{passthrough()} method
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   536
        on the event).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   537
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   538
        @param object: Real object to be proxied, and replaced by the mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   539
                       on replay mode.  It may also be an "import path",
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   540
                       such as C{"time.time"}, in which case the object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   541
                       will be the C{time} function from the C{time} module.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   542
        @param spec: Method calls will be checked for correctness against
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   543
                     the given object, which may be a class or an instance
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   544
                     where attributes will be looked up.  Defaults to the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   545
                     the C{object} parameter.  May be set to None explicitly,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   546
                     in which case spec checking is disabled.  Checks may
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   547
                     also be disabled explicitly on a per-event basis with
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   548
                     the L{nospec()} method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   549
        @param type: If set, the Mock's __class__ attribute will return
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   550
                     the given type.  This will make C{isinstance()} calls
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   551
                     on the object work.  Defaults to the type of the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   552
                     C{object} parameter.  May be set to None explicitly.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   553
        @param name: Name for the mock object, used in the representation of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   554
                     expressions.  The name is rarely needed, as it's usually
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   555
                     guessed correctly from the variable name used.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   556
        @param count: If set to false, expressions may be executed any number
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   557
                     of times, unless an expectation is explicitly set using
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   558
                     the L{count()} method.  By default, expressions are
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   559
                     expected once.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   560
        @param passthrough: If set to False, passthrough of actions on the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   561
                            proxy to the real object will only happen when
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   562
                            explicitly requested via the L{passthrough()}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   563
                            method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   564
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   565
        if isinstance(object, basestring):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   566
            if name is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   567
                name = object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   568
            import_stack = object.split(".")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   569
            attr_stack = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   570
            while import_stack:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   571
                module_path = ".".join(import_stack)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   572
                try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   573
                    object = __import__(module_path, {}, {}, [""])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   574
                except ImportError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   575
                    attr_stack.insert(0, import_stack.pop())
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   576
                    if not import_stack:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   577
                        raise
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   578
                    continue
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   579
                else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   580
                    for attr in attr_stack:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   581
                        object = getattr(object, attr)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   582
                    break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   583
        if spec is True:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   584
            spec = object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   585
        if type is True:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   586
            type = __builtin__.type(object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   587
        return Mock(self, spec=spec, type=type, object=object,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   588
                    name=name, count=count, passthrough=passthrough)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   589
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   590
    def replace(self, object, spec=True, type=True, name=None, count=True,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   591
                passthrough=True):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   592
        """Create a proxy, and replace the original object with the mock.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   593
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   594
        On replay, the original object will be replaced by the returned
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   595
        proxy in all dictionaries found in the running interpreter via
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   596
        the garbage collecting system.  This should cover module
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   597
        namespaces, class namespaces, instance namespaces, and so on.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   598
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   599
        @param object: Real object to be proxied, and replaced by the mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   600
                       on replay mode.  It may also be an "import path",
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   601
                       such as C{"time.time"}, in which case the object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   602
                       will be the C{time} function from the C{time} module.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   603
        @param spec: Method calls will be checked for correctness against
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   604
                     the given object, which may be a class or an instance
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   605
                     where attributes will be looked up.  Defaults to the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   606
                     the C{object} parameter.  May be set to None explicitly,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   607
                     in which case spec checking is disabled.  Checks may
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   608
                     also be disabled explicitly on a per-event basis with
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   609
                     the L{nospec()} method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   610
        @param type: If set, the Mock's __class__ attribute will return
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   611
                     the given type.  This will make C{isinstance()} calls
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   612
                     on the object work.  Defaults to the type of the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   613
                     C{object} parameter.  May be set to None explicitly.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   614
        @param name: Name for the mock object, used in the representation of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   615
                     expressions.  The name is rarely needed, as it's usually
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   616
                     guessed correctly from the variable name used.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   617
        @param passthrough: If set to False, passthrough of actions on the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   618
                            proxy to the real object will only happen when
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   619
                            explicitly requested via the L{passthrough()}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   620
                            method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   621
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   622
        mock = self.proxy(object, spec, type, name, count, passthrough)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   623
        event = self._get_replay_restore_event()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   624
        event.add_task(ProxyReplacer(mock))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   625
        return mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   626
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   627
    def patch(self, object, spec=True):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   628
        """Patch an existing object to reproduce recorded events.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   629
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   630
        @param object: Class or instance to be patched.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   631
        @param spec: Method calls will be checked for correctness against
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   632
                     the given object, which may be a class or an instance
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   633
                     where attributes will be looked up.  Defaults to the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   634
                     the C{object} parameter.  May be set to None explicitly,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   635
                     in which case spec checking is disabled.  Checks may
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   636
                     also be disabled explicitly on a per-event basis with
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   637
                     the L{nospec()} method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   638
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   639
        The result of this method is still a mock object, which can be
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   640
        used like any other mock object to record events.  The difference
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   641
        is that when the mocker is put on replay mode, the *real* object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   642
        will be modified to behave according to recorded expectations.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   643
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   644
        Patching works in individual instances, and also in classes.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   645
        When an instance is patched, recorded events will only be
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   646
        considered on this specific instance, and other instances should
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   647
        behave normally.  When a class is patched, the reproduction of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   648
        events will be considered on any instance of this class once
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   649
        created (collectively).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   650
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   651
        Observe that, unlike with proxies which catch only events done
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   652
        through the mock object, *all* accesses to recorded expectations
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   653
        will be considered;  even these coming from the object itself
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   654
        (e.g. C{self.hello()} is considered if this method was patched).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   655
        While this is a very powerful feature, and many times the reason
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   656
        to use patches in the first place, it's important to keep this
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   657
        behavior in mind.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   658
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   659
        Patching of the original object only takes place when the mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   660
        is put on replay mode, and the patched object will be restored
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   661
        to its original state once the L{restore()} method is called
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   662
        (explicitly, or implicitly with alternative conventions, such as
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   663
        a C{with mocker:} block, or a MockerTestCase class).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   664
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   665
        if spec is True:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   666
            spec = object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   667
        patcher = Patcher()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   668
        event = self._get_replay_restore_event()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   669
        event.add_task(patcher)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   670
        mock = Mock(self, object=object, patcher=patcher,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   671
                    passthrough=True, spec=spec)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   672
        object.__mocker_mock__ = mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   673
        return mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   674
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   675
    def act(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   676
        """This is called by mock objects whenever something happens to them.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   677
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   678
        This method is part of the implementation between the mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   679
        and mock objects.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   680
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   681
        if self._recording:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   682
            event = self.add_event(Event(path))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   683
            for recorder in self._recorders:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   684
                recorder(self, event)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   685
            return Mock(self, path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   686
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   687
            # First run events that may run, then run unsatisfied events, then
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   688
            # ones not previously run. We put the index in the ordering tuple
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   689
            # instead of the actual event because we want a stable sort
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   690
            # (ordering between 2 events is undefined).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   691
            events = self._events
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   692
            order = [(events[i].satisfied()*2 + events[i].has_run(), i)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   693
                     for i in range(len(events))]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   694
            order.sort()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   695
            postponed = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   696
            for weight, i in order:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   697
                event = events[i]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   698
                if event.matches(path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   699
                    if event.may_run(path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   700
                        return event.run(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   701
                    elif postponed is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   702
                        postponed = event
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   703
            if postponed is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   704
                return postponed.run(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   705
            raise MatchError(ERROR_PREFIX + "Unexpected expression: %s" % path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   706
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   707
    def get_recorders(cls, self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   708
        """Return recorders associated with this mocker class or instance.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   709
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   710
        This method may be called on mocker instances and also on mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   711
        classes.  See the L{add_recorder()} method for more information.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   712
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   713
        return (self or cls)._recorders[:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   714
    get_recorders = classinstancemethod(get_recorders)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   715
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   716
    def add_recorder(cls, self, recorder):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   717
        """Add a recorder to this mocker class or instance.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   718
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   719
        @param recorder: Callable accepting C{(mocker, event)} as parameters.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   720
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   721
        This is part of the implementation of mocker.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   722
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   723
        All registered recorders are called for translating events that
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   724
        happen during recording into expectations to be met once the state
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   725
        is switched to replay mode.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   726
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   727
        This method may be called on mocker instances and also on mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   728
        classes.  When called on a class, the recorder will be used by
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   729
        all instances, and also inherited on subclassing.  When called on
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   730
        instances, the recorder is added only to the given instance.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   731
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   732
        (self or cls)._recorders.append(recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   733
        return recorder
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   734
    add_recorder = classinstancemethod(add_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   735
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   736
    def remove_recorder(cls, self, recorder):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   737
        """Remove the given recorder from this mocker class or instance.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   738
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   739
        This method may be called on mocker classes and also on mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   740
        instances.  See the L{add_recorder()} method for more information.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   741
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   742
        (self or cls)._recorders.remove(recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   743
    remove_recorder = classinstancemethod(remove_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   744
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   745
    def result(self, value):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   746
        """Make the last recorded event return the given value on replay.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   747
        
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   748
        @param value: Object to be returned when the event is replayed.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   749
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   750
        self.call(lambda *args, **kwargs: value)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   751
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   752
    def generate(self, sequence):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   753
        """Last recorded event will return a generator with the given sequence.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   754
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   755
        @param sequence: Sequence of values to be generated.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   756
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   757
        def generate(*args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   758
            for value in sequence:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   759
                yield value
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   760
        self.call(generate)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   761
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   762
    def throw(self, exception):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   763
        """Make the last recorded event raise the given exception on replay.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   764
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   765
        @param exception: Class or instance of exception to be raised.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   766
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   767
        def raise_exception(*args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   768
            raise exception
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   769
        self.call(raise_exception)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   770
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   771
    def call(self, func):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   772
        """Make the last recorded event cause the given function to be called.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   773
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   774
        @param func: Function to be called.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   775
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   776
        The result of the function will be used as the event result.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   777
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   778
        self._events[-1].add_task(FunctionRunner(func))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   779
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   780
    def count(self, min, max=False):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   781
        """Last recorded event must be replayed between min and max times.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   782
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   783
        @param min: Minimum number of times that the event must happen.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   784
        @param max: Maximum number of times that the event must happen.  If
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   785
                    not given, it defaults to the same value of the C{min}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   786
                    parameter.  If set to None, there is no upper limit, and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   787
                    the expectation is met as long as it happens at least
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   788
                    C{min} times.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   789
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   790
        event = self._events[-1]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   791
        for task in event.get_tasks():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   792
            if isinstance(task, RunCounter):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   793
                event.remove_task(task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   794
        event.add_task(RunCounter(min, max))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   795
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   796
    def is_ordering(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   797
        """Return true if all events are being ordered.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   798
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   799
        See the L{order()} method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   800
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   801
        return self._ordering
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   802
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   803
    def unorder(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   804
        """Disable the ordered mode.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   805
        
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   806
        See the L{order()} method for more information.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   807
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   808
        self._ordering = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   809
        self._last_orderer = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   810
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   811
    def order(self, *path_holders):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   812
        """Create an expectation of order between two or more events.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   813
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   814
        @param path_holders: Objects returned as the result of recorded events.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   815
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   816
        By default, mocker won't force events to happen precisely in
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   817
        the order they were recorded.  Calling this method will change
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   818
        this behavior so that events will only match if reproduced in
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   819
        the correct order.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   820
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   821
        There are two ways in which this method may be used.  Which one
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   822
        is used in a given occasion depends only on convenience.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   823
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   824
        If no arguments are passed, the mocker will be put in a mode where
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   825
        all the recorded events following the method call will only be met
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   826
        if they happen in order.  When that's used, the mocker may be put
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   827
        back in unordered mode by calling the L{unorder()} method, or by
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   828
        using a 'with' block, like so::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   829
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   830
            with mocker.ordered():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   831
                <record events>
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   832
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   833
        In this case, only expressions in <record events> will be ordered,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   834
        and the mocker will be back in unordered mode after the 'with' block.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   835
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   836
        The second way to use it is by specifying precisely which events
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   837
        should be ordered.  As an example::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   838
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   839
            mock = mocker.mock()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   840
            expr1 = mock.hello()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   841
            expr2 = mock.world
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   842
            expr3 = mock.x.y.z
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   843
            mocker.order(expr1, expr2, expr3)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   844
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   845
        This method of ordering only works when the expression returns
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   846
        another object.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   847
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   848
        Also check the L{after()} and L{before()} methods, which are
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   849
        alternative ways to perform this.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   850
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   851
        if not path_holders:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   852
            self._ordering = True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   853
            return OrderedContext(self)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   854
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   855
        last_orderer = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   856
        for path_holder in path_holders:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   857
            if type(path_holder) is Path:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   858
                path = path_holder
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   859
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   860
                path = path_holder.__mocker_path__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   861
            for event in self._events:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   862
                if event.path is path:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   863
                    for task in event.get_tasks():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   864
                        if isinstance(task, Orderer):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   865
                            orderer = task
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   866
                            break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   867
                    else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   868
                        orderer = Orderer(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   869
                        event.add_task(orderer)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   870
                    if last_orderer:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   871
                        orderer.add_dependency(last_orderer)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   872
                    last_orderer = orderer
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   873
                    break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   874
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   875
    def after(self, *path_holders):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   876
        """Last recorded event must happen after events referred to.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   877
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   878
        @param path_holders: Objects returned as the result of recorded events
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   879
                             which should happen before the last recorded event
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   880
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   881
        As an example, the idiom::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   882
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   883
            expect(mock.x).after(mock.y, mock.z)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   884
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   885
        is an alternative way to say::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   886
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   887
            expr_x = mock.x
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   888
            expr_y = mock.y
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   889
            expr_z = mock.z
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   890
            mocker.order(expr_y, expr_x)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   891
            mocker.order(expr_z, expr_x)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   892
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   893
        See L{order()} for more information.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   894
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   895
        last_path = self._events[-1].path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   896
        for path_holder in path_holders:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   897
            self.order(path_holder, last_path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   898
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   899
    def before(self, *path_holders):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   900
        """Last recorded event must happen before events referred to.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   901
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   902
        @param path_holders: Objects returned as the result of recorded events
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   903
                             which should happen after the last recorded event
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   904
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   905
        As an example, the idiom::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   906
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   907
            expect(mock.x).before(mock.y, mock.z)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   908
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   909
        is an alternative way to say::
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   910
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   911
            expr_x = mock.x
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   912
            expr_y = mock.y
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   913
            expr_z = mock.z
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   914
            mocker.order(expr_x, expr_y)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   915
            mocker.order(expr_x, expr_z)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   916
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   917
        See L{order()} for more information.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   918
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   919
        last_path = self._events[-1].path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   920
        for path_holder in path_holders:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   921
            self.order(last_path, path_holder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   922
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   923
    def nospec(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   924
        """Don't check method specification of real object on last event.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   925
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   926
        By default, when using a mock created as the result of a call to
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   927
        L{proxy()}, L{replace()}, and C{patch()}, or when passing the spec
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   928
        attribute to the L{mock()} method, method calls on the given object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   929
        are checked for correctness against the specification of the real
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   930
        object (or the explicitly provided spec).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   931
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   932
        This method will disable that check specifically for the last
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   933
        recorded event.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   934
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   935
        event = self._events[-1]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   936
        for task in event.get_tasks():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   937
            if isinstance(task, SpecChecker):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   938
                event.remove_task(task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   939
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   940
    def passthrough(self, result_callback=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   941
        """Make the last recorded event run on the real object once seen.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   942
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   943
        @param result_callback: If given, this function will be called with
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   944
            the result of the *real* method call as the only argument.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   945
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   946
        This can only be used on proxies, as returned by the L{proxy()}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   947
        and L{replace()} methods, or on mocks representing patched objects,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   948
        as returned by the L{patch()} method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   949
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   950
        event = self._events[-1]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   951
        if event.path.root_object is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   952
            raise TypeError("Mock object isn't a proxy")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   953
        event.add_task(PathExecuter(result_callback))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   954
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   955
    def __enter__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   956
        """Enter in a 'with' context.  This will run replay()."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   957
        self.replay()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   958
        return self
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   959
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   960
    def __exit__(self, type, value, traceback):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   961
        """Exit from a 'with' context.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   962
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   963
        This will run restore() at all times, but will only run verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   964
        if the 'with' block itself hasn't raised an exception.  Exceptions
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   965
        in that block are never swallowed.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   966
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   967
        self.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   968
        if type is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   969
            self.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   970
        return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   971
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   972
    def _get_replay_restore_event(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   973
        """Return unique L{ReplayRestoreEvent}, creating if needed.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   974
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   975
        Some tasks only want to replay/restore.  When that's the case,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   976
        they shouldn't act on other events during replay.  Also, they
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   977
        can all be put in a single event when that's the case.  Thus,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   978
        we add a single L{ReplayRestoreEvent} as the first element of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   979
        the list.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   980
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   981
        if not self._events or type(self._events[0]) != ReplayRestoreEvent:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   982
            self._events.insert(0, ReplayRestoreEvent())
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   983
        return self._events[0]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   984
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   985
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   986
class OrderedContext(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   987
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   988
    def __init__(self, mocker):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   989
        self._mocker = mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   990
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   991
    def __enter__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   992
        return None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   993
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   994
    def __exit__(self, type, value, traceback):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   995
        self._mocker.unorder()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   996
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   997
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   998
class Mocker(MockerBase):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
   999
    __doc__ = MockerBase.__doc__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1000
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1001
# Decorator to add recorders on the standard Mocker class.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1002
recorder = Mocker.add_recorder
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1003
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1004
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1005
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1006
# Mock object.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1007
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1008
class Mock(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1009
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1010
    def __init__(self, mocker, path=None, name=None, spec=None, type=None,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1011
                 object=None, passthrough=False, patcher=None, count=True):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1012
        self.__mocker__ = mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1013
        self.__mocker_path__ = path or Path(self, object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1014
        self.__mocker_name__ = name
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1015
        self.__mocker_spec__ = spec
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1016
        self.__mocker_object__ = object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1017
        self.__mocker_passthrough__ = passthrough
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1018
        self.__mocker_patcher__ = patcher
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1019
        self.__mocker_replace__ = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1020
        self.__mocker_type__ = type
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1021
        self.__mocker_count__ = count
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1022
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1023
    def __mocker_act__(self, kind, args=(), kwargs={}, object=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1024
        if self.__mocker_name__ is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1025
            self.__mocker_name__ = find_object_name(self, 2)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1026
        action = Action(kind, args, kwargs, self.__mocker_path__)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1027
        path = self.__mocker_path__ + action
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1028
        if object is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1029
            path.root_object = object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1030
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1031
            return self.__mocker__.act(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1032
        except MatchError, exception:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1033
            root_mock = path.root_mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1034
            if (path.root_object is not None and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1035
                root_mock.__mocker_passthrough__):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1036
                return path.execute(path.root_object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1037
            # Reinstantiate to show raise statement on traceback, and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1038
            # also to make the traceback shown shorter.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1039
            raise MatchError(str(exception))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1040
        except AssertionError, e:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1041
            lines = str(e).splitlines()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1042
            message = [ERROR_PREFIX + "Unmet expectation:", ""]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1043
            message.append("=> " + lines.pop(0))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1044
            message.extend([" " + line for line in lines])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1045
            message.append("")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1046
            raise AssertionError(os.linesep.join(message))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1047
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1048
    def __getattribute__(self, name):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1049
        if name.startswith("__mocker_"):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1050
            return super(Mock, self).__getattribute__(name)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1051
        if name == "__class__":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1052
            if self.__mocker__.is_recording() or self.__mocker_type__ is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1053
                return type(self)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1054
            return self.__mocker_type__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1055
        return self.__mocker_act__("getattr", (name,))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1056
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1057
    def __setattr__(self, name, value):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1058
        if name.startswith("__mocker_"):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1059
            return super(Mock, self).__setattr__(name, value)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1060
        return self.__mocker_act__("setattr", (name, value))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1061
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1062
    def __delattr__(self, name):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1063
        return self.__mocker_act__("delattr", (name,))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1064
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1065
    def __call__(self, *args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1066
        return self.__mocker_act__("call", args, kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1067
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1068
    def __contains__(self, value):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1069
        return self.__mocker_act__("contains", (value,))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1070
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1071
    def __getitem__(self, key):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1072
        return self.__mocker_act__("getitem", (key,))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1073
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1074
    def __setitem__(self, key, value):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1075
        return self.__mocker_act__("setitem", (key, value))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1076
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1077
    def __delitem__(self, key):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1078
        return self.__mocker_act__("delitem", (key,))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1079
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1080
    def __len__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1081
        # MatchError is turned on an AttributeError so that list() and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1082
        # friends act properly when trying to get length hints on
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1083
        # something that doesn't offer them.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1084
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1085
            result = self.__mocker_act__("len")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1086
        except MatchError, e:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1087
            raise AttributeError(str(e))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1088
        if type(result) is Mock:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1089
            return 0
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1090
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1091
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1092
    def __nonzero__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1093
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1094
            return self.__mocker_act__("nonzero")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1095
        except MatchError, e:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1096
            return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1097
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1098
    def __iter__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1099
        # XXX On py3k, when next() becomes __next__(), we'll be able
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1100
        #     to return the mock itself because it will be considered
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1101
        #     an iterator (we'll be mocking __next__ as well, which we
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1102
        #     can't now).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1103
        result = self.__mocker_act__("iter")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1104
        if type(result) is Mock:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1105
            return iter([])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1106
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1107
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1108
    # When adding a new action kind here, also add support for it on
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1109
    # Action.execute() and Path.__str__().
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1110
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1111
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1112
def find_object_name(obj, depth=0):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1113
    """Try to detect how the object is named on a previous scope."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1114
    try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1115
        frame = sys._getframe(depth+1)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1116
    except:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1117
        return None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1118
    for name, frame_obj in frame.f_locals.iteritems():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1119
        if frame_obj is obj:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1120
            return name
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1121
    self = frame.f_locals.get("self")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1122
    if self is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1123
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1124
            items = list(self.__dict__.iteritems())
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1125
        except:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1126
            pass
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1127
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1128
            for name, self_obj in items:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1129
                if self_obj is obj:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1130
                    return name
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1131
    return None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1132
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1133
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1134
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1135
# Action and path.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1136
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1137
class Action(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1138
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1139
    def __init__(self, kind, args, kwargs, path=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1140
        self.kind = kind
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1141
        self.args = args
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1142
        self.kwargs = kwargs
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1143
        self.path = path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1144
        self._execute_cache = {}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1145
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1146
    def __repr__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1147
        if self.path is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1148
            return "Action(%r, %r, %r)" % (self.kind, self.args, self.kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1149
        return "Action(%r, %r, %r, %r)" % \
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1150
               (self.kind, self.args, self.kwargs, self.path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1151
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1152
    def __eq__(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1153
        return (self.kind == other.kind and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1154
                self.args == other.args and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1155
                self.kwargs == other.kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1156
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1157
    def __ne__(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1158
        return not self.__eq__(other)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1159
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1160
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1161
        return (self.kind == other.kind and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1162
                match_params(self.args, self.kwargs, other.args, other.kwargs))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1163
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1164
    def execute(self, object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1165
        # This caching scheme may fail if the object gets deallocated before
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1166
        # the action, as the id might get reused.  It's somewhat easy to fix
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1167
        # that with a weakref callback.  For our uses, though, the object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1168
        # should never get deallocated before the action itself, so we'll
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1169
        # just keep it simple.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1170
        if id(object) in self._execute_cache:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1171
            return self._execute_cache[id(object)]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1172
        execute = getattr(object, "__mocker_execute__", None)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1173
        if execute is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1174
            result = execute(self, object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1175
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1176
            kind = self.kind
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1177
            if kind == "getattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1178
                result = getattr(object, self.args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1179
            elif kind == "setattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1180
                result = setattr(object, self.args[0], self.args[1])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1181
            elif kind == "delattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1182
                result = delattr(object, self.args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1183
            elif kind == "call":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1184
                result = object(*self.args, **self.kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1185
            elif kind == "contains":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1186
                result = self.args[0] in object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1187
            elif kind == "getitem":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1188
                result = object[self.args[0]]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1189
            elif kind == "setitem":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1190
                result = object[self.args[0]] = self.args[1]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1191
            elif kind == "delitem":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1192
                del object[self.args[0]]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1193
                result = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1194
            elif kind == "len":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1195
                result = len(object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1196
            elif kind == "nonzero":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1197
                result = bool(object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1198
            elif kind == "iter":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1199
                result = iter(object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1200
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1201
                raise RuntimeError("Don't know how to execute %r kind." % kind)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1202
        self._execute_cache[id(object)] = result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1203
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1204
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1205
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1206
class Path(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1207
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1208
    def __init__(self, root_mock, root_object=None, actions=()):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1209
        self.root_mock = root_mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1210
        self.root_object = root_object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1211
        self.actions = tuple(actions)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1212
        self.__mocker_replace__ = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1213
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1214
    def parent_path(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1215
        if not self.actions:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1216
            return None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1217
        return self.actions[-1].path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1218
    parent_path = property(parent_path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1219
 
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1220
    def __add__(self, action):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1221
        """Return a new path which includes the given action at the end."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1222
        return self.__class__(self.root_mock, self.root_object,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1223
                              self.actions + (action,))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1224
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1225
    def __eq__(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1226
        """Verify if the two paths are equal.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1227
        
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1228
        Two paths are equal if they refer to the same mock object, and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1229
        have the actions with equal kind, args and kwargs.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1230
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1231
        if (self.root_mock is not other.root_mock or
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1232
            self.root_object is not other.root_object or
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1233
            len(self.actions) != len(other.actions)):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1234
            return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1235
        for action, other_action in zip(self.actions, other.actions):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1236
            if action != other_action:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1237
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1238
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1239
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1240
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1241
        """Verify if the two paths are equivalent.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1242
        
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1243
        Two paths are equal if they refer to the same mock object, and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1244
        have the same actions performed on them.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1245
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1246
        if (self.root_mock is not other.root_mock or
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1247
            len(self.actions) != len(other.actions)):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1248
            return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1249
        for action, other_action in zip(self.actions, other.actions):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1250
            if not action.matches(other_action):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1251
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1252
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1253
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1254
    def execute(self, object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1255
        """Execute all actions sequentially on object, and return result.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1256
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1257
        for action in self.actions:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1258
            object = action.execute(object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1259
        return object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1260
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1261
    def __str__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1262
        """Transform the path into a nice string such as obj.x.y('z')."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1263
        result = self.root_mock.__mocker_name__ or "<mock>"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1264
        for action in self.actions:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1265
            if action.kind == "getattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1266
                result = "%s.%s" % (result, action.args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1267
            elif action.kind == "setattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1268
                result = "%s.%s = %r" % (result, action.args[0], action.args[1])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1269
            elif action.kind == "delattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1270
                result = "del %s.%s" % (result, action.args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1271
            elif action.kind == "call":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1272
                args = [repr(x) for x in action.args]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1273
                items = list(action.kwargs.iteritems())
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1274
                items.sort()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1275
                for pair in items:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1276
                    args.append("%s=%r" % pair)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1277
                result = "%s(%s)" % (result, ", ".join(args))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1278
            elif action.kind == "contains":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1279
                result = "%r in %s" % (action.args[0], result)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1280
            elif action.kind == "getitem":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1281
                result = "%s[%r]" % (result, action.args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1282
            elif action.kind == "setitem":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1283
                result = "%s[%r] = %r" % (result, action.args[0],
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1284
                                          action.args[1])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1285
            elif action.kind == "delitem":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1286
                result = "del %s[%r]" % (result, action.args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1287
            elif action.kind == "len":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1288
                result = "len(%s)" % result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1289
            elif action.kind == "nonzero":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1290
                result = "bool(%s)" % result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1291
            elif action.kind == "iter":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1292
                result = "iter(%s)" % result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1293
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1294
                raise RuntimeError("Don't know how to format kind %r" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1295
                                   action.kind)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1296
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1297
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1298
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1299
class SpecialArgument(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1300
    """Base for special arguments for matching parameters."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1301
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1302
    def __init__(self, object=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1303
        self.object = object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1304
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1305
    def __repr__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1306
        if self.object is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1307
            return self.__class__.__name__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1308
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1309
            return "%s(%r)" % (self.__class__.__name__, self.object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1310
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1311
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1312
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1313
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1314
    def __eq__(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1315
        return type(other) == type(self) and self.object == other.object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1316
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1317
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1318
class ANY(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1319
    """Matches any single argument."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1320
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1321
ANY = ANY()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1322
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1323
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1324
class ARGS(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1325
    """Matches zero or more positional arguments."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1326
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1327
ARGS = ARGS()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1328
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1329
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1330
class KWARGS(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1331
    """Matches zero or more keyword arguments."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1332
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1333
KWARGS = KWARGS()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1334
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1335
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1336
class IS(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1337
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1338
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1339
        return self.object is other
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1340
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1341
    def __eq__(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1342
        return type(other) == type(self) and self.object is other.object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1343
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1344
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1345
class CONTAINS(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1346
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1347
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1348
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1349
            other.__contains__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1350
        except AttributeError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1351
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1352
                iter(other)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1353
            except TypeError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1354
                # If an object can't be iterated, and has no __contains__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1355
                # hook, it'd blow up on the test below.  We test this in
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1356
                # advance to prevent catching more errors than we really
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1357
                # want.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1358
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1359
        return self.object in other
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1360
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1361
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1362
class IN(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1363
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1364
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1365
        return other in self.object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1366
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1367
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1368
class MATCH(SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1369
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1370
    def matches(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1371
        return bool(self.object(other))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1372
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1373
    def __eq__(self, other):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1374
        return type(other) == type(self) and self.object is other.object
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1375
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1376
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1377
def match_params(args1, kwargs1, args2, kwargs2):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1378
    """Match the two sets of parameters, considering special parameters."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1379
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1380
    has_args = ARGS in args1
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1381
    has_kwargs = KWARGS in args1
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1382
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1383
    if has_kwargs:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1384
        args1 = [arg1 for arg1 in args1 if arg1 is not KWARGS]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1385
    elif len(kwargs1) != len(kwargs2):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1386
        return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1387
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1388
    if not has_args and len(args1) != len(args2):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1389
        return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1390
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1391
    # Either we have the same number of kwargs, or unknown keywords are
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1392
    # accepted (KWARGS was used), so check just the ones in kwargs1.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1393
    for key, arg1 in kwargs1.iteritems():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1394
        if key not in kwargs2:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1395
            return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1396
        arg2 = kwargs2[key]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1397
        if isinstance(arg1, SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1398
            if not arg1.matches(arg2):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1399
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1400
        elif arg1 != arg2:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1401
            return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1402
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1403
    # Keywords match.  Now either we have the same number of
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1404
    # arguments, or ARGS was used.  If ARGS wasn't used, arguments
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1405
    # must match one-on-one necessarily.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1406
    if not has_args:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1407
        for arg1, arg2 in zip(args1, args2):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1408
            if isinstance(arg1, SpecialArgument):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1409
                if not arg1.matches(arg2):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1410
                    return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1411
            elif arg1 != arg2:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1412
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1413
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1414
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1415
    # Easy choice. Keywords are matching, and anything on args is accepted.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1416
    if (ARGS,) == args1:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1417
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1418
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1419
    # We have something different there. If we don't have positional
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1420
    # arguments on the original call, it can't match.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1421
    if not args2:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1422
        # Unless we have just several ARGS (which is bizarre, but..).
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1423
        for arg1 in args1:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1424
            if arg1 is not ARGS:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1425
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1426
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1427
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1428
    # Ok, all bets are lost.  We have to actually do the more expensive
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1429
    # matching.  This is an algorithm based on the idea of the Levenshtein
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1430
    # Distance between two strings, but heavily hacked for this purpose.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1431
    args2l = len(args2)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1432
    if args1[0] is ARGS:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1433
        args1 = args1[1:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1434
        array = [0]*args2l
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1435
    else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1436
        array = [1]*args2l
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1437
    for i in range(len(args1)):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1438
        last = array[0]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1439
        if args1[i] is ARGS:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1440
            for j in range(1, args2l):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1441
                last, array[j] = array[j], min(array[j-1], array[j], last)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1442
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1443
            array[0] = i or int(args1[i] != args2[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1444
            for j in range(1, args2l):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1445
                last, array[j] = array[j], last or int(args1[i] != args2[j])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1446
        if 0 not in array:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1447
            return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1448
    if array[-1] != 0:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1449
        return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1450
    return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1451
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1452
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1453
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1454
# Event and task base.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1455
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1456
class Event(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1457
    """Aggregation of tasks that keep track of a recorded action.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1458
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1459
    An event represents something that may or may not happen while the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1460
    mocked environment is running, such as an attribute access, or a
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1461
    method call.  The event is composed of several tasks that are
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1462
    orchestrated together to create a composed meaning for the event,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1463
    including for which actions it should be run, what happens when it
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1464
    runs, and what's the expectations about the actions run.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1465
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1466
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1467
    def __init__(self, path=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1468
        self.path = path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1469
        self._tasks = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1470
        self._has_run = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1471
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1472
    def add_task(self, task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1473
        """Add a new task to this taks."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1474
        self._tasks.append(task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1475
        return task
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1476
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1477
    def remove_task(self, task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1478
        self._tasks.remove(task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1479
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1480
    def get_tasks(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1481
        return self._tasks[:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1482
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1483
    def matches(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1484
        """Return true if *all* tasks match the given path."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1485
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1486
            if not task.matches(path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1487
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1488
        return bool(self._tasks)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1489
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1490
    def has_run(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1491
        return self._has_run
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1492
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1493
    def may_run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1494
        """Verify if any task would certainly raise an error if run.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1495
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1496
        This will call the C{may_run()} method on each task and return
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1497
        false if any of them returns false.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1498
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1499
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1500
            if not task.may_run(path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1501
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1502
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1503
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1504
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1505
        """Run all tasks with the given action.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1506
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1507
        @param path: The path of the expression run.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1508
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1509
        Running an event means running all of its tasks individually and in
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1510
        order.  An event should only ever be run if all of its tasks claim to
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1511
        match the given action.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1512
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1513
        The result of this method will be the last result of a task
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1514
        which isn't None, or None if they're all None.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1515
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1516
        self._has_run = True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1517
        result = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1518
        errors = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1519
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1520
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1521
                task_result = task.run(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1522
            except AssertionError, e:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1523
                error = str(e)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1524
                if not error:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1525
                    raise RuntimeError("Empty error message from %r" % task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1526
                errors.append(error)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1527
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1528
                if task_result is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1529
                    result = task_result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1530
        if errors:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1531
            message = [str(self.path)]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1532
            if str(path) != message[0]:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1533
                message.append("- Run: %s" % path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1534
            for error in errors:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1535
                lines = error.splitlines()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1536
                message.append("- " + lines.pop(0))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1537
                message.extend(["  " + line for line in lines])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1538
            raise AssertionError(os.linesep.join(message))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1539
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1540
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1541
    def satisfied(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1542
        """Return true if all tasks are satisfied.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1543
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1544
        Being satisfied means that there are no unmet expectations.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1545
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1546
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1547
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1548
                task.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1549
            except AssertionError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1550
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1551
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1552
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1553
    def verify(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1554
        """Run verify on all tasks.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1555
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1556
        The verify method is supposed to raise an AssertionError if the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1557
        task has unmet expectations, with a one-line explanation about
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1558
        why this item is unmet.  This method should be safe to be called
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1559
        multiple times without side effects.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1560
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1561
        errors = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1562
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1563
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1564
                task.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1565
            except AssertionError, e:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1566
                error = str(e)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1567
                if not error:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1568
                    raise RuntimeError("Empty error message from %r" % task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1569
                errors.append(error)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1570
        if errors:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1571
            message = [str(self.path)]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1572
            for error in errors:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1573
                lines = error.splitlines()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1574
                message.append("- " + lines.pop(0))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1575
                message.extend(["  " + line for line in lines])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1576
            raise AssertionError(os.linesep.join(message))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1577
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1578
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1579
        """Put all tasks in replay mode."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1580
        self._has_run = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1581
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1582
            task.replay()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1583
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1584
    def restore(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1585
        """Restore the state of all tasks."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1586
        for task in self._tasks:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1587
            task.restore()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1588
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1589
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1590
class ReplayRestoreEvent(Event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1591
    """Helper event for tasks which need replay/restore but shouldn't match."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1592
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1593
    def matches(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1594
        return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1595
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1596
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1597
class Task(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1598
    """Element used to track one specific aspect on an event.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1599
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1600
    A task is responsible for adding any kind of logic to an event.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1601
    Examples of that are counting the number of times the event was
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1602
    made, verifying parameters if any, and so on.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1603
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1604
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1605
    def matches(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1606
        """Return true if the task is supposed to be run for the given path.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1607
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1608
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1609
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1610
    def may_run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1611
        """Return false if running this task would certainly raise an error."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1612
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1613
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1614
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1615
        """Perform the task item, considering that the given action happened.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1616
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1617
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1618
    def verify(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1619
        """Raise AssertionError if expectations for this item are unmet.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1620
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1621
        The verify method is supposed to raise an AssertionError if the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1622
        task has unmet expectations, with a one-line explanation about
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1623
        why this item is unmet.  This method should be safe to be called
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1624
        multiple times without side effects.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1625
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1626
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1627
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1628
        """Put the task in replay mode.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1629
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1630
        Any expectations of the task should be reset.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1631
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1632
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1633
    def restore(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1634
        """Restore any environmental changes made by the task.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1635
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1636
        Verify should continue to work after this is called.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1637
        """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1638
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1639
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1640
# --------------------------------------------------------------------
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1641
# Task implementations.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1642
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1643
class OnRestoreCaller(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1644
    """Call a given callback when restoring."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1645
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1646
    def __init__(self, callback):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1647
        self._callback = callback
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1648
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1649
    def restore(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1650
        self._callback()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1651
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1652
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1653
class PathMatcher(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1654
    """Match the action path against a given path."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1655
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1656
    def __init__(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1657
        self.path = path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1658
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1659
    def matches(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1660
        return self.path.matches(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1661
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1662
def path_matcher_recorder(mocker, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1663
    event.add_task(PathMatcher(event.path))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1664
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1665
Mocker.add_recorder(path_matcher_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1666
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1667
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1668
class RunCounter(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1669
    """Task which verifies if the number of runs are within given boundaries.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1670
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1671
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1672
    def __init__(self, min, max=False):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1673
        self.min = min
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1674
        if max is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1675
            self.max = sys.maxint
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1676
        elif max is False:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1677
            self.max = min
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1678
        else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1679
            self.max = max
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1680
        self._runs = 0
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1681
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1682
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1683
        self._runs = 0
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1684
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1685
    def may_run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1686
        return self._runs < self.max
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1687
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1688
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1689
        self._runs += 1
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1690
        if self._runs > self.max:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1691
            self.verify()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1692
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1693
    def verify(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1694
        if not self.min <= self._runs <= self.max:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1695
            if self._runs < self.min:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1696
                raise AssertionError("Performed fewer times than expected.")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1697
            raise AssertionError("Performed more times than expected.")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1698
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1699
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1700
class ImplicitRunCounter(RunCounter):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1701
    """RunCounter inserted by default on any event.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1702
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1703
    This is a way to differentiate explicitly added counters and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1704
    implicit ones.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1705
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1706
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1707
def run_counter_recorder(mocker, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1708
    """Any event may be repeated once, unless disabled by default."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1709
    if event.path.root_mock.__mocker_count__:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1710
        event.add_task(ImplicitRunCounter(1))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1711
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1712
Mocker.add_recorder(run_counter_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1713
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1714
def run_counter_removal_recorder(mocker, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1715
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1716
    Events created by getattr actions which lead to other events
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1717
    may be repeated any number of times. For that, we remove implicit
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1718
    run counters of any getattr actions leading to the current one.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1719
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1720
    parent_path = event.path.parent_path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1721
    for event in mocker.get_events()[::-1]:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1722
        if (event.path is parent_path and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1723
            event.path.actions[-1].kind == "getattr"):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1724
            for task in event.get_tasks():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1725
                if type(task) is ImplicitRunCounter:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1726
                    event.remove_task(task)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1727
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1728
Mocker.add_recorder(run_counter_removal_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1729
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1730
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1731
class MockReturner(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1732
    """Return a mock based on the action path."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1733
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1734
    def __init__(self, mocker):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1735
        self.mocker = mocker
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1736
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1737
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1738
        return Mock(self.mocker, path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1739
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1740
def mock_returner_recorder(mocker, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1741
    """Events that lead to other events must return mock objects."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1742
    parent_path = event.path.parent_path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1743
    for event in mocker.get_events():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1744
        if event.path is parent_path:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1745
            for task in event.get_tasks():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1746
                if isinstance(task, MockReturner):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1747
                    break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1748
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1749
                event.add_task(MockReturner(mocker))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1750
            break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1751
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1752
Mocker.add_recorder(mock_returner_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1753
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1754
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1755
class FunctionRunner(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1756
    """Task that runs a function everything it's run.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1757
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1758
    Arguments of the last action in the path are passed to the function,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1759
    and the function result is also returned.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1760
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1761
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1762
    def __init__(self, func):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1763
        self._func = func
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1764
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1765
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1766
        action = path.actions[-1]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1767
        return self._func(*action.args, **action.kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1768
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1769
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1770
class PathExecuter(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1771
    """Task that executes a path in the real object, and returns the result."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1772
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1773
    def __init__(self, result_callback=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1774
        self._result_callback = result_callback
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1775
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1776
    def get_result_callback(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1777
        return self._result_callback
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1778
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1779
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1780
        result = path.execute(path.root_object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1781
        if self._result_callback is not None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1782
            self._result_callback(result)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1783
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1784
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1785
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1786
class Orderer(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1787
    """Task to establish an order relation between two events.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1788
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1789
    An orderer task will only match once all its dependencies have
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1790
    been run.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1791
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1792
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1793
    def __init__(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1794
        self.path = path
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1795
        self._run = False 
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1796
        self._dependencies = []
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1797
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1798
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1799
        self._run = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1800
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1801
    def has_run(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1802
        return self._run
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1803
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1804
    def may_run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1805
        for dependency in self._dependencies:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1806
            if not dependency.has_run():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1807
                return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1808
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1809
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1810
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1811
        for dependency in self._dependencies:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1812
            if not dependency.has_run():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1813
                raise AssertionError("Should be after: %s" % dependency.path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1814
        self._run = True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1815
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1816
    def add_dependency(self, orderer):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1817
        self._dependencies.append(orderer)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1818
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1819
    def get_dependencies(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1820
        return self._dependencies
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1821
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1822
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1823
class SpecChecker(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1824
    """Task to check if arguments of the last action conform to a real method.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1825
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1826
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1827
    def __init__(self, method):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1828
        self._method = method
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1829
        self._unsupported = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1830
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1831
        if method:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1832
            try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1833
                self._args, self._varargs, self._varkwargs, self._defaults = \
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1834
                    inspect.getargspec(method)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1835
            except TypeError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1836
                self._unsupported = True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1837
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1838
                if self._defaults is None:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1839
                    self._defaults = ()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1840
                if type(method) is type(self.run):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1841
                    self._args = self._args[1:]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1842
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1843
    def get_method(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1844
        return self._method
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1845
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1846
    def _raise(self, message):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1847
        spec = inspect.formatargspec(self._args, self._varargs,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1848
                                     self._varkwargs, self._defaults)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1849
        raise AssertionError("Specification is %s%s: %s" %
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1850
                             (self._method.__name__, spec, message))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1851
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1852
    def verify(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1853
        if not self._method:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1854
            raise AssertionError("Method not found in real specification")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1855
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1856
    def may_run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1857
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1858
            self.run(path)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1859
        except AssertionError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1860
            return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1861
        return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1862
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1863
    def run(self, path):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1864
        if not self._method:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1865
            raise AssertionError("Method not found in real specification")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1866
        if self._unsupported:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1867
            return # Can't check it. Happens with builtin functions. :-(
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1868
        action = path.actions[-1]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1869
        obtained_len = len(action.args)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1870
        obtained_kwargs = action.kwargs.copy()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1871
        nodefaults_len = len(self._args) - len(self._defaults)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1872
        for i, name in enumerate(self._args):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1873
            if i < obtained_len and name in action.kwargs:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1874
                self._raise("%r provided twice" % name)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1875
            if (i >= obtained_len and i < nodefaults_len and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1876
                name not in action.kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1877
                self._raise("%r not provided" % name)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1878
            obtained_kwargs.pop(name, None)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1879
        if obtained_len > len(self._args) and not self._varargs:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1880
            self._raise("too many args provided")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1881
        if obtained_kwargs and not self._varkwargs:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1882
            self._raise("unknown kwargs: %s" % ", ".join(obtained_kwargs))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1883
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1884
def spec_checker_recorder(mocker, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1885
    spec = event.path.root_mock.__mocker_spec__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1886
    if spec:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1887
        actions = event.path.actions
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1888
        if len(actions) == 1:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1889
            if actions[0].kind == "call":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1890
                method = getattr(spec, "__call__", None)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1891
                event.add_task(SpecChecker(method))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1892
        elif len(actions) == 2:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1893
            if actions[0].kind == "getattr" and actions[1].kind == "call":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1894
                method = getattr(spec, actions[0].args[0], None)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1895
                event.add_task(SpecChecker(method))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1896
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1897
Mocker.add_recorder(spec_checker_recorder)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1898
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1899
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1900
class ProxyReplacer(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1901
    """Task which installs and deinstalls proxy mocks.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1902
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1903
    This task will replace a real object by a mock in all dictionaries
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1904
    found in the running interpreter via the garbage collecting system.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1905
    """
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1906
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1907
    def __init__(self, mock):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1908
        self.mock = mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1909
        self.__mocker_replace__ = False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1910
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1911
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1912
        global_replace(self.mock.__mocker_object__, self.mock)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1913
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1914
    def restore(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1915
        global_replace(self.mock, self.mock.__mocker_object__)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1916
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1917
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1918
def global_replace(remove, install):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1919
    """Replace object 'remove' with object 'install' on all dictionaries."""
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1920
    for referrer in gc.get_referrers(remove):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1921
        if (type(referrer) is dict and
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1922
            referrer.get("__mocker_replace__", True)):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1923
            for key, value in referrer.items():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1924
                if value is remove:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1925
                    referrer[key] = install
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1926
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1927
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1928
class Undefined(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1929
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1930
    def __repr__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1931
        return "Undefined"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1932
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1933
Undefined = Undefined()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1934
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1935
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1936
class Patcher(Task):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1937
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1938
    def __init__(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1939
        super(Patcher, self).__init__()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1940
        self._monitored = {} # {kind: {id(object): object}}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1941
        self._patched = {}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1942
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1943
    def is_monitoring(self, obj, kind):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1944
        monitored = self._monitored.get(kind)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1945
        if monitored:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1946
            if id(obj) in monitored:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1947
                return True
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1948
            cls = type(obj)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1949
            if issubclass(cls, type):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1950
                cls = obj
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1951
            bases = set([id(base) for base in cls.__mro__])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1952
            bases.intersection_update(monitored)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1953
            return bool(bases)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1954
        return False
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1955
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1956
    def monitor(self, obj, kind):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1957
        if kind not in self._monitored:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1958
            self._monitored[kind] = {}
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1959
        self._monitored[kind][id(obj)] = obj
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1960
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1961
    def patch_attr(self, obj, attr, value):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1962
        original = obj.__dict__.get(attr, Undefined)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1963
        self._patched[id(obj), attr] = obj, attr, original
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1964
        setattr(obj, attr, value)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1965
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1966
    def get_unpatched_attr(self, obj, attr):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1967
        cls = type(obj)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1968
        if issubclass(cls, type):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1969
            cls = obj
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1970
        result = Undefined
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1971
        for mro_cls in cls.__mro__:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1972
            key = (id(mro_cls), attr)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1973
            if key in self._patched:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1974
                result = self._patched[key][2]
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1975
                if result is not Undefined:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1976
                    break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1977
            elif attr in mro_cls.__dict__:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1978
                result = mro_cls.__dict__.get(attr, Undefined)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1979
                break
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1980
        if isinstance(result, object) and hasattr(type(result), "__get__"):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1981
            if cls is obj:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1982
                obj = None
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1983
            return result.__get__(obj, cls)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1984
        return result
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1985
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1986
    def _get_kind_attr(self, kind):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1987
        if kind == "getattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1988
            return "__getattribute__"
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1989
        return "__%s__" % kind
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1990
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1991
    def replay(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1992
        for kind in self._monitored:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1993
            attr = self._get_kind_attr(kind)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1994
            seen = set()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1995
            for obj in self._monitored[kind].itervalues():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1996
                cls = type(obj)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1997
                if issubclass(cls, type):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1998
                    cls = obj
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  1999
                if cls not in seen:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2000
                    seen.add(cls)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2001
                    unpatched = getattr(cls, attr, Undefined)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2002
                    self.patch_attr(cls, attr,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2003
                                    PatchedMethod(kind, unpatched,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2004
                                                  self.is_monitoring))
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2005
                    self.patch_attr(cls, "__mocker_execute__",
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2006
                                    self.execute)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2007
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2008
    def restore(self):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2009
        for obj, attr, original in self._patched.itervalues():
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2010
            if original is Undefined:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2011
                delattr(obj, attr)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2012
            else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2013
                setattr(obj, attr, original)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2014
        self._patched.clear()
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2015
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2016
    def execute(self, action, object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2017
        attr = self._get_kind_attr(action.kind)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2018
        unpatched = self.get_unpatched_attr(object, attr)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2019
        try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2020
            return unpatched(*action.args, **action.kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2021
        except AttributeError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2022
            if action.kind == "getattr":
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2023
                # The normal behavior of Python is to try __getattribute__,
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2024
                # and if it raises AttributeError, try __getattr__.   We've
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2025
                # tried the unpatched __getattribute__ above, and we'll now
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2026
                # try __getattr__.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2027
                try:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2028
                    __getattr__ = unpatched("__getattr__")
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2029
                except AttributeError:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2030
                    pass
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2031
                else:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2032
                    return __getattr__(*action.args, **action.kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2033
            raise
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2034
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2035
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2036
class PatchedMethod(object):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2037
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2038
    def __init__(self, kind, unpatched, is_monitoring):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2039
        self._kind = kind
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2040
        self._unpatched = unpatched
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2041
        self._is_monitoring = is_monitoring
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2042
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2043
    def __get__(self, obj, cls=None):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2044
        object = obj or cls
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2045
        if not self._is_monitoring(object, self._kind):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2046
            return self._unpatched.__get__(obj, cls)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2047
        def method(*args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2048
            if self._kind == "getattr" and args[0].startswith("__mocker_"):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2049
                return self._unpatched.__get__(obj, cls)(args[0])
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2050
            mock = object.__mocker_mock__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2051
            return mock.__mocker_act__(self._kind, args, kwargs, object)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2052
        return method
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2053
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2054
    def __call__(self, obj, *args, **kwargs):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2055
        # At least with __getattribute__, Python seems to use *both* the
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2056
        # descriptor API and also call the class attribute directly.  It
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2057
        # looks like an interpreter bug, or at least an undocumented
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2058
        # inconsistency.
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2059
        return self.__get__(obj)(*args, **kwargs)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2060
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2061
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2062
def patcher_recorder(mocker, event):
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2063
    mock = event.path.root_mock
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2064
    if mock.__mocker_patcher__ and len(event.path.actions) == 1:
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2065
        patcher = mock.__mocker_patcher__
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2066
        patcher.monitor(mock.__mocker_object__, event.path.actions[0].kind)
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2067
5a2786fd5048 Add mocker 0.10.1 to trunk/thirdparty, so that it can be used to create mocks
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
  2068
Mocker.add_recorder(patcher_recorder)