Commits

Tino de Bruijn committed dccd167

Improved tests. Made MockupSerial a deque.

  • Participants
  • Parent commits 1bc5de2

Comments (0)

Files changed (3)

File pyfirmata/mockup.py

+from collections import deque
 import pyfirmata
 
-class MockupSerial(object):
-    """ A Mockup object for python's Serial. Can only return Firmata version. """
-    def __init__(self, port, baudrate, timeout=1):
-        self.return_id = False
-        self.echoed = False
+class MockupSerial(deque):
+    """ 
+    A Mockup object for python's Serial. Functions as a fifo-stack. Push to
+    it with ``write``, read from it with ``read``.
+    
+    >>> s = MockupSerial('someport', 4800)
+    >>> s.read()
+    ''
+    >>> s.write(chr(100))
+    >>> s.write('blaat')
+    >>> s.write(100000)
+    >>> s.read(2)
+    ['d', 'blaat']
+    >>> s.read()
+    100000
+    >>> s.read()
+    ''
+    >>> s.read(2)
+    ['', '']
+    >>> s.close()
+    """
+    def __init__(self, port, baudrate, timeout=0.02):
+        pass
         
-    def read(self, len=1):
-        if self.return_id:
-            if self.echoed:
-                self.return_id = False
-                return chr(1)
-            else:
-                self.echoed = True
-                return chr(pyfirmata.REPORT_ARDUINO_ID)
+    def read(self, count=1):
+        if count > 1:
+            val = []
+            for i in range(count):
+                try:
+                    val.append(self.popleft())
+                except IndexError:
+                    val.append('')
         else:
-            return ''
+            try:
+                val = self.popleft()
+            except IndexError:
+                val = ''
+        return val
             
     def write(self, value):
-        pass
+        self.append(value)
             
     def close(self):
-        pass
+        self.clear()
         
 class MockupBoard(pyfirmata.Board):
 
-    def __init__(self, port='', type="normal", values_dict={}, name=''):
-        self.name = name
-        self.sp = MockupSerial(port, 57600, timeout=0)
+    def __init__(self, port='', type="arduino", values_dict={}):
+        self.sp = MockupSerial(port, 57600)
         self.setup_layout(pyfirmata.BOARDS[type])
         self.values_dict = values_dict
         self.id = 1
     def stop(self):
         pass
 
-pyfirmata.Port = MockupPort
-pyfirmata.Pin = MockupPin
-
-Board = MockupBoard
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()

File pyfirmata/pyfirmata.py

         self.pins = []
         for i in range(8):
             pin_nr = i + self.port_number * 8
-            self.pins.append(Pin(sp, pin_nr, type=DIGITAL, port=self, board_name=self.board.name))
+            self.pins.append(Pin(self.board, pin_nr, type=DIGITAL, port=self))
             
     def __str__(self):
         return "Digital Port %i on %s" % (self.port_number, self.board)
 
 import pyfirmata
 from pyfirmata import mockup
+from pyfirmata.boards import BOARDS
 from pyfirmata.util import to_7_bits
 
 # This should be covered:
 # sysex end             0xF7   
 # protocol version      0xF9              major version         minor version
 # system reset          0xFF
+#
+# SysEx-based commands (0x00-0x7F) are used for an extended command set.
+# 
+# type                command  first byte       second byte      ...
+# ----------------------------------------------------------------------------
+# string                0x71   char *string ...
+# firmware name/version 0x79   major version   minor version     char *name...
+
 
 class BoardBaseTest(unittest.TestCase):
     def setUp(self):
+        # Test with the MockupSerial so no real connection is needed
         pyfirmata.pyfirmata.serial.Serial = mockup.MockupSerial
         self.board = pyfirmata.Board('test')
 
     # TODO Test layout of Board Mega
     # TODO Test if messages written are correct...
 
+    # First test the handlers
     def test_handle_analog_message(self):
         self.assertEqual(self.board.analog[3].read(), None)
         # Test it returns false with not enough params
         self.assertTrue(self.board._handle_report_version([2, 1]))
         self.assertEqual(self.board.firmata_version, (2, 1))
         
+    # Now test the whole structure.
+    
+        
 class TestBoardLayout(BoardBaseTest):
 
     def test_pwm_layout(self):
                 pins.append(self.board.get_pin('d:%d:p' % pin.pin_number))
         for pin in pins:
             self.assertEqual(pin.mode, pyfirmata.PWM)
+            self.assertTrue(pin.pin_number in BOARDS['arduino']['pwm'])
+        self.assertTrue(len(pins) == len(BOARDS['arduino']['pwm']))
         
     def test_get_pin_digital(self):
         pin = self.board.get_pin('d:13:o')
         self.board.exit()
         pyfirmata.serial.Serial = serial.Serial
         
-class TestMockupBoard(unittest.TestCase):
-    
+class TestMockupBoardLayout(TestBoardLayout, TestBoardMessages):
+    """
+    TestMockupBoardLayout is subclassed from TestBoardLayout and
+    TestBoardMessages as it should pass the same tests, but with the
+    MockupBoard.
+    """
     def setUp(self):
         self.board = mockup.MockupBoard('test')
-    
-    def test_identifier(self):
-        self.assertEqual(self.board.identifier, ord(chr(ID)))
-    
-    def test_pwm_layout(self):
-        pins = []
-        for pin_nr in self.board.layout['p']:
-            pins.append(self.board.get_pin('d:%d:p' % pin_nr))
-        for pin in pins:
-            mode = pin.get_mode()
-            self.assertEqual(mode, pyfirmata.DIGITAL_PWM)
-        
-    def test_get_pin_digital(self):
-        pin = self.board.get_pin('d:13:o')
-        self.assertEqual(pin.pin_number, 5)
-        self.assertEqual(pin.mode, pyfirmata.DIGITAL_OUTPUT)
-        self.assertEqual(pin.sp, self.board.sp)
-        self.assertEqual(pin.port.port_number, 1)
-        self.assertEqual(pin._get_board_pin_number(), 13)
-        self.assertEqual(pin.port.get_active(), 1)
-        
-    def test_get_pin_analog(self):
-        pin = self.board.get_pin('a:5:i')
-        self.assertEqual(pin.pin_number, 5)
-        self.assertEqual(pin.sp, self.board.sp)
-        self.assertEqual(pin.get_active(), 1)
-        self.assertEqual(pin.value, -1)
-        
-    def tearDown(self):
-        self.board.exit()
-        pyfirmata.serial.Serial = serial.Serial
 
 
 board_messages = unittest.TestLoader().loadTestsFromTestCase(TestBoardMessages)
 board_layout = unittest.TestLoader().loadTestsFromTestCase(TestBoardLayout)
 default = unittest.TestSuite([board_messages, board_layout])
-mockup_suite = unittest.TestLoader().loadTestsFromTestCase(TestMockupBoard)
+mockup_suite = unittest.TestLoader().loadTestsFromTestCase(TestMockupBoardLayout)
 
 if __name__ == '__main__':
     parser = OptionParser()