Commits

Attila Magyar committed 260bb4c

added some docstrings

Comments (0)

Files changed (3)

src/macaque/core/mocks.py

 from .context import Given, Expectation, global_context_manager
 
 class Stubber(object):
-    def __init__(self, args):
-        self.args = args
+    """
+        Provides canned response for the specified arguments
+    """
+    def __init__(self, arg_map):
+        self.arg_map = arg_map
                 
     def program(self, command, *args, **kargs):
-        if not self.args.has_matched(*args, **kargs):
-            self.args.store(BatchCommand(command), *args, **kargs)
+        if not self.arg_map.has_matched(*args, **kargs):
+            self.arg_map.store(BatchCommand(command), *args, **kargs)
         else:
-            existing_commands = self.args.find_matched(*args, **kargs)
+            existing_commands = self.arg_map.find_matched(*args, **kargs)
             existing_commands[0].add_command(command)
         
     def execute(self, *args, **kargs):
-        commands = self.args.find_matched(*args, **kargs)
+        commands = self.arg_map.find_matched(*args, **kargs)
         return commands[0]()
         
 class Verifer(object):
-    def __init__(self, args, method_name=""): 
-        self.args = args
+    """
+        Verifies whether expected arguments match to the stored ones
+    """
+    def __init__(self, arg_map, method_name=""): 
+        self.arg_map = arg_map
         self.method_name = method_name
         
     def verify(self, expected_call_count, *args, **kargs):
-        actual_call_count = sum(self.args.find_matched(*args, **kargs))
+        actual_call_count = sum(self.arg_map.find_matched(*args, **kargs))
         self._verify_call_count(expected_call_count, actual_call_count, (args, kargs))
 
     def _is_interval(self, expected):
         if self._is_interval(expected_call_count):
             self._verify_call_interval(expected_call_count, actual_call_count, msg_formatter)
         elif expected_call_count != actual_call_count:
-            raise AssertionError(msg_formatter.unexpected_message(self.args))
+            raise AssertionError(msg_formatter.unexpected_message(self.arg_map))
 
     def _verify_call_interval(self, expected, actual, formatter):
         if len(expected) != 2:
             raise Exception('Expected call count must be a number or a tuple with 2 elements')
         lower, upper = expected
         if self._below_lower_bound(actual, lower): # TODO validate lower<upper if not _
-            raise AssertionError(formatter.unexpected_message(self.args))
+            raise AssertionError(formatter.unexpected_message(self.arg_map))
         if self._above_upper_bound(actual, upper):
-            raise AssertionError(formatter.unexpected_message(self.args))
+            raise AssertionError(formatter.unexpected_message(self.arg_map))
 
     def _below_lower_bound(self, actual, lower_limit):
         return not isinstance(lower_limit, AnyMatcher) and lower_limit > actual
         return not isinstance(upper_limit, AnyMatcher) and upper_limit < actual
 
     def register_call(self, *args, **kargs):
-        call_count = sum(self.args.find_matched(*args, **kargs))
-        self.args.store(call_count +1, *args, **kargs)
+        call_count = sum(self.arg_map.find_matched(*args, **kargs))
+        self.arg_map.store(call_count +1, *args, **kargs)
         
 class MessageFormatter(object):
     def __init__(self, method_name, method_args, expected_call_count, actual_call_count):
         return '%s(%s)' % (self.method_name, ', '.join(args + kargs))
         
 class Method(object):
+    """
+        Handles a method invocation, verifies call count and provides stubbed answers
+    """
     def __init__(self, verifier, stub, context_manager): 
         self.verifier = verifier
         self.stubber = stub
             return self.called_in_code()
     
 class Mock(object):
+    """
+        Responses to every unknown method call by returning a Method instance
+    """
     def __init__(self):
         self.context_manager = global_context_manager
         self.stub_methods = {}

src/macaque/test/stubbertest.py

 class StubberTest(unittest.TestCase):
 
     def setUp(self):
-        self.args = mockito()
-        self.stubber = Stubber(self.args)
+        self.arg_map = mockito()
+        self.stubber = Stubber(self.arg_map)
 
     def mock_command(self):
         m = mockito()
     def testProgramStoresCommandInBatchCommandAtTheFirstTime(self):
         command_to_store = ReturnCommand(42)        
         self.stubber.program(command_to_store, 'arg', key='key')
-        verify(self.args).store(BatchCommand(command_to_store), 'arg', key='key')
+        verify(self.arg_map).store(BatchCommand(command_to_store), 'arg', key='key')
 
     def testProgramsAddsCommandToExistingBatch(self):
         existing_cmd, cmd_to_store = mockito(), mockito()        
-        when(self.args).has_matched('arg').thenReturn(True)
-        when(self.args).find_matched('arg').thenReturn([existing_cmd])
+        when(self.arg_map).has_matched('arg').thenReturn(True)
+        when(self.arg_map).find_matched('arg').thenReturn([existing_cmd])
         self.stubber.program(cmd_to_store, 'arg')
         verify(existing_cmd).add_command(cmd_to_store)
 
     def testExecutesCommand(self):        
         mock_cmd, command = self.mock_command()
-        when(self.args).find_matched('arg', key='key').thenReturn([command])
+        when(self.arg_map).find_matched('arg', key='key').thenReturn([command])
         self.stubber.execute('arg', key='key')
         verify(mock_cmd).__call__()
         

src/macaque/test/verifertet.py

 class VerifierTest(unittest.TestCase):
     
     def setUp(self):
-        self.args = mockito()
-        self.args.__class__.__iter__ = lambda s: iter([])
-        self.verifer = Verifer(self.args)
+        self.arg_map = mockito()
+        self.arg_map.__class__.__iter__ = lambda s: iter([])
+        self.verifer = Verifer(self.arg_map)
     
     @expected(AssertionError)
     def testVerifyThrowsExceptionIfCallCountNoMatched(self):
-        when(self.args).find_matched().thenReturn([0])
+        when(self.arg_map).find_matched().thenReturn([0])
         self.verifer.verify(1)
         
     @expected(AssertionError)
     def testVerifyThrowsExceptionIfCallCountNoMatchedToInterval(self):
-        when(self.args).find_matched().thenReturn([1])
+        when(self.arg_map).find_matched().thenReturn([1])
         self.verifer.verify((2, 3))
         
     @expected(AssertionError)
     def testVerifyThrowsExceptionIfCallCountNoMatchedToWildCardLowerBound(self):
-        when(self.args).find_matched().thenReturn([2])
+        when(self.arg_map).find_matched().thenReturn([2])
         self.verifer.verify((_, 1))
         
     @expected(AssertionError)
     def testVerifyThrowsExceptionIfCallCountNoMatchedToWildCardUpperBound(self):
-        when(self.args).find_matched().thenReturn([2])
+        when(self.arg_map).find_matched().thenReturn([2])
         self.verifer.verify((3, _))
                 
     def testNoExceptionIfCallCountMatches(self):
-        when(self.args).find_matched().thenReturn([3])
+        when(self.arg_map).find_matched().thenReturn([3])
         self.verifer.verify(3)
         self.verifer.verify((1, 4))
         self.verifer.verify((2, 3))
         self.verifer.verify((3, _))
                 
     def testRegisterStoresArgumentsWithCallCountPlusOne(self):
-        when(self.args).find_matched('arg').thenReturn([5])
+        when(self.arg_map).find_matched('arg').thenReturn([5])
         self.verifer.register_call('arg')
-        verify(self.args).store(6, 'arg')
+        verify(self.arg_map).store(6, 'arg')