44
55 .. md-tab-item:: I2C
66
7- .. literalinclude:: ../tests/test_i2c.py
7+ .. literalinclude:: ../../ tests/test_i2c.py
88 :language: python
99
1010 .. md-tab-item:: SPI
1111
12- .. literalinclude:: ../tests/test_spi.py
12+ .. literalinclude:: ../../ tests/test_spi.py
1313 :language: python
1414
1515 .. md-tab-item:: UART
1616
17- .. literalinclude:: ../tests/test_uart.py
17+ .. literalinclude:: ../../ tests/test_uart.py
1818 :language: python
1919"""
2020
2727from circuitpython_mocks .busio .operations import (
2828 UARTRead ,
2929 UARTWrite ,
30+ UARTFlush ,
3031 I2CRead ,
3132 I2CWrite ,
3233 I2CTransfer ,
@@ -310,9 +311,11 @@ def __init__(
310311 timeout : float = 1 ,
311312 receiver_buffer_size : int = 64 ,
312313 ) -> None :
314+ self ._baudrate = baudrate
315+ self ._timeout = timeout
313316 super ().__init__ ()
314317
315- def read (self , nbytes : int | None = None ) -> bytes | None :
318+ def read (self , nbytes : int | None = None ) -> Optional [ bytes ] :
316319 """A function that mocks :external:py:meth:`busio.UART.read()`.
317320
318321 .. mock-expects::
@@ -323,12 +326,12 @@ def read(self, nbytes: int | None = None) -> bytes | None:
323326 assert self .expectations , "no expectation found for UART.read()"
324327 op = self .expectations .popleft ()
325328 assert isinstance (op , UARTRead ), f"Read operation expected, found { repr (op )} "
326- length = nbytes or len (op .response )
329+ length = nbytes or 0 if not op . response else len (op .response )
327330 buffer = bytearray (length )
328331 op .assert_response (buffer , 0 , length )
329- return bytes (buffer )
332+ return None if not buffer else bytes (buffer )
330333
331- def readinto (self , buf : circuitpython_typing .WriteableBuffer ) -> int | None :
334+ def readinto (self , buf : circuitpython_typing .WriteableBuffer ) -> Optional [ int ] :
332335 """A function that mocks :external:py:meth:`busio.UART.readinto()`.
333336
334337 .. mock-expects::
@@ -339,11 +342,11 @@ def readinto(self, buf: circuitpython_typing.WriteableBuffer) -> int | None:
339342 assert self .expectations , "no expectation found for UART.readinto()"
340343 op = self .expectations .popleft ()
341344 assert isinstance (op , UARTRead ), f"Read operation expected, found { repr (op )} "
342- len_buf = len (op .response )
345+ len_buf = 0 if not op . response else len (op .response )
343346 op .assert_response (buf , 0 , len_buf )
344347 return len_buf
345348
346- def readline (self ) -> bytes :
349+ def readline (self ) -> Optional [ bytes ] :
347350 """A function that mocks :external:py:meth:`busio.UART.readline()`.
348351
349352 .. mock-expects::
@@ -354,10 +357,10 @@ def readline(self) -> bytes:
354357 assert self .expectations , "no expectation found for UART.readline()"
355358 op = self .expectations .popleft ()
356359 assert isinstance (op , UARTRead ), f"Read operation expected, found { repr (op )} "
357- len_buf = len (op .response )
360+ len_buf = 0 if not op . response else len (op .response )
358361 buf = bytearray (len_buf )
359362 op .assert_response (buf , 0 , len_buf )
360- return bytes (buf )
363+ return None if buf else bytes (buf )
361364
362365 def write (self , buf : circuitpython_typing .ReadableBuffer ) -> int | None :
363366 """A function that mocks :external:py:meth:`busio.UART.write()`.
@@ -373,3 +376,48 @@ def write(self, buf: circuitpython_typing.ReadableBuffer) -> int | None:
373376 len_buf = len (op .expected )
374377 op .assert_expected (buf , 0 , len_buf )
375378 return len (buf ) or None
379+
380+ @property
381+ def baudrate (self ) -> int :
382+ """The current baudrate."""
383+ return self ._baudrate
384+
385+ @baudrate .setter
386+ def baudrate (self , val : int ):
387+ self ._baudrate = int (val )
388+
389+ @property
390+ def timeout (self ) -> float :
391+ """The current timeout, in seconds."""
392+ return self ._timeout
393+
394+ @timeout .setter
395+ def timeout (self , val : float ):
396+ self ._timeout = float (val )
397+
398+ @property
399+ def in_waiting (self ) -> int :
400+ """The number of bytes in the input buffer, available to be read.
401+
402+ .. mock-expects::
403+
404+ This property peeks at the number of bytes in next available operation in
405+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`. If a
406+ `UARTRead` operation is not immediately expected, then ``0`` is returned.
407+ """
408+ if self .expectations and isinstance (self .expectations [0 ], UARTRead ):
409+ return len (self .expectations [0 ].response )
410+ return 0
411+
412+ def reset_input_buffer (self ) -> None :
413+ """Discard any unread characters in the input buffer.
414+
415+ .. mock-expects::
416+
417+ This function merely checks the immediately queued
418+ :py:class:`~circuitpython_mocks._mixins.Expecting.expectations` for
419+ a `UARTFlush` operation. It does not actually discard any data.
420+ """
421+ assert self .expectations
422+ op = self .expectations .popleft ()
423+ assert isinstance (op , UARTFlush ), f"Flush operation expected, found { repr (op )} "
0 commit comments