Coverage for src/prosemark/adapters/fake_console.py: 100%
19 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-09-24 18:08 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-09-24 18:08 +0000
1# Copyright (c) 2024 Prosemark Contributors
2# This software is licensed under the MIT License
4"""Fake console adapter for testing output operations."""
6from typing import TYPE_CHECKING
8from prosemark.ports.console_port import ConsolePort
10if TYPE_CHECKING: # pragma: no cover
11 from prosemark.domain.models import Binder
14class FakeConsolePort(ConsolePort):
15 """In-memory fake implementation of ConsolePort for testing.
17 Provides minimal console output functionality by collecting messages
18 in memory instead of displaying them. Includes test helper methods
19 for asserting console output in tests.
21 This fake stores all printed messages in order and provides methods
22 to inspect the output for test assertions without exposing internal
23 implementation details.
25 Examples:
26 >>> console = FakeConsolePort()
27 >>> console.print('Hello, world!')
28 >>> console.print('Goodbye!')
29 >>> console.get_output()
30 ['Hello, world!', 'Goodbye!']
31 >>> console.last_output()
32 'Goodbye!'
33 >>> console.output_contains('Hello')
34 True
36 """
38 def __init__(self) -> None:
39 """Initialize empty fake console port."""
40 self._output: list[str] = []
41 self._tree_calls: list[Binder] = []
43 def print(self, msg: str) -> None:
44 """Store message in output buffer.
46 Args:
47 msg: Message to add to the output buffer.
49 """
50 self._output.append(msg)
52 def print_tree(self, binder: 'Binder') -> None:
53 """Store binder tree call for testing.
55 Args:
56 binder: Binder object to store for verification in tests.
58 """
59 self._tree_calls.append(binder)
61 def get_output(self) -> list[str]:
62 """Return list of all printed messages.
64 Returns:
65 List of messages in the order they were printed.
67 """
68 return self._output.copy()
70 def last_output(self) -> str:
71 """Return the last printed message.
73 Returns:
74 The most recently printed message.
76 Raises:
77 IndexError: If no messages have been printed.
79 """
80 if not self._output: # pragma: no cover
81 msg = 'No output has been printed'
82 raise IndexError(msg) # pragma: no cover
83 return self._output[-1] # pragma: no cover
85 def output_contains(self, text: str) -> bool:
86 """Check if any output contains the given text.
88 Args:
89 text: Text to search for in the output.
91 Returns:
92 True if any message contains the given text.
94 """
95 return any(text in msg for msg in self._output)
97 def get_tree_calls(self) -> list['Binder']:
98 """Return list of all print_tree calls.
100 Returns:
101 List of Binder objects passed to print_tree in order.
103 """
104 return self._tree_calls.copy()
106 def tree_call_count(self) -> int:
107 """Return the number of print_tree calls made.
109 Returns:
110 Number of times print_tree was called.
112 """
113 return len(self._tree_calls)