pylibftdi / tests / test_bus.py

"""
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 unittest
from pylibftdi.util import Bus


class TestBus(unittest.TestCase):

    class MockDevice(object):
        port = 0

    class Bus1(object):
        a = Bus(0, 2)
        b = Bus(2, 1)
        c = Bus(3, 5)

        def __init__(self):
            self.device = TestBus.MockDevice()

    def test_bus_write(self):
        test_bus = TestBus.Bus1()
        # test writing to the bus
        self.assertEqual(test_bus.device.port, 0)
        test_bus.a = 3
        test_bus.b = 1
        test_bus.c = 31
        self.assertEqual(test_bus.device.port, 255)
        test_bus.b = 0
        self.assertEqual(test_bus.device.port, 251)
        test_bus.c = 16
        self.assertEqual(test_bus.device.port, 131)

    def test_bus_read(self):
        test_bus = TestBus.Bus1()
        # test reading from the bus
        test_bus.device.port = 0x55
        assert test_bus.a == 1
        assert test_bus.b == 1
        assert test_bus.c == 10
        test_bus.device.port = 0xAA
        assert test_bus.a == 2
        assert test_bus.b == 0
        assert test_bus.c == 21
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.