Skip to content

Commit 59f3a74

Browse files
committed
adjust tests for accuracy
1 parent 672b23d commit 59f3a74

3 files changed

Lines changed: 21 additions & 14 deletions

File tree

.cspell.config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ words:
3838
- spidev
3939
- testpaths
4040
- urandom
41+
- xfer

tests/conftest.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
registers in a dict."""
33

44
import logging
5-
from typing import Union, Tuple
5+
from typing import Union, Tuple, List
66
import pytest
77
from circuitpython_nrf24l01.rf24 import (
88
RF24,
@@ -54,8 +54,8 @@ class RadioState:
5454
}
5555

5656
# SPI commands
57-
rx_fifo = [] # command 0x61
58-
tx_fifo = [] # commands 0xA0, 0xB0, 0xA8 + pipe number
57+
rx_fifo: List[bytearray] = [] # command 0x61
58+
tx_fifo: List[bytearray] = [] # commands 0xA0, 0xB0, 0xA8 + pipe number
5959
logger = logging.getLogger("nRF24L01")
6060

6161
def __init__(self):
@@ -80,6 +80,7 @@ def __init__(self):
8080
def read_payload(master_out_buf):
8181
"""Read payload from state machine's RX FIFO.
8282
For simplicity, this doesn't pop payloads from the FIFO."""
83+
status_byte = RadioState.registers[7][0] # copy current STATUS byte
8384
if RadioState.rx_fifo:
8485
RadioState.logger.debug(
8586
"Requesting %d bytes from the RX FIFO", len(master_out_buf)
@@ -107,21 +108,22 @@ def read_payload(master_out_buf):
107108
if len(RadioState.rx_fifo) >= 3:
108109
RadioState.registers[0x17][0] |= 2
109110
return (
110-
RadioState.registers[7]
111+
bytearray([status_byte])
111112
+ ret_val[:size]
112113
+ bytearray(b"." * (len(master_out_buf) - size))
113114
)
114-
return RadioState.registers[7] + (b"." * len(master_out_buf))
115+
return bytearray([status_byte]) + (b"." * len(master_out_buf))
115116

116117
@staticmethod
117118
def write_payload(payload):
118119
"""Write payload to state machine's TX FIFO."""
120+
status_byte = RadioState.registers[7][0] # copy current STATUS byte
119121
RadioState.tx_fifo.append(payload)
120122
RadioState.registers[0x17][0] &= 0xCF
121123
if len(RadioState.tx_fifo) >= 3:
122124
RadioState.registers[0x17][0] |= 0x20
123125
RadioState.registers[7][0] |= 1
124-
return RadioState.registers[7] + (b"\0" * len(payload))
126+
return bytearray([status_byte]) + (b"\0" * len(payload))
125127

126128
@staticmethod
127129
def get_pl_width(*_):
@@ -138,20 +140,22 @@ def non_op(*_):
138140
@staticmethod
139141
def flush_tx(*_):
140142
"""Flush the state machine's TX FIFO."""
143+
status_byte = RadioState.registers[7][0] # copy current STATUS byte
141144
RadioState.tx_fifo.clear()
142145
RadioState.registers[7][0] &= 0xFE
143146
RadioState.registers[0x17][0] &= 0xCF
144147
RadioState.registers[0x17][0] |= 0x10
145-
return RadioState.registers[7]
148+
return bytearray([status_byte])
146149

147150
@staticmethod
148151
def flush_rx(*_):
149152
"""Flush the state machine's RX FIFO."""
153+
status_byte = RadioState.registers[7][0] # copy current STATUS byte
150154
RadioState.rx_fifo.clear()
151155
RadioState.registers[7][0] |= 0x0E
152156
RadioState.registers[0x17][0] &= 0xF0
153157
RadioState.registers[0x17][0] |= 1
154-
return RadioState.registers[7]
158+
return bytearray([status_byte])
155159

156160
def __getitem__(self, __offset: int) -> bytearray:
157161
register = __offset & 0x1F
@@ -212,15 +216,17 @@ def xfer2(self, out_buf: Union[bytes, bytearray], baud_rate: int) -> bytearray:
212216
"""A mock function for a full duplex SPI transaction."""
213217
register = out_buf[0]
214218
assert baud_rate
219+
# copy STATUS register before outcome alters it
220+
status_byte = self.state.registers[7]
215221
try: # assume `register` is a write operation
216222
self.state[register] = bytearray(out_buf[1:])
217-
return self.state.registers[7]
223+
return status_byte
218224
except RegisterReadOnly: # `register` is a SPI read operation
219225
return self.state[register]
220226
except RegisterError: # `register` is a SPI command
221227
if register in self.state.commands:
222228
return self.state.commands[register](out_buf[1:])
223-
return self.state.registers[7]
229+
return status_byte
224230

225231

226232
class ShimDigitalIO:

tests/test_rf24.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_context(rf24_obj: RF24, ble_obj: FakeBLE):
2626

2727

2828
@pytest.mark.parametrize("value", [True, False])
29-
def test_ce(rf24_obj: RF24, value: False):
29+
def test_ce(rf24_obj: RF24, value: bool):
3030
"""test ce_pin attribute."""
3131
rf24_obj.ce_pin = value
3232
assert rf24_obj.ce_pin == value
@@ -156,7 +156,7 @@ def test_dyn_pl_attr(rf24_obj: RF24):
156156
"""test dynamic_payloads attribute (using list of integers)."""
157157
enable = [1, -1, 0, 1]
158158
previous = rf24_obj.dynamic_payloads & enable.index(-1)
159-
rf24_obj.dynamic_payloads = enable
159+
rf24_obj.dynamic_payloads = enable # type: ignore[assignment]
160160
for i, pipe in enumerate(enable):
161161
if pipe < 0:
162162
assert rf24_obj.get_dynamic_payloads(i) == previous
@@ -177,7 +177,7 @@ def test_payload_length_attr(rf24_obj: RF24):
177177
"""test payload_length attribute (using list of integers)."""
178178
enable = [1, -1, 20, 0]
179179
previous = rf24_obj.payload_length
180-
rf24_obj.payload_length = enable
180+
rf24_obj.payload_length = enable # type: ignore[assignment]
181181
for i, pipe in enumerate(enable):
182182
if pipe <= 0:
183183
assert rf24_obj.get_payload_length(i) == previous
@@ -198,7 +198,7 @@ def test_auto_ack_attr(rf24_obj: RF24):
198198
"""test auto_ack attribute (using list of integers)."""
199199
enable = [1, -1, 20, 0]
200200
previous = bool(rf24_obj.auto_ack & (enable.index(-1)))
201-
rf24_obj.auto_ack = enable
201+
rf24_obj.auto_ack = enable # type: ignore[assignment]
202202
for i, pipe in enumerate(enable):
203203
if pipe < 0:
204204
assert rf24_obj.get_auto_ack(i) == previous

0 commit comments

Comments
 (0)