Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
eblot committed Apr 6, 2024
1 parent 93954e2 commit 70e7796
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 92 deletions.
23 changes: 13 additions & 10 deletions pyftdi/tests/bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring

import unittest
from sys import modules
from unittest import TestCase, TestLoader, TestSuite, main as ut_main
from pyftdi.bits import BitSequence, BitZSequence, BitSequenceError


class BitSequenceTestCase(unittest.TestCase):
class BitSequenceTestCase(TestCase):

def setUp(self):
self.bs1 = BitSequence(0x01, msb=True, length=8)
Expand Down Expand Up @@ -80,19 +81,19 @@ def test_cmp(self):
self.assertTrue(bzs.matches(self.bs7))

def test_representation(self):
self.assertEqual(f'{self.bs1} / {self.bs1!r}' % (self.bs1, self.bs1),
self.assertEqual(f'{self.bs1} / {self.bs1!r}',
'8: 10000000 / 10000000')
self.assertEqual(f'{self.bs2} / {self.bs2!r}' % (self.bs2, self.bs2),
self.assertEqual(f'{self.bs2} / {self.bs2!r}',
'8: 01000000 / 01000000')
self.assertEqual(f'{self.bs3} / {self.bs3!r}' % (self.bs3, self.bs3),
self.assertEqual(f'{self.bs3} / {self.bs3!r}',
'7: 0010000 / 0010000')
self.assertEqual(f'{self.bs4} / {self.bs4!r}' % (self.bs4, self.bs4),
self.assertEqual(f'{self.bs4} / {self.bs4!r}',
'11: 001 00000000 / 00100000000')
self.assertEqual(f'{self.bs5} / {self.bs5!r}' % (self.bs5, self.bs5),
self.assertEqual(f'{self.bs5} / {self.bs5!r}',
'49: 1 00010000 11011001 00110001 01101110 10111111 '
'11111110 / 100010000110110010011000101101110101111'
'1111111110')
self.assertEqual(f'{self.bs6} / {self.bs6!r}' % (self.bs6, self.bs6),
self.assertEqual(f'{self.bs6} / {self.bs6!r}',
'49: 1 00010000 11011001 00110001 01101110 10111111 '
'11111111 / 100010000110110010011000101101110101111'
'1111111111')
Expand Down Expand Up @@ -210,8 +211,10 @@ def test_concat(self):


def suite():
return unittest.makeSuite(BitSequenceTestCase, 'test_')
suite_ = TestSuite()
suite_.addTest(TestLoader().loadTestsFromModule(modules[__name__]))
return suite_


if __name__ == '__main__':
unittest.main(defaultTest='suite')
ut_main(defaultTest='suite')
27 changes: 21 additions & 6 deletions pyftdi/tests/cbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@
import sys
from doctest import testmod
from os import environ
from unittest import TestCase, TestSuite, makeSuite, main as ut_main
from sys import modules
from unittest import TestCase, TestLoader, TestSuite, main as ut_main
from pyftdi.ftdi import Ftdi, FtdiError
from pyftdi.eeprom import FtdiEeprom

# pylint: disable=empty-docstring
# pylint: disable=missing-docstring


class CbusGpioTestCase(TestCase):
class CbusOutputGpioTestCase(TestCase):
"""FTDI CBUS GPIO feature test case"""

@classmethod
def setUpClass(cls):
"""Default values"""
cls.url = environ.get('FTDI_DEVICE', 'ftdi:///1')

def test_output_gpio(self):
def test_gpio(self):
"""Simple test to demonstrate ouput bit-banging on CBUS.
You need a CBUS-capable FTDI (FT232R/FT232H/FT230X/FT231X), whose
Expand Down Expand Up @@ -60,7 +61,16 @@ def test_output_gpio(self):
sig = int(not ftdi.get_cts()) | (int(not ftdi.get_dsr()) << 1)
self.assertEqual(value, sig)

def test_input_gpio(self):

class CbusInputGpioTestCase(TestCase):
"""FTDI CBUS GPIO feature test case"""

@classmethod
def setUpClass(cls):
"""Default values"""
cls.url = environ.get('FTDI_DEVICE', 'ftdi:///1')

def test_gpio(self):
"""Simple test to demonstrate input bit-banging on CBUS.
You need a CBUS-capable FTDI (FT232R/FT232H/FT230X/FT231X), whose
Expand Down Expand Up @@ -96,9 +106,14 @@ def test_input_gpio(self):

def suite():
suite_ = TestSuite()
loader = TestLoader()
mod = modules[__name__]
# peak the test that matches your HW setup, see test doc for details
# suite_.addTest(makeSuite(CbusGpioTestCase, 'test_output'))
suite_.addTest(makeSuite(CbusGpioTestCase, 'test_input'))
tests = ( # 'CbusOutputGpio',
'CbusInputGpio')
for testname in tests:
testcase = getattr(mod, f'{testname}TestCase')
suite_.addTest(loader.loadTestsFromTestCase(testcase))
return suite_


Expand Down
11 changes: 6 additions & 5 deletions pyftdi/tests/eeprom.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
# SPDX-License-Identifier: BSD-3-Clause

import logging
import unittest
from doctest import testmod
from unittest import TestCase, TestLoader, TestSuite, main as ut_main
from os import environ
from sys import modules, stdout
from pyftdi import FtdiLogger
Expand All @@ -20,7 +20,8 @@

# pylint: disable=missing-docstring

class EepromTestCase(unittest.TestCase):

class EepromTestCase(TestCase):
"""FTDI EEPROM access method test case"""

@classmethod
Expand Down Expand Up @@ -95,8 +96,8 @@ def test_eeprom_write(self):


def suite():
suite_ = unittest.TestSuite()
suite_.addTest(unittest.makeSuite(EepromTestCase, 'test'))
suite_ = TestSuite()
suite_.addTest(TestLoader().loadTestsFromModule(modules[__name__]))
return suite_


Expand All @@ -110,7 +111,7 @@ def main():
raise ValueError(f'Invalid log level: {level}') from exc
FtdiLogger.set_level(loglevel)
testmod(modules[__name__])
unittest.main(defaultTest='suite')
ut_main(defaultTest='suite')


if __name__ == '__main__':
Expand Down
34 changes: 20 additions & 14 deletions pyftdi/tests/eeprom_mock.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
from os import environ
from sys import modules, stdout
from unittest import TestCase, TestSuite, makeSuite, main as ut_main
from unittest import TestCase, TestLoader, TestSuite, main as ut_main
from pyftdi import FtdiLogger
from pyftdi.ftdi import Ftdi
from pyftdi.eeprom import FtdiEeprom
Expand Down Expand Up @@ -50,10 +50,10 @@ def setUp(self):

class EepromMirrorTestCase(FtdiTestCase):
"""Test FTDI EEPROM mirror feature (duplicate eeprom data over 2 eeprom
sectors). Generally this is tested with a virtual eeprom (by setting
environment variable FTDI_VIRTUAL=on), however you may also test with an
actual device at your own risk. Note that none of the tests should
commit any of their eeprom changes
sectors). Generally this is tested with a virtual eeprom (by setting
environment variable FTDI_VIRTUAL=on), however you may also test with an
actual device at your own risk. Note that none of the tests should
commit any of their eeprom changes
"""

@classmethod
Expand Down Expand Up @@ -200,7 +200,7 @@ def _check_for_mirrored_eeprom_contents(self, eeprom: FtdiEeprom):

class NonMirroredEepromTestCase(FtdiTestCase):
"""Test FTDI EEPROM mirror features do not break FTDI devices that do
not use mirroring
not use mirroring
"""
TEST_MANU_NAME = "MNAME"
TEST_PROD_NAME = "PNAME"
Expand Down Expand Up @@ -386,17 +386,23 @@ class EepromNonMirroredFt4232hTestCase(NonMirroredEepromTestCase, TestCase):

def suite():
suite_ = TestSuite()
loader = TestLoader()
mod = modules[__name__]
tests = []
# Test devices that support the mirroring capability
suite_.addTest(makeSuite(EepromMirrorFt232hTestCase, 'test'))
suite_.addTest(makeSuite(EepromMirrorFt2232hTestCase, 'test'))
suite_.addTest(makeSuite(EepromMirrorFt4232hTestCase, 'test'))
tests.extend(('EepromMirrorFt232h',
'EepromMirrorFt2232h',
'EepromMirrorFt4232h'))
# Test devices that do not support the mirror capability
suite_.addTest(makeSuite(EepromMirrorFt232rTestCase, 'test'))
suite_.addTest(makeSuite(EepromMirrorFt230xTestCase, 'test'))
tests.extend(('EepromMirrorFt232r',
'EepromMirrorFt230x'))
# test devices that support the mirroring capability, but have it disabled
suite_.addTest(makeSuite(EepromNonMirroredFt232hTestCase, 'test'))
suite_.addTest(makeSuite(EepromNonMirroredFt2232hTestCase, 'test'))
suite_.addTest(makeSuite(EepromNonMirroredFt4232hTestCase, 'test'))
tests.extend(('EepromNonMirroredFt232h',
'EepromNonMirroredFt2232h',
'EepromNonMirroredFt4232h'))
for testname in tests:
testcase = getattr(mod, f'{testname}TestCase')
suite_.addTest(loader.loadTestsFromTestCase(testcase))
return suite_


Expand Down
13 changes: 8 additions & 5 deletions pyftdi/tests/ftdi.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from os import environ
from sys import modules, stdout
from time import sleep, time as now
from unittest import TestCase, TestSuite, SkipTest, makeSuite, main as ut_main
from unittest import TestCase, TestLoader, TestSuite, SkipTest, main as ut_main
from pyftdi import FtdiLogger
from pyftdi.ftdi import Ftdi, FtdiError
from pyftdi.usbtools import UsbTools, UsbToolsError
Expand Down Expand Up @@ -152,10 +152,13 @@ def test_close_on_disconnect(self):

def suite():
suite_ = TestSuite()
#suite_.addTest(makeSuite(FtdiTestCase, 'test'))
#suite_.addTest(makeSuite(HotplugTestCase, 'test'))
suite_.addTest(makeSuite(ResetTestCase, 'test'))
suite_.addTest(makeSuite(DisconnectTestCase, 'test'))
loader = TestLoader()
mod = modules[__name__]
# tests = 'Ftdi Hotplug Reset Disconnect'
tests = 'Reset Disconnect'
for testname in tests.split():
testcase = getattr(mod, f'{testname}TestCase')
suite_.addTest(loader.loadTestsFromTestCase(testcase))
return suite_


Expand Down
7 changes: 2 additions & 5 deletions pyftdi/tests/gpio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from os import environ
from sys import modules, stdout
from time import sleep
from unittest import TestCase, TestSuite, SkipTest, makeSuite, main as ut_main
from unittest import TestCase, TestLoader, TestSuite, SkipTest, main as ut_main
from pyftdi import FtdiLogger
from pyftdi.ftdi import Ftdi
from pyftdi.gpio import (GpioAsyncController,
Expand Down Expand Up @@ -607,10 +607,7 @@ def test_stream_gpio(self):

def suite():
suite_ = TestSuite()
suite_.addTest(makeSuite(GpioAsyncTestCase, 'test'))
suite_.addTest(makeSuite(GpioSyncTestCase, 'test'))
suite_.addTest(makeSuite(GpioMpsseTestCase, 'test'))
suite_.addTest(makeSuite(GpioMultiportTestCase, 'test'))
suite_.addTest(TestLoader().loadTestsFromModule(modules[__name__]))
return suite_


Expand Down
37 changes: 19 additions & 18 deletions pyftdi/tests/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# SPDX-License-Identifier: BSD-3-Clause

import logging
from unittest import TestCase, TestSuite, main as ut_main, makeSuite
from unittest import TestCase, TestLoader, TestSuite, main as ut_main
from binascii import hexlify
from doctest import testmod
from os import environ
Expand Down Expand Up @@ -54,7 +54,7 @@ def _close(self):
self._i2c.terminate()


class I2cAccelTest(TestCase):
class I2cAccelTestCase(TestCase):
"""Basic test for an ADXL345 device on I2C bus @ address 0x53
"""

Expand All @@ -81,7 +81,7 @@ def _close(self):
self._i2c.terminate()


class I2cReadTest(TestCase):
class I2cReadTestCase(TestCase):
"""Simple test to read a sequence of bytes I2C bus @ address 0x36
"""

Expand All @@ -108,7 +108,7 @@ def _close(self):
self._i2c.terminate()


class I2cEepromTest(TestCase):
class I2cEepromTestCase(TestCase):
"""Simple test to read a sequence of bytes I2C bus @ address 0x50,
from an I2C data flash
"""
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_long(self):
self.assertEqual(text[8:12], 'Worl')


class I2cReadGpioTest(TestCase):
class I2cReadGpioTestCase(TestCase):
"""Simple test to exercise I2C + GPIO mode.
A slave device (such as EEPROM) should be connected to the I2C bus
Expand Down Expand Up @@ -225,7 +225,7 @@ def _close(self):
self._i2c.terminate()


class I2cClockStrechingGpioCheck(TestCase):
class I2cClockStrechingGpioTestCase(TestCase):
"""Simple test to check clock stretching cannot be overwritten with
GPIOs.
"""
Expand All @@ -238,7 +238,7 @@ def test(self):
self.assertRaises(I2cIOError, gpio.set_direction, 1 << 7, 0)


class I2cDualMaster(TestCase):
class I2cDualMasterTestCase(TestCase):
"""Check the behaviour of 2 I2C masters. Requires a multi port FTDI device,
i.e. FT2232H, FT4232H or FT4232HA. See issue #159.
"""
Expand All @@ -255,7 +255,7 @@ def test(self):
print(port.read_from(0x00, 2))


class I2cIssue143(TestCase):
class I2cIssue143TestCase(TestCase):
"""#143.
"""

Expand Down Expand Up @@ -289,16 +289,17 @@ def suite():
Do NOT run this test if you use FTDI port A as an UART or SPI
bridge -or any unsupported setup!! You've been warned.
"""
ste = TestSuite()
# ste.addTest(I2cTca9555TestCase('test'))
# ste.addTest(I2cAccelTest('test'))
# ste.addTest(I2cReadTest('test'))
ste.addTest(makeSuite(I2cEepromTest, 'test'))
# ste.addTest(I2cReadGpioTest('test'))
ste.addTest(I2cClockStrechingGpioCheck('test'))
# ste.addTest(I2cDualMaster('test'))
ste.addTest(I2cIssue143('test'))
return ste
suite_ = TestSuite()
loader = TestLoader()
mod = modules[__name__]
tests = ( # 'I2cTca9555', 'I2cAccel', 'I2cRead',
'I2cEeprom', # 'I2cReadGpio',
'I2cClockStrechingGpio', # 'I2cDualMaster',
'I2cIssue143')
for testname in tests:
testcase = getattr(mod, f'{testname}TestCase')
suite_.addTest(loader.loadTestsFromTestCase(testcase))
return suite_


def main():
Expand Down
7 changes: 5 additions & 2 deletions pyftdi/tests/jtag.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# SPDX-License-Identifier: BSD-3-Clause

from os import environ
from unittest import TestCase, main as ut_main, makeSuite
from sys import modules
from unittest import TestCase, TestLoader, TestSuite, main as ut_main
from pyftdi.jtag import JtagEngine, JtagTool
from pyftdi.bits import BitSequence

Expand Down Expand Up @@ -85,7 +86,9 @@ def _test_detect_ir_length(self):


def suite():
return makeSuite(JtagTestCase, 'test')
suite_ = TestSuite()
suite_.addTest(TestLoader().loadTestsFromModule(modules[__name__]))
return suite_


if __name__ == '__main__':
Expand Down

0 comments on commit 70e7796

Please sign in to comment.