mirror of
https://github.com/naruxde/revpimodio2.git
synced 2025-11-08 13:53:53 +01:00
test: Replace io function
This commit is contained in:
@@ -6,6 +6,7 @@ __license__ = "GPLv2"
|
|||||||
|
|
||||||
from os.path import join, dirname
|
from os.path import join, dirname
|
||||||
|
|
||||||
|
from revpimodio2.io import IntIOReplaceable
|
||||||
from tests import TestRevPiModIO
|
from tests import TestRevPiModIO
|
||||||
|
|
||||||
|
|
||||||
@@ -13,6 +14,107 @@ class TestReplaceIO(TestRevPiModIO):
|
|||||||
|
|
||||||
data_dir = dirname(__file__)
|
data_dir = dirname(__file__)
|
||||||
|
|
||||||
|
def test_replacing(self):
|
||||||
|
"""Test replacing IOs."""
|
||||||
|
rpi = self.modio()
|
||||||
|
|
||||||
|
# Test type of IOs on an virtual device
|
||||||
|
for io in rpi.device.virt01:
|
||||||
|
self.assertIsInstance(io, IntIOReplaceable)
|
||||||
|
|
||||||
|
# Try to replace hardware IO
|
||||||
|
with self.assertRaises(AttributeError):
|
||||||
|
rpi.io.v_druck.replace_io("test2", frm="?")
|
||||||
|
|
||||||
|
rpi.io.pbit0_7.replace_io("test4", frm="?", bit=4)
|
||||||
|
rpi.io.pbit0_7.replace_io("test5", frm="?", bit=5, byteorder="big")
|
||||||
|
self.assertFalse(rpi.io.test4())
|
||||||
|
self.assertFalse(rpi.io.test4.value)
|
||||||
|
with self.assertRaises(MemoryError):
|
||||||
|
rpi.io.pbit0_7.replace_io("test4_2", frm="?", bit=4)
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
rpi.io.meldung0_7.replace_io("outtest", "?", bit=100)
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
rpi.io.meldung0_7.replace_io("outtest", "?", byteorder="test")
|
||||||
|
|
||||||
|
# Work with default values
|
||||||
|
rpi.io.meldung0_7.replace_io("outtest", "?", defaultvalue=True)
|
||||||
|
self.assertTrue(rpi.io.outtest.defaultvalue)
|
||||||
|
self.assertFalse(rpi.io.outtest.value)
|
||||||
|
rpi.io.outtest.value = True
|
||||||
|
self.assertTrue(rpi.io.outtest.value)
|
||||||
|
rpi.io.outtest.value = False
|
||||||
|
|
||||||
|
# Apply given default values
|
||||||
|
rpi.setdefaultvalues()
|
||||||
|
self.assertTrue(rpi.io.outtest.value)
|
||||||
|
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
rpi.io.pbit8_15.replace_io("test2", frm="hf")
|
||||||
|
|
||||||
|
rpi.io.pbit8_15.replace_io("test2", frm="h")
|
||||||
|
rpi.io.meldung8_15.replace_io(
|
||||||
|
"testmeldung1",
|
||||||
|
frm="h",
|
||||||
|
byteorder="big",
|
||||||
|
event=lambda io_name, io_value: None,
|
||||||
|
)
|
||||||
|
with self.assertRaises(MemoryError):
|
||||||
|
rpi.io.meldung0_7.replace_io("testmeldung2", frm="h", byteorder="big")
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
rpi.io._private_register_new_io_object(None)
|
||||||
|
with self.assertRaises(AttributeError):
|
||||||
|
rpi.io.testmeldung1.replace_io("testx", frm="?")
|
||||||
|
|
||||||
|
self.assertEqual(rpi.io.testmeldung1.defaultvalue, 0)
|
||||||
|
self.assertEqual(rpi.io.testmeldung1.frm, "h")
|
||||||
|
self.assertTrue(rpi.io.testmeldung1.signed)
|
||||||
|
self.assertEqual(rpi.io.testmeldung1.value, 0)
|
||||||
|
|
||||||
|
# Set value
|
||||||
|
rpi.io.testmeldung1.value = 200
|
||||||
|
self.assertEqual(rpi.io.testmeldung1(), 200)
|
||||||
|
rpi.io.testmeldung1(100)
|
||||||
|
self.assertEqual(rpi.io.testmeldung1.value, 100)
|
||||||
|
|
||||||
|
with self.assertRaises(BufferError):
|
||||||
|
rpi.io.Output_32.replace_io("test", "h")
|
||||||
|
|
||||||
|
# Byte value with default value
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
rpi.io.Output_9.replace_io("drehzahl", "H", defaultvalue=b"\x00\x00\x00")
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
rpi.io.Output_9.replace_io("drehzahl", "H", defaultvalue=b"\x00")
|
||||||
|
rpi.io.Output_9.replace_io("drehzahl", "H", defaultvalue=b"\xff\xff")
|
||||||
|
self.assertEqual(rpi.io.drehzahl.frm, "H")
|
||||||
|
self.assertFalse(rpi.io.drehzahl.signed)
|
||||||
|
self.assertEqual(rpi.io.drehzahl.defaultvalue, 65535)
|
||||||
|
self.assertEqual(rpi.io.drehzahl.value, 0)
|
||||||
|
rpi.setdefaultvalues()
|
||||||
|
self.assertEqual(rpi.io.drehzahl.value, 65535)
|
||||||
|
|
||||||
|
# Bit value with defaultvalue
|
||||||
|
rpi.io.Output_11._defaultvalue = b"\x02"
|
||||||
|
rpi.io.Output_11.replace_io("bitwert0", "?", bit=0)
|
||||||
|
rpi.io.Output_11.replace_io("bitwert1", "?", bit=1)
|
||||||
|
self.assertFalse(rpi.io.bitwert0.defaultvalue)
|
||||||
|
self.assertTrue(rpi.io.bitwert1.defaultvalue)
|
||||||
|
|
||||||
|
# Multi bytes
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
rpi.io.Output_11.replace_io("mehrbyte", "ss")
|
||||||
|
rpi.io.Output_11.replace_io("mehrbyte", "4s")
|
||||||
|
self.assertEqual(rpi.io.mehrbyte.length, 4)
|
||||||
|
self.assertEqual(rpi.io.mehrbyte.frm, "4s")
|
||||||
|
self.assertEqual(rpi.io.mehrbyte.value, b"\x00\x00\x00\x00")
|
||||||
|
rpi.io.mehrbyte.value = b"\xff\xff\xff\xff"
|
||||||
|
self.assertEqual(rpi.io.mehrbyte.value, b"\xff\xff\xff\xff")
|
||||||
|
|
||||||
|
# String defaultvalue (Encoding erros are filled with \x00)
|
||||||
|
rpi.io.Output_15.replace_io("string", "4s", defaultvalue="t\xffst")
|
||||||
|
self.assertEqual(rpi.io.string.value, b"\x00\x00\x00\x00")
|
||||||
|
|
||||||
def test_replace_io_file(self):
|
def test_replace_io_file(self):
|
||||||
replace_io_file = join(self.data_dir, "replace_io.conf")
|
replace_io_file = join(self.data_dir, "replace_io.conf")
|
||||||
rpi = self.modio(replace_io_file=replace_io_file)
|
rpi = self.modio(replace_io_file=replace_io_file)
|
||||||
@@ -44,7 +146,7 @@ class TestReplaceIO(TestRevPiModIO):
|
|||||||
self.assertTrue(rpi.io.byte_test.export)
|
self.assertTrue(rpi.io.byte_test.export)
|
||||||
self.assertEqual(rpi.io.byte_test.defaultvalue, b"\xff\x00\x80")
|
self.assertEqual(rpi.io.byte_test.defaultvalue, b"\xff\x00\x80")
|
||||||
|
|
||||||
def test_fb_replace_io_fail(self):
|
def test_replace_io_file_fail(self):
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
rpi = self.modio(replace_io_file=join(self.data_dir, "replace_io_fail.conf"))
|
rpi = self.modio(replace_io_file=join(self.data_dir, "replace_io_fail.conf"))
|
||||||
with self.assertRaises(RuntimeError):
|
with self.assertRaises(RuntimeError):
|
||||||
|
|||||||
Reference in New Issue
Block a user