1
+ """A module to mock the data bus transactions."""
2
+
1
3
from enum import Enum , auto
2
4
import sys
3
5
from typing import List
4
6
5
7
import circuitpython_typing
6
8
7
9
from circuitpython_mocks .busio .operations import (
8
- Read ,
9
- Write ,
10
- Transfer ,
10
+ UARTRead ,
11
+ UARTWrite ,
11
12
I2CRead ,
12
13
I2CWrite ,
13
14
I2CTransfer ,
15
+ SPIRead ,
16
+ SPIWrite ,
17
+ SPITransfer ,
14
18
)
15
19
from circuitpython_mocks ._mixins import Expecting , Lockable
16
20
from circuitpython_mocks .board import Pin
17
21
18
22
19
23
class I2C (Expecting , Lockable ):
24
+ """A mock of `busio.I2C` class."""
25
+
20
26
def __init__ (
21
27
self ,
22
28
scl : Pin ,
@@ -28,6 +34,8 @@ def __init__(
28
34
super ().__init__ ()
29
35
30
36
def scan (self ) -> List [int ]:
37
+ """Returns an empty list.
38
+ Use :py:meth:`pytest.MonkeyPatch.setattr()` to change this output."""
31
39
return []
32
40
33
41
def readfrom_into (
@@ -38,6 +46,9 @@ def readfrom_into(
38
46
start : int = 0 ,
39
47
end : int = sys .maxsize ,
40
48
) -> None :
49
+ """A mock imitation of :external:py:meth:`busio.I2C.readfrom_into()`.
50
+ This function checks against `I2CRead`
51
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
41
52
assert self .expectations , "no expectation found for I2C.readfrom_into()"
42
53
op = self .expectations .popleft ()
43
54
assert isinstance (op , I2CRead ), f"Read operation expected, found { repr (op )} "
@@ -53,6 +64,9 @@ def writeto(
53
64
start : int = 0 ,
54
65
end : int = sys .maxsize ,
55
66
) -> None :
67
+ """A mock imitation of :external:py:meth:`busio.I2C.writeto()`.
68
+ This function checks against `I2CWrite`
69
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
56
70
assert self .expectations , "no expectation found for I2C.writeto()"
57
71
op = self .expectations .popleft ()
58
72
assert isinstance (op , I2CWrite ), f"Read operation expected, found { repr (op )} "
@@ -70,6 +84,9 @@ def writeto_then_readfrom(
70
84
in_start : int = 0 ,
71
85
in_end : int = sys .maxsize ,
72
86
) -> None :
87
+ """A mock imitation of :external:py:meth:`busio.I2C.writeto_then_readfrom()`.
88
+ This function checks against `I2CTransfer`
89
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
73
90
assert self .expectations , "no expectation found for I2C.writeto_then_readfrom()"
74
91
op = self .expectations .popleft ()
75
92
assert isinstance (
@@ -89,6 +106,7 @@ def __init__(
89
106
MISO : Pin | None = None ,
90
107
half_duplex : bool = False ,
91
108
):
109
+ """A class to mock :external:py:class:`busio.SPI`."""
92
110
super ().__init__ ()
93
111
self ._frequency = 1000000
94
112
@@ -100,10 +118,12 @@ def configure(
100
118
phase : int = 0 ,
101
119
bits : int = 8 ,
102
120
) -> None :
121
+ """A dummy function to mock :external:py:meth:`busio.SPI.configure()`"""
103
122
self ._frequency = baudrate
104
123
105
124
@property
106
125
def frequency (self ) -> int :
126
+ """Returns the value passed to ``baudrate`` parameter of `configure()`."""
107
127
return self ._frequency
108
128
109
129
def write (
@@ -113,9 +133,12 @@ def write(
113
133
start : int = 0 ,
114
134
end : int = sys .maxsize ,
115
135
) -> None :
136
+ """A function that mocks :external:py:meth:`busio.SPI.write()`.
137
+ This function checks against `SPIWrite`
138
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
116
139
assert self .expectations , "no expectation found for SPI.write()"
117
140
op = self .expectations .popleft ()
118
- assert isinstance (op , Write ), f"Read operation expected, found { repr (op )} "
141
+ assert isinstance (op , SPIWrite ), f"Read operation expected, found { repr (op )} "
119
142
op .assert_expected (buffer , start , end )
120
143
121
144
def readinto (
@@ -126,9 +149,12 @@ def readinto(
126
149
end : int = sys .maxsize ,
127
150
write_value : int = 0 ,
128
151
) -> None :
152
+ """A function that mocks :external:py:meth:`busio.SPI.readinto()`.
153
+ This function checks against `SPIRead`
154
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
129
155
assert self .expectations , "no expectation found for SPI.readinto()"
130
156
op = self .expectations .popleft ()
131
- assert isinstance (op , Read ), f"Read operation expected, found { repr (op )} "
157
+ assert isinstance (op , SPIRead ), f"Read operation expected, found { repr (op )} "
132
158
op .assert_response (buffer , start , end )
133
159
134
160
def write_readinto (
@@ -141,20 +167,23 @@ def write_readinto(
141
167
in_start : int = 0 ,
142
168
in_end : int = sys .maxsize ,
143
169
) -> None :
170
+ """A function that mocks :external:py:meth:`busio.SPI.write_readinto()`.
171
+ This function checks against `SPITransfer`
172
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
144
173
assert self .expectations , "no expectation found for I2C.writeto_then_readfrom()"
145
174
op = self .expectations .popleft ()
146
175
assert isinstance (
147
- op , Transfer
176
+ op , SPITransfer
148
177
), f"Transfer operation expected, found { repr (op )} "
149
178
op .assert_transaction (
150
179
out_buffer , in_buffer , out_start , out_end , in_start , in_end
151
180
)
152
181
153
182
154
183
class UART (Expecting , Lockable ):
155
- class Parity (Enum ):
156
- """Parity Enumeration"""
184
+ """A class that mocks :external:py:class:`busio.UART`."""
157
185
186
+ class Parity (Enum ):
158
187
ODD = auto ()
159
188
EVEN = auto ()
160
189
@@ -177,35 +206,47 @@ def __init__(
177
206
super ().__init__ ()
178
207
179
208
def read (self , nbytes : int | None = None ) -> bytes | None :
209
+ """A function that mocks :external:py:meth:`busio.UART.read()`.
210
+ This function checks against `UARTRead`
211
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
180
212
assert self .expectations , "no expectation found for UART.read()"
181
213
op = self .expectations .popleft ()
182
- assert isinstance (op , Read ), f"Read operation expected, found { repr (op )} "
214
+ assert isinstance (op , UARTRead ), f"Read operation expected, found { repr (op )} "
183
215
length = nbytes or len (op .response )
184
216
buffer = bytearray (length )
185
217
op .assert_response (buffer , 0 , length )
186
218
return bytes (buffer )
187
219
188
220
def readinto (self , buf : circuitpython_typing .WriteableBuffer ) -> int | None :
221
+ """A function that mocks :external:py:meth:`busio.UART.readinto()`.
222
+ This function checks against `UARTRead`
223
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
189
224
assert self .expectations , "no expectation found for UART.readinto()"
190
225
op = self .expectations .popleft ()
191
- assert isinstance (op , Read ), f"Read operation expected, found { repr (op )} "
226
+ assert isinstance (op , UARTRead ), f"Read operation expected, found { repr (op )} "
192
227
len_buf = len (op .response )
193
228
op .assert_response (buf , 0 , len_buf )
194
229
return len_buf
195
230
196
231
def readline (self ) -> bytes :
232
+ """A function that mocks :external:py:meth:`busio.UART.readline()`.
233
+ This function checks against `UARTRead`
234
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
197
235
assert self .expectations , "no expectation found for UART.readline()"
198
236
op = self .expectations .popleft ()
199
- assert isinstance (op , Read ), f"Read operation expected, found { repr (op )} "
237
+ assert isinstance (op , UARTRead ), f"Read operation expected, found { repr (op )} "
200
238
len_buf = len (op .response )
201
239
buf = bytearray (len_buf )
202
240
op .assert_response (buf , 0 , len_buf )
203
241
return bytes (buf )
204
242
205
243
def write (self , buf : circuitpython_typing .ReadableBuffer ) -> int | None :
244
+ """A function that mocks :external:py:meth:`busio.UART.write()`.
245
+ This function checks against `UARTWrite`
246
+ :py:attr:`~circuitpython_mocks._mixins.Expecting.expectations`"""
206
247
assert self .expectations , "no expectation found for UART.write()"
207
248
op = self .expectations .popleft ()
208
- assert isinstance (op , Write ), f"Read operation expected, found { repr (op )} "
249
+ assert isinstance (op , UARTWrite ), f"Read operation expected, found { repr (op )} "
209
250
len_buf = len (op .expected )
210
251
op .assert_expected (buf , 0 , len_buf )
211
252
return len (buf ) or None
0 commit comments