5
5
6
6
import pytest
7
7
8
+ import can
8
9
from can import BitTiming , BitTimingFd
9
10
from can .exceptions import CanInitializationError
10
11
from can .util import (
@@ -132,10 +133,7 @@ def _test_func3(a):
132
133
133
134
134
135
class TestBusConfig (unittest .TestCase ):
135
- base_config = dict (interface = "socketcan" , bitrate = 500_000 )
136
- port_alpha_config = dict (interface = "socketcan" , bitrate = 500_000 , port = "fail123" )
137
- port_to_high_config = dict (interface = "socketcan" , bitrate = 500_000 , port = "999999" )
138
- port_wrong_type_config = dict (interface = "socketcan" , bitrate = 500_000 , port = (1234 ,))
136
+ base_config = {"interface" : "socketcan" , "bitrate" : 500_000 }
139
137
140
138
def test_timing_can_use_int (self ):
141
139
"""
@@ -147,17 +145,69 @@ def test_timing_can_use_int(self):
147
145
_create_bus_config ({** self .base_config , ** timing_conf })
148
146
except TypeError as e :
149
147
self .fail (e )
148
+
149
+ def test_port_datatype (self ):
150
150
self .assertRaises (
151
- ValueError , _create_bus_config , {** self .port_alpha_config , ** timing_conf }
151
+ ValueError , _create_bus_config , {** self .base_config , "port" : "fail123" }
152
152
)
153
153
self .assertRaises (
154
- ValueError , _create_bus_config , {** self .port_to_high_config , ** timing_conf }
154
+ ValueError , _create_bus_config , {** self .base_config , "port" : "999999" }
155
155
)
156
156
self .assertRaises (
157
- TypeError ,
158
- _create_bus_config ,
159
- {** self .port_wrong_type_config , ** timing_conf },
157
+ TypeError , _create_bus_config , {** self .base_config , "port" : (1234 ,)}
158
+ )
159
+
160
+ try :
161
+ _create_bus_config ({** self .base_config , "port" : "1234" })
162
+ except TypeError as e :
163
+ self .fail (e )
164
+
165
+ def test_bit_timing_cfg (self ):
166
+ can_cfg = _create_bus_config (
167
+ {
168
+ ** self .base_config ,
169
+ "f_clock" : "8000000" ,
170
+ "brp" : "1" ,
171
+ "tseg1" : "5" ,
172
+ "tseg2" : "2" ,
173
+ "sjw" : "1" ,
174
+ "nof_samples" : "1" ,
175
+ }
176
+ )
177
+ timing = can_cfg ["timing" ]
178
+ assert isinstance (timing , can .BitTiming )
179
+ assert timing .f_clock == 8_000_000
180
+ assert timing .brp == 1
181
+ assert timing .tseg1 == 5
182
+ assert timing .tseg2 == 2
183
+ assert timing .sjw == 1
184
+
185
+ def test_bit_timing_fd_cfg (self ):
186
+ canfd_cfg = _create_bus_config (
187
+ {
188
+ ** self .base_config ,
189
+ "f_clock" : "80000000" ,
190
+ "nom_brp" : "1" ,
191
+ "nom_tseg1" : "119" ,
192
+ "nom_tseg2" : "40" ,
193
+ "nom_sjw" : "40" ,
194
+ "data_brp" : "1" ,
195
+ "data_tseg1" : "29" ,
196
+ "data_tseg2" : "10" ,
197
+ "data_sjw" : "10" ,
198
+ }
160
199
)
200
+ timing = canfd_cfg ["timing" ]
201
+ assert isinstance (timing , can .BitTimingFd )
202
+ assert timing .f_clock == 80_000_000
203
+ assert timing .nom_brp == 1
204
+ assert timing .nom_tseg1 == 119
205
+ assert timing .nom_tseg2 == 40
206
+ assert timing .nom_sjw == 40
207
+ assert timing .data_brp == 1
208
+ assert timing .data_tseg1 == 29
209
+ assert timing .data_tseg2 == 10
210
+ assert timing .data_sjw == 10
161
211
162
212
163
213
class TestChannel2Int (unittest .TestCase ):
0 commit comments