Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
import time
|
||||
from pygui.backend.frame import Frame
|
||||
from pygui.serialcomm.stream_reader import StreamReader
|
||||
|
||||
class FakeTransport:
|
||||
"""Yield canned line then blocks (returns b'')."""
|
||||
def __init__(self, lines): self._lines = list(lines); self.closed = False
|
||||
def readline(self):
|
||||
time.sleep(0.001)
|
||||
return self._lines.pop(0) if self._lines else b""
|
||||
def close(self): self.closed = True
|
||||
|
||||
def parse_line(raw: str, seq: int) -> Frame:
|
||||
parts = [float(x) for x in raw.split(",")]
|
||||
return Frame(seq = seq, time = parts[0], values = parts[1:])
|
||||
|
||||
def test_reads_frames_and_counts_errors():
|
||||
got, errors = [], []
|
||||
transport = FakeTransport([b"0.0,149,148\n", b"garbage\n", b"0.5,150,149\n"])
|
||||
r = StreamReader(transport, parse_line,
|
||||
on_frame = got.append, on_error = lambda e: errors.append(e))
|
||||
|
||||
r.start()
|
||||
time.sleep(0.1)
|
||||
r.stop()
|
||||
assert [f.values for f in got] == [[149.0, 148.0], [150.0, 149.0]]
|
||||
assert r.error_count == 1
|
||||
assert transport.closed
|
||||
Reference in New Issue
Block a user