'''Unit test package for package "tws".'''

__copyright__ = "Copyright (c) 2008 Kevin J Bluck"
__version__   = "$Id$"

import socket
from tws import EWrapper


def test_import():
    '''Verify successful import of top-level "tws" package'''
    import tws
    assert tws


class mock_wrapper(EWrapper):

    def __init__(self):
        self.errors = []
    
    def error(self, id, code, text):
        self.errors.append((id, code, text))
        
class mock_socket(object):

    def __init__(self):
        self._peer = ()

    def connect(self, peer, error=False):
        if error: raise socket.error()
        self._peer = peer

    def getpeername(self):
        if not self._peer: raise socket.error()
        return self._peer

    def makefile(self, mode):
        return StringIO()




'''Unit test package for package "tws".'''

__copyright__ = "Copyright (c) 2008 Kevin J Bluck"
__version__   = "$Id$"

import socket
from tws import EWrapper


def test_import():
    '''Verify successful import of top-level "tws" package'''
    import tws
    assert tws


class mock_wrapper(EWrapper):

    def __init__(self):
        self.calldata = []
        self.errors = []
    
    def error(self, id, code, text):
        self.errors.append((id, code, text))

    def __getattr__(self, name):
        # Any arbitrary unknown attribute is mapped to a function call which is
        # recorded into self.calldata. 
        return lambda *args, **kwds: self.calldata.append((name, args, kwds))


class mock_socket(object):

    def __init__(self):
        self._peer = ()

    def connect(self, peer, error=False):
        if error: raise socket.error()
        self._peer = peer

    def getpeername(self):
        if not self._peer: raise socket.error()
        return self._peer

    def makefile(self, mode):
        return StringIO()




Implement a __getattr__() for mock_wrapper that just returns a lambda that records whatever call was attempted along with the call params.
Implement a __getattr__() for mock_wrapper that just returns a lambda that records whatever call was attempted along with the call params.