Source

pylibftdi / tests / test_driver.py

Ben Bass 0b31119 












Ben Bass cfc0152 
Ben Bass 0b31119 










Ben Bass f95d570 
Ben Bass 0b31119 








Ben Bass 927fe8f 





Ben Bass 0b31119 






































Ben Bass f95d570 








Ben Bass 0b31119 
"""
pylibftdi - python wrapper for libftdi

Copyright (c) 2010-2012 Ben Bass <benbass@codedstructure.net>
See LICENSE file for details and (absence of) warranty

pylibftdi: http://bitbucket.org/codedstructure/pylibftdi

This module contains some basic tests for the higher-level
functionality without requiring an actual hardware device
to be attached.
"""

from .test_common import (LoopDevice, Device, CallCheckMixin, unittest)

# and now some test cases...


class DeviceFunctions(CallCheckMixin, unittest.TestCase):

    def testContextManager(self):
        def _():
            with Device():
                pass
        self.assertCallsExact(_,
                ['ftdi_init', 'ftdi_usb_open', 'ftdi_set_bitmode',
                 'ftdi_usb_close', 'ftdi_deinit'])

    def testOpen(self):
        # a lazy_open open() shouldn't do anything
        self.assertCallsExact(lambda: Device(lazy_open=True), [])
        # a non-lazy_open open() should open the port...
        self.assertCalls(lambda: Device(), 'ftdi_usb_open')
        # and given a device_id, it should do a open_desc
        self.assertCalls(lambda: Device('bogus'), 'ftdi_usb_open_desc')
        # check that opening a specific interface does that

    def testOpenInterface(self):
        self.assertCalls(lambda: Device(interface=1), 'ftdi_set_interface')
        # check that opening a specific interface does that
        self.assertNotCalls(lambda: Device(), 'ftdi_set_interface')

    def testReadWrite(self):
        with Device() as dev:
            self.assertCalls(lambda: dev.write('xxx'), 'ftdi_write_data')
            self.assertCalls(lambda: dev.read(10), 'ftdi_read_data')

    def testFlush(self):
        with Device() as dev:
            self.assertCalls(dev.flush_input, 'ftdi_usb_purge_rx_buffer')
            self.assertCalls(dev.flush_output, 'ftdi_usb_purge_tx_buffer')
            self.assertCalls(dev.flush, 'ftdi_usb_purge_buffers')


class LoopbackTest(unittest.TestCase):
    """
    these all require mode='t' to pass in Python3
    """

    def testPrint(self):
        d = LoopDevice(mode='t')
        d.write('Hello')
        d.write(' World\n')
        d.write('Bye')
        self.assertEqual(d.readline(), 'Hello World\n')
        self.assertEqual(d.readline(), 'Bye')

    def testLines(self):
        d = LoopDevice(mode='t')
        lines = ['Hello\n', 'World\n', 'And\n', 'Goodbye\n']
        d.writelines(lines)
        self.assertEqual(d.readlines(), lines)

    def testIterate(self):
        d = LoopDevice(mode='t')
        lines = ['Hello\n', 'World\n', 'And\n', 'Goodbye\n']
        d.writelines(lines)
        for idx, line in enumerate(d):
            self.assertEqual(line, lines[idx])

    def testBuffer(self):
        d = LoopDevice(mode='t', buffer_size=3)
        d.write('Hello')
        d.write(' World\n')
        d.write('Bye')
        self.assertEqual(d.readline(), 'Hello World\n')
        self.assertEqual(d.readline(), 'Bye')


if __name__ == "__main__":
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.