Commit 92da2f8a authored by auke.klazema's avatar auke.klazema

Added initial tests and some small fixes and improvements.

parent 41c9cd81
......@@ -17,10 +17,16 @@ class PROLOGIX_GPIB_USB():
port = "/dev/ttys000"
portFormat = "%03d"
maxDeviceNum = 999
else:
raise NotImplementedError("Windows is not supported")
if verbose:
print "Looking for ports such as %s in range %d on %s" % \
( port, maxDeviceNum, platform.system() )
self._rematch=re.search(r"(/dev/tty.*[^0-9])([0-9]+$)",port)
if not self._rematch:
raise ValueError(port)
self._serialPort = None
while int(self._rematch.group(2)) < maxDeviceNum:
if verbose:
......@@ -32,7 +38,7 @@ class PROLOGIX_GPIB_USB():
port=self._rematch.group(1) +\
(portFormat % (int(self._rematch.group(2))+1))
self._rematch=re.search("(/dev/tty.*[^0-9])([0-9]+$)",port)
if port==None:
if self._serialPort==None:
raise serial.SerialException("Cannot find port")
self._serialPort.write("++savecfg 0" + chr(10))
self._serialPort.write("++auto 0" + chr(10))
......
import hpib
import unittest
from mock import patch, call
from serial import SerialException
class PrologixGpibUsbTest(unittest.TestCase):
def setUp(self):
serial_patcher = patch('hpib.serial.Serial')
self.serial_mock = serial_patcher.start()
self.addCleanup(serial_patcher.stop)
platform_pathcer = patch('hpib.platform')
self.platform_mock = platform_pathcer.start()
self.addCleanup(platform_pathcer.stop)
self.platform_mock.system.return_value = "Linux"
def test_when_platform_linux_port_gets_set_to_ttyusb0(self):
hpib.PROLOGIX_GPIB_USB()
self.serial_mock.assert_called_with('/dev/ttyUSB0', timeout=1)
def test_when_platform_darwin_port_gets_set_to_ttys000(self):
self.platform_mock.system.return_value = "Darwin"
hpib.PROLOGIX_GPIB_USB()
self.serial_mock.assert_called_with('/dev/ttys000', timeout=1)
def test_getDevices_returns_not_scanned_when_find_devices_is_false(self):
usb = hpib.PROLOGIX_GPIB_USB(findDevices=False)
self.assertEqual(usb.getDevices(), 'Not scanned.')
def test_raises_serial_exception_when_all_serial_ports_fail(self):
self.serial_mock.side_effect = SerialException
with self.assertRaises(SerialException):
hpib.PROLOGIX_GPIB_USB()
def test_when_platform_linux_last_port_tried_is_ttyusb98(self):
self.serial_mock.side_effect = SerialException
with self.assertRaises(SerialException):
hpib.PROLOGIX_GPIB_USB()
self.serial_mock.assert_called_with('/dev/ttyUSB98', timeout=1)
def test_when_platform_darwin_last_port_tried_is_ttyusb98(self):
self.platform_mock.system.return_value = "Darwin"
self.serial_mock.side_effect = SerialException
with self.assertRaises(SerialException):
hpib.PROLOGIX_GPIB_USB()
self.serial_mock.assert_called_with('/dev/ttys998', timeout=1)
def test_setup_device_when_port_found(self):
write_mock = self.serial_mock.return_value.write
hpib.PROLOGIX_GPIB_USB()
self.assertEqual(write_mock.call_args_list[0], call("++savecfg 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[1], call("++auto 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[2], call("++eoi 1" + chr(10)))
self.assertEqual(write_mock.call_args_list[3], call("++eos 2" + chr(10)))
self.assertEqual(write_mock.call_args_list[4], call("++eot_enable 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[5], call("++eot_char 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[6], call("++read_tmo_ms 500" + chr(10)))
def test_setup_calls_identify_command_when_finding_devices(self):
self.serial_mock.return_value.readline.return_value = ""
write_mock = self.serial_mock.return_value.write
hpib.PROLOGIX_GPIB_USB(findDevices=True)
self.assertEqual(write_mock.call_args_list[7],
call("++addr " + str(0) + chr(10) + "*idn?" + chr(10) + "++read" + chr(10)))
def test_if_find_devices_getDevices_will_return_a_list_of_devices(self):
self.serial_mock.return_value.readline.return_value = "Device 1"
usb = hpib.PROLOGIX_GPIB_USB(findDevices=True)
devices = usb.getDevices()
self.assertEqual(len(devices), 2 * 31)
self.assertEqual(devices[0], 0)
self.assertEqual(devices[1], "Device 1")
def test_if_platform_windows_and_not_port_defined_raises_not_implemented_exception(self):
self.platform_mock.system.return_value = "Windows"
with self.assertRaises(NotImplementedError):
hpib.PROLOGIX_GPIB_USB()
def test_if_given_port_does_not_start_with_dev_tty_an_exception_is_raised(self):
with self.assertRaises(ValueError):
hpib.PROLOGIX_GPIB_USB(port="device")
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment