Пример #1
0
 def __enter__(self):
     self.__traceroute__ = TraceRoute()
     self.__traceroute_expected__ = TraceRoute()
     self.__expectation__ = []
     self.__recording__ = RECORDING
     self.__dependency_injection__ = DependencyInjection(double=self)
     return self
Пример #2
0
 def __enter__(self):
     self.__traceroute__ = TraceRoute()
     self.__traceroute_expected__ = TraceRoute()
     self.__expectation__ = []
     self.__recording__ = RECORDING
     self.__dependency_injection__ = DependencyInjection(double = self)
     return self
Пример #3
0
class Mock(_TestDouble):
    """Mocks are what we are talking about here:
    objects pre-programmed with expectations which form a
    specification of the calls they are expected to receive.
    """
    __expectation__ =[]#[MockedCall(attribute, args, kargs),]
    __recording__ = RECORDING 
    __traceroute__ = None
    __traceroute_expected__ = None
    __dependency_injection__ = None

    def __enter__(self):
        self.__traceroute__ = TraceRoute()
        self.__traceroute_expected__ = TraceRoute()
        self.__expectation__ = []
        self.__recording__ = RECORDING
        self.__dependency_injection__ = DependencyInjection(double = self)
        return self

    def __methodCalled__(self, *args, **kargs):
        property = getframeinfo(getframe(1))[2]
        return self._property_called(property, args, kargs)

    def _property_called(self, property, args=[], kargs={}):
        if self.__recording__:
            self.__traceroute_expected__.remember()
            self._new_expectation(MockedCall(property, args = args, kargs = kargs, response = self))
            return self 
        else:
            self.__traceroute__.remember()
            return self._expectancy_recorded(property, args, kargs)

    def __exit__(self, type, value, traceback):
        self.__dependency_injection__.restoure_import()
        self.__recording__ = STOPRECORD

    def __setattr__(self, attr, value):
        if attr in dir(Mock):
            object.__setattr__(self, attr, value)
        else:
            self._property_called('__setattr__', args=[attr, value])

    def _new_expectation(self, attr):
        self.__expectation__.append(attr)

    def __rshift__(self, response):
            self.__expectation__[-1].set_response(response)
    __lshift__ = __rshift__ 

    def _expectancy_recorded(self, attr, args=[], kargs={}):
        try:
            if self._is_ordered():
                return self._call_mocked_ordered(attr, args, kargs)
            else:
                return self._call_mocked_unordered(attr, args, kargs)
        except (CallExpectation, IndexError):
            raise MockExpectationError(self._unexpected_call_msg(attr, args, kargs))

    def _unexpected_call_msg(self, attr, args, kargs):
        return ("Mock Object received unexpected call:%s\n"
                "Expected:\n"
                "%s\n"
                "Got:\n"
                "%s") % (
                    format_called(attr, args, kargs),
                    self.__traceroute_expected__.stack_code(),
                    self.__traceroute__.stack_trace())

    def _is_ordered(self):
        return self.__kargs__.get('ordered', True)

    def _call_mocked_unordered(self, attr, args, kargs):
        for number, call in enumerate(self.__expectation__):
            if call.has_callable(attr, args, kargs):
                call_mocked = self.__expectation__.pop(number)
                return call_mocked.call(attr, args, kargs)
        raise CallExpectation("Mock object has no called %s" %attr)

    def _call_mocked_ordered(self, attr, args, kargs):
        call_mocked = self.__expectation__.pop(0)
        return call_mocked.call(attr, args, kargs)

    def __getattr__(self, x):
        return self._property_called('__getattribute__',[x])

    def validate(self):
        self.__dependency_injection__.restoure_object()
        if self.__expectation__:
            raise MockExpectationError(
                    self._call_waiting_msg())

    def __del__(self):
        self.__dependency_injection__.restoure_object()
        if self.__expectation__:
            print  self._call_waiting_msg()
    
    def _call_waiting_msg(self):
        return("Call waiting:\n"
               "Expected:\n"
               "%s\n"
               "Got only:\n"
               "%s") % (
                    self.__traceroute_expected__.stack_code(),
                    self.__traceroute__.stack_code())
Пример #4
0
 def __enter__(self):
     self.__expectation__ = []
     self.__recording__ = RECORDING
     self.__dependency_injection__ = DependencyInjection(double = self)
     return self
Пример #5
0
class Mock(_TestDouble):
    """Mocks are what we are talking about here:
    objects pre-programmed with expectations which form a
    specification of the calls they are expected to receive.
    """
    __expectation__ = []  #[MockedCall(attribute, args, kargs),]
    __recording__ = RECORDING
    __traceroute__ = None
    __traceroute_expected__ = None
    __dependency_injection__ = None

    def __enter__(self):
        self.__traceroute__ = TraceRoute()
        self.__traceroute_expected__ = TraceRoute()
        self.__expectation__ = []
        self.__recording__ = RECORDING
        self.__dependency_injection__ = DependencyInjection(double=self)
        return self

    def __methodCalled__(self, *args, **kargs):
        property = getframeinfo(getframe(1))[2]
        return self._property_called(property, args, kargs)

    def _property_called(self, property, args=[], kargs={}):
        if self.__recording__:
            self.__traceroute_expected__.remember()
            self._new_expectation(
                MockedCall(property, args=args, kargs=kargs, response=self))
            return self
        else:
            self.__traceroute__.remember()
            return self._expectancy_recorded(property, args, kargs)

    def __exit__(self, type, value, traceback):
        self.__dependency_injection__.restore_import()
        self.__recording__ = STOPRECORD

    def __setattr__(self, attr, value):
        if attr in dir(Mock):
            object.__setattr__(self, attr, value)
        else:
            self._property_called('__setattr__', args=[attr, value])

    def _new_expectation(self, attr):
        self.__expectation__.append(attr)

    def __rshift__(self, response):
        self.__expectation__[-1].set_response(response)

    __lshift__ = __rshift__

    def _expectancy_recorded(self, attr, args=[], kargs={}):
        try:
            if self._is_ordered():
                return self._call_mocked_ordered(attr, args, kargs)
            else:
                return self._call_mocked_unordered(attr, args, kargs)
        except (CallExpectation, IndexError):
            raise MockExpectationError(
                self._unexpected_call_msg(attr, args, kargs))

    def _unexpected_call_msg(self, attr, args, kargs):
        return ("Mock Object received unexpected call:%s\n"
                "Expected:\n"
                "%s\n"
                "Got:\n"
                "%s") % (format_called(attr, args, kargs),
                         self.__traceroute_expected__.stack_code(),
                         self.__traceroute__.stack_trace())

    def _is_ordered(self):
        return self.__kargs__.get('ordered', True)

    def _call_mocked_unordered(self, attr, args, kargs):
        for number, call in enumerate(self.__expectation__):
            if call.has_callable(attr, args, kargs):
                call_mocked = self.__expectation__.pop(number)
                return call_mocked.call(attr, args, kargs)
        raise CallExpectation("Mock object has no called %s" % attr)

    def _call_mocked_ordered(self, attr, args, kargs):
        call_mocked = self.__expectation__.pop(0)
        return call_mocked.call(attr, args, kargs)

    def __getattr__(self, x):
        return self._property_called('__getattribute__', [x])

    def validate(self):
        self.__dependency_injection__.restore_object()
        if self.__expectation__:
            raise MockExpectationError(self._call_waiting_msg())

    def __del__(self):
        self.__dependency_injection__.restore_object()
        if self.__expectation__:
            print self._call_waiting_msg()

    def _call_waiting_msg(self):
        return ("Call waiting:\n"
                "Expected:\n"
                "%s\n"
                "Got only:\n"
                "%s") % (self.__traceroute_expected__.stack_code(),
                         self.__traceroute__.stack_code())
Пример #6
0
 def __enter__(self):
     self.__expectation__ = []
     self.__recording__ = RECORDING
     self.__dependency_injection__ = DependencyInjection(double=self)
     return self
Пример #7
0
class Stub(_TestDouble):
    """Stubs provides canned answers to calls made during the test.
    """
    __expectation__ = []  # [(attribute, args, kargs),]
    __recording__ = RECORDING
    __last_property_called__ = None
    __dependency_injection__ = None

    def __enter__(self):
        self.__expectation__ = []
        self.__recording__ = RECORDING
        self.__dependency_injection__ = DependencyInjection(double=self)
        return self

    def __methodCalled__(self, *args, **kargs):
        property_name = self._property_called_name()
        return self._property_called(property_name, args, kargs)

    def _property_called_name(self):
        property_called_name = self.__last_property_called__ or getframeinfo(
            getframe(2))[2]
        self.__last_property_called__ = None
        return property_called_name

    def _property_called(self, property, args=[], kargs={}, response=None):
        if self.__recording__:
            response = response if response is not None else self
            self._new_expectation([property, args, kargs, response])
            return self
        else:
            return self._expectation_value(property, args, kargs)

    def __exit__(self, type, value, traceback):
        self.__dependency_injection__.restore_import()
        self.__recording__ = STOPRECORD

    def __setattr__(self, attr, value):
        if attr in dir(Stub):
            object.__setattr__(self, attr, value)
        else:
            self._property_called('__setattr__', args=[attr, value])

    def _new_expectation(self, expectation):
        self.__expectation__.append(expectation)

    def __rshift__(self, response):
        self.__expectation__[-1][3] = response

    __lshift__ = __rshift__

    def _expectation_value(self, attr, args=[], kargs={}):
        for position, (attr_expectation, args_expectation, kargs_expectation,
                       response) in enumerate(self.__expectation__):
            if (attr_expectation, args_expectation,
                    kargs_expectation) == (attr, args, kargs):
                self._to_the_end(position)
                return response
        if self._has_proxy():
            return self._proxy(attr, args, kargs)
        self._attribute_expectation(attr, args, kargs)

    def _attribute_expectation(self, attr, args, kargs):
        raise AttributeError("Stub Object received unexpected call. %s" %
                             (self._format_called(attr, args, kargs)))

    def _proxy(self, attr, args, kargs):
        proxy = self.__kargs__.get('proxy')
        if attr in ['__getattribute__', '__getattr__']:
            return getattr(proxy, args[0])
        return getattr(proxy, attr)(*args, **kargs)

    def _has_proxy(self):
        return self.__kargs__.has_key('proxy')

    def _format_called(self, attr, args, kargs):
        if attr == '__call__' and self.__last_property_called__:
            attr = self.__last_property_called__
        return format_called(attr, args, kargs)

    def _to_the_end(self, position):
        self.__expectation__.append(self.__expectation__.pop(position))

    def __getattr__(self, x):
        self.__last_property_called__ = x
        return self._property_called('__getattribute__', (x, ), response=self)

    def __del__(self):
        self.__dependency_injection__.restore_object()

    def restore_import(self):
        self.__dependency_injection__.restore_object()
Пример #8
0
class Stub(_TestDouble):
    """Stubs provides canned answers to calls made during the test.
    """
    __expectation__= [] # [(attribute, args, kargs),]
    __recording__ = RECORDING
    __last_property_called__ = None
    __dependency_injection__ = None

    def __enter__(self):
        self.__expectation__= []
        self.__recording__ = RECORDING
        self.__dependency_injection__ = DependencyInjection(double = self)
        return self

    def __methodCalled__(self, *args, **kargs):
        property_name = self._property_called_name()
        return self._property_called(property_name, args, kargs)

    def _property_called_name(self):
        property_called_name =  self.__last_property_called__ or getframeinfo(getframe(2))[2]
        self.__last_property_called__ = None
        return property_called_name

    def _property_called(self, property, args=[], kargs={}, response=None):
        if self.__recording__:
            response = response if response is not None else self
            self._new_expectation([property, args, kargs, response])
            return self
        else:
            return self._expectation_value(property, args, kargs)

    def __exit__(self, type, value, traceback):
        self.__dependency_injection__.restore_import()
        self.__recording__ = STOPRECORD

    def __setattr__(self, attr, value):
        if attr in dir(Stub):
            object.__setattr__(self, attr, value)
        else:
            self._property_called('__setattr__', args=[attr, value])

    def _new_expectation(self, expectation):
        self.__expectation__.append(expectation)

    def __rshift__(self, response):
            self.__expectation__[-1][3] = response
    __lshift__ = __rshift__

    def _expectation_value(self, attr, args=[], kargs={}):
        for position, (attr_expectation, args_expectation, kargs_expectation, response) in enumerate(self.__expectation__):
            if (attr_expectation, args_expectation, kargs_expectation) == (attr, args, kargs):
                self._to_the_end(position)
                return response
        if self._has_proxy():
            return self._proxy(attr, args, kargs)
        self._attribute_expectation(attr, args, kargs)

    def _attribute_expectation(self, attr, args, kargs):
        raise AttributeError(
            "Stub Object received unexpected call. %s"%(
                    self._format_called(attr, args, kargs)))

    def _proxy(self, attr, args, kargs):
        proxy = self.__kargs__.get('proxy')
        if attr in ['__getattribute__', '__getattr__'] :
            return getattr(proxy, args[0])
        return getattr(proxy, attr)(*args, **kargs)

    def _has_proxy(self):
        return self.__kargs__.has_key('proxy')

    def _format_called(self, attr, args, kargs):
        if attr == '__call__' and self.__last_property_called__:
            attr = self.__last_property_called__
        return format_called(attr, args, kargs)

    def _to_the_end(self, position):
        self.__expectation__.append(self.__expectation__.pop(position))

    def __getattr__(self, x):
        self.__last_property_called__ = x
        return self._property_called('__getattribute__', (x,), response=self)
    
    def __del__(self):
        self.__dependency_injection__.restore_object()
    
    def restore_import(self):
        self.__dependency_injection__.restore_object()