Mock socket file for testing _PdbServer without actual socket connections.
| 35 | |
| 36 | |
| 37 | class MockSocketFile: |
| 38 | """Mock socket file for testing _PdbServer without actual socket connections.""" |
| 39 | |
| 40 | def __init__(self): |
| 41 | self.input_queue = [] |
| 42 | self.output_buffer = [] |
| 43 | |
| 44 | def write(self, data: bytes) -> None: |
| 45 | """Simulate write to socket.""" |
| 46 | self.output_buffer.append(data) |
| 47 | |
| 48 | def flush(self) -> None: |
| 49 | """No-op flush implementation.""" |
| 50 | pass |
| 51 | |
| 52 | def readline(self) -> bytes: |
| 53 | """Read a line from the prepared input queue.""" |
| 54 | if not self.input_queue: |
| 55 | return b"" |
| 56 | return self.input_queue.pop(0) |
| 57 | |
| 58 | def close(self) -> None: |
| 59 | """Close the mock socket file.""" |
| 60 | pass |
| 61 | |
| 62 | def add_input(self, data: dict) -> None: |
| 63 | """Add input that will be returned by readline.""" |
| 64 | self.input_queue.append(json.dumps(data).encode() + b"\n") |
| 65 | |
| 66 | def get_output(self) -> List[dict]: |
| 67 | """Get the output that was written by the object being tested.""" |
| 68 | results = [] |
| 69 | for data in self.output_buffer: |
| 70 | if isinstance(data, bytes) and data.endswith(b"\n"): |
| 71 | try: |
| 72 | results.append(json.loads(data.decode().strip())) |
| 73 | except json.JSONDecodeError: |
| 74 | pass # Ignore non-JSON output |
| 75 | self.output_buffer = [] |
| 76 | return results |
| 77 | |
| 78 | |
| 79 | class PdbClientTestCase(unittest.TestCase): |