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

1# Copyright (c) 2024 Prosemark Contributors 

2# This software is licensed under the MIT License 

3 

4"""Fake console adapter for testing output operations.""" 

5 

6from typing import TYPE_CHECKING 

7 

8from prosemark.ports.console_port import ConsolePort 

9 

10if TYPE_CHECKING: # pragma: no cover 

11 from prosemark.domain.models import Binder 

12 

13 

14class FakeConsolePort(ConsolePort): 

15 """In-memory fake implementation of ConsolePort for testing. 

16 

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. 

20 

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. 

24 

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 

35 

36 """ 

37 

38 def __init__(self) -> None: 

39 """Initialize empty fake console port.""" 

40 self._output: list[str] = [] 

41 self._tree_calls: list[Binder] = [] 

42 

43 def print(self, msg: str) -> None: 

44 """Store message in output buffer. 

45 

46 Args: 

47 msg: Message to add to the output buffer. 

48 

49 """ 

50 self._output.append(msg) 

51 

52 def print_tree(self, binder: 'Binder') -> None: 

53 """Store binder tree call for testing. 

54 

55 Args: 

56 binder: Binder object to store for verification in tests. 

57 

58 """ 

59 self._tree_calls.append(binder) 

60 

61 def get_output(self) -> list[str]: 

62 """Return list of all printed messages. 

63 

64 Returns: 

65 List of messages in the order they were printed. 

66 

67 """ 

68 return self._output.copy() 

69 

70 def last_output(self) -> str: 

71 """Return the last printed message. 

72 

73 Returns: 

74 The most recently printed message. 

75 

76 Raises: 

77 IndexError: If no messages have been printed. 

78 

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 

84 

85 def output_contains(self, text: str) -> bool: 

86 """Check if any output contains the given text. 

87 

88 Args: 

89 text: Text to search for in the output. 

90 

91 Returns: 

92 True if any message contains the given text. 

93 

94 """ 

95 return any(text in msg for msg in self._output) 

96 

97 def get_tree_calls(self) -> list['Binder']: 

98 """Return list of all print_tree calls. 

99 

100 Returns: 

101 List of Binder objects passed to print_tree in order. 

102 

103 """ 

104 return self._tree_calls.copy() 

105 

106 def tree_call_count(self) -> int: 

107 """Return the number of print_tree calls made. 

108 

109 Returns: 

110 Number of times print_tree was called. 

111 

112 """ 

113 return len(self._tree_calls)