88import feeph .i2c as sut # sytem under test
99
1010
11- # pylint: disable=protected-access
11+ # pylint: disable=protected-access,too-many-public-methods
1212class TestBurstHandler (unittest .TestCase ):
1313
1414 def test_read_device_register (self ):
@@ -39,6 +39,15 @@ def test_read_device_register_multibyte(self):
3939 # -----------------------------------------------------------------
4040 self .assertEqual (computed , expected )
4141
42+ def test_read_device_register_insufficient_tries (self ):
43+ i2c_bus = sut .EmulatedI2C (state = {})
44+ # -----------------------------------------------------------------
45+ # -----------------------------------------------------------------
46+ with sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x4C ) as bh :
47+ # under realistic circumstances the max_tries would be a positive
48+ # value but we're intentionally setting it to 0 to force an error
49+ self .assertRaises (RuntimeError , bh .read_register , 0x00 , max_tries = 0 )
50+
4251 def test_read_device_registers (self ):
4352 state = {
4453 0x4C : {
@@ -105,6 +114,15 @@ def test_write_device_register_multibyte(self):
105114 # -----------------------------------------------------------------
106115 self .assertEqual (computed , expected )
107116
117+ def test_write_device_register_insufficient_tries (self ):
118+ i2c_bus = sut .EmulatedI2C (state = {})
119+ # -----------------------------------------------------------------
120+ # -----------------------------------------------------------------
121+ with sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x4C ) as bh :
122+ # under realistic circumstances the max_tries would be a positive
123+ # value but we're intentionally setting it to 0 to force an error
124+ self .assertRaises (RuntimeError , bh .write_register , 0x00 , value = 0x12 , max_tries = 0 )
125+
108126 def test_write_device_registers (self ):
109127 state = {
110128 0x4C : {
@@ -188,6 +206,15 @@ def test_get_state_multibyte(self):
188206 # -----------------------------------------------------------------
189207 self .assertEqual (computed , expected )
190208
209+ def test_get_state_insufficient_tries (self ):
210+ i2c_bus = sut .EmulatedI2C (state = {})
211+ # -----------------------------------------------------------------
212+ # -----------------------------------------------------------------
213+ with sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x4C ) as bh :
214+ # under realistic circumstances the max_tries would be a positive
215+ # value but we're intentionally setting it to 0 to force an error
216+ self .assertRaises (RuntimeError , bh .get_state , max_tries = 0 )
217+
191218 def test_set_state (self ):
192219 state = {
193220 0x70 : {- 1 : 0x00 },
@@ -206,11 +233,49 @@ def test_set_state_multibyte(self):
206233 # -----------------------------------------------------------------
207234 # -----------------------------------------------------------------
208235 with sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x70 ) as bh :
209- self .assertRaises (ValueError , bh .set_state , value = 0x0102 )
236+ self .assertRaises (ValueError , bh .set_state , value = 0x0102 , byte_count = 2 )
237+
238+ def test_set_state_insufficient_tries (self ):
239+ i2c_bus = sut .EmulatedI2C (state = {})
240+ # -----------------------------------------------------------------
241+ # -----------------------------------------------------------------
242+ with sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x4C ) as bh :
243+ # under realistic circumstances the max_tries would be a positive
244+ # value but we're intentionally setting it to 0 to force an error
245+ self .assertRaises (RuntimeError , bh .set_state , value = 0x12 , max_tries = 0 )
210246
211247 # ---------------------------------------------------------------------
212248
213- def test_no_timeout (self ):
249+ def test_invalid_device_address (self ):
250+ # this code tests the equivalent of:
251+ # with sut.BurstHandler(i2c_bus=i2c_bus, i2c_adr=0xFFFF) as bh:
252+ # ...
253+ i2c_bus = sut .EmulatedI2C (state = {})
254+ bh = sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0xFFFF )
255+ # -----------------------------------------------------------------
256+ # -----------------------------------------------------------------
257+ self .assertRaises (ValueError , bh .__enter__ )
258+
259+ def test_invalid_device_register (self ):
260+ # this code tests the equivalent of:
261+ # with sut.BurstHandler(i2c_bus=i2c_bus, i2c_adr=0xFFFF) as bh:
262+ # ...
263+ i2c_bus = sut .EmulatedI2C (state = {})
264+ # -----------------------------------------------------------------
265+ # -----------------------------------------------------------------
266+ with sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x70 ) as bh :
267+ self .assertRaises (ValueError , bh .read_register , register = 0xFFFF )
268+
269+ def test_invalid_timeout (self ):
270+ # this code tests the equivalent of:
271+ # with sut.BurstHandler(i2c_bus=i2c_bus, i2c_adr=0x4C, timeout_ms=0) as bh:
272+ # ...
273+ i2c_bus = sut .EmulatedI2C (state = {}, lock_chance = 1 )
274+ # -----------------------------------------------------------------
275+ # -----------------------------------------------------------------
276+ self .assertRaises (ValueError , sut .BurstHandler , i2c_bus = i2c_bus , i2c_adr = 0x4C , timeout_ms = 0 )
277+
278+ def test_hard_to_lock (self ):
214279 state = {
215280 0x4C : {
216281 0x00 : 0x12 ,
@@ -225,3 +290,13 @@ def test_no_timeout(self):
225290 expected = 0x12
226291 # -----------------------------------------------------------------
227292 self .assertEqual (computed , expected )
293+
294+ def test_unable_to_lock (self ):
295+ # this code tests the equivalent of:
296+ # with sut.BurstHandler(i2c_bus=i2c_bus, i2c_adr=0x4C) as bh:
297+ # ...
298+ i2c_bus = sut .EmulatedI2C (state = {}, lock_chance = 0 ) # impossible to acquire a lock
299+ bh = sut .BurstHandler (i2c_bus = i2c_bus , i2c_adr = 0x4C )
300+ # -----------------------------------------------------------------
301+ # -----------------------------------------------------------------
302+ self .assertRaises (RuntimeError , bh .__enter__ )
0 commit comments