forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_util.py
More file actions
78 lines (60 loc) · 2.6 KB
/
test_util.py
File metadata and controls
78 lines (60 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
import unittest
import warnings
from can.util import _create_bus_config, _rename_kwargs, channel2int
class RenameKwargsTest(unittest.TestCase):
expected_kwargs = dict(a=1, b=2, c=3, d=4)
def _test(self, kwargs, aliases):
# Test that we do get the DeprecationWarning when called with deprecated kwargs
with self.assertWarnsRegex(DeprecationWarning, "is deprecated"):
_rename_kwargs("unit_test", kwargs, aliases)
# Test that the aliases contains the deprecated values and
# the obsolete kwargs have been removed
assert kwargs == self.expected_kwargs
# Test that we do not get a DeprecationWarning when we call
# without deprecated kwargs
# Cause all warnings to always be triggered.
warnings.simplefilter("error", DeprecationWarning)
try:
_rename_kwargs("unit_test", kwargs, aliases)
finally:
warnings.resetwarnings()
def test_rename(self):
kwargs = dict(old_a=1, old_b=2, c=3, d=4)
aliases = {"old_a": "a", "old_b": "b"}
self._test(kwargs, aliases)
def test_obsolete(self):
kwargs = dict(a=1, b=2, c=3, d=4, z=10)
aliases = {"z": None}
self._test(kwargs, aliases)
def test_rename_and_obsolete(self):
kwargs = dict(old_a=1, old_b=2, c=3, d=4, z=10)
aliases = {"old_a": "a", "old_b": "b", "z": None}
self._test(kwargs, aliases)
def test_with_new_and_alias_present(self):
kwargs = dict(old_a=1, a=1, b=2, c=3, d=4, z=10)
aliases = {"old_a": "a", "old_b": "b", "z": None}
with self.assertRaises(TypeError):
self._test(kwargs, aliases)
class TestBusConfig(unittest.TestCase):
base_config = dict(interface="socketcan", bitrate=500_000)
def test_timing_can_use_int(self):
"""
Test that an exception is not raised when using
integers for timing values in config.
"""
timing_conf = dict(tseg1=5, tseg2=10, sjw=25)
try:
_create_bus_config({**self.base_config, **timing_conf})
except TypeError as e:
self.fail(e)
class TestChannel2Int(unittest.TestCase):
def test_channel2int(self) -> None:
self.assertEqual(0, channel2int("can0"))
self.assertEqual(0, channel2int("vcan0"))
self.assertEqual(1, channel2int("vcan1"))
self.assertEqual(12, channel2int("vcan12"))
self.assertEqual(3, channel2int(3))
self.assertEqual(42, channel2int("42"))
self.assertEqual(None, channel2int("can"))
self.assertEqual(None, channel2int("can0a"))