repl-toolkit 1.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,232 @@
1
+ """
2
+ Tests for formatting utilities.
3
+ """
4
+
5
+ import pytest
6
+ from prompt_toolkit.formatted_text import ANSI, HTML
7
+
8
+ from repl_toolkit.formatting import (
9
+ auto_format,
10
+ create_auto_printer,
11
+ detect_format_type,
12
+ print_auto_formatted,
13
+ )
14
+
15
+
16
+ class TestDetectFormatType:
17
+ """Tests for detect_format_type function."""
18
+
19
+ def test_detect_html(self):
20
+ """Test HTML tag detection."""
21
+ assert detect_format_type("<b>Bold</b>") == "html"
22
+ assert detect_format_type("<darkcyan>Colored</darkcyan>") == "html"
23
+ assert detect_format_type("<b><darkcyan>Nested</darkcyan></b>") == "html"
24
+ assert detect_format_type("<i>Italic</i>") == "html"
25
+ assert detect_format_type("<u>Underline</u>") == "html"
26
+
27
+ def test_detect_html_uppercase(self):
28
+ """Test HTML detection with uppercase tags."""
29
+ assert detect_format_type("<B>Bold</B>") == "html"
30
+ assert detect_format_type("<TAG>content</TAG>") == "html"
31
+
32
+ def test_detect_html_self_closing(self):
33
+ """Test HTML self-closing tag detection."""
34
+ assert detect_format_type("<br/>") == "html"
35
+ assert detect_format_type("<tag />") == "html"
36
+
37
+ def test_detect_ansi(self):
38
+ """Test ANSI escape code detection."""
39
+ assert detect_format_type("\x1b[1mBold\x1b[0m") == "ansi"
40
+ assert detect_format_type("\x1b[31mRed\x1b[0m") == "ansi"
41
+ assert detect_format_type("\x1b[1;32mBold Green\x1b[0m") == "ansi"
42
+ assert detect_format_type("\033[1mBold\033[0m") == "ansi"
43
+
44
+ def test_detect_plain(self):
45
+ """Test plain text detection."""
46
+ assert detect_format_type("Plain text") == "plain"
47
+ assert detect_format_type("No formatting here") == "plain"
48
+ assert detect_format_type("") == "plain"
49
+
50
+ def test_edge_cases(self):
51
+ """Test edge cases that should not be detected as HTML."""
52
+ # Comparison operators
53
+ assert detect_format_type("a < b and c > d") == "plain"
54
+
55
+ # Numbers in tag names (invalid HTML)
56
+ assert detect_format_type("<123>content</123>") == "plain"
57
+
58
+ # Hyphens in tag names (invalid HTML)
59
+ assert detect_format_type("<a-b>content</a-b>") == "plain"
60
+
61
+ # Underscore start (invalid HTML)
62
+ assert detect_format_type("<_tag>content</_tag>") == "plain"
63
+
64
+ # Angle brackets but not tags
65
+ assert detect_format_type("Text with <angle brackets> but not tags") == "plain"
66
+
67
+ def test_mixed_content(self):
68
+ """Test that ANSI takes precedence over HTML."""
69
+ # If text has both ANSI and HTML-like content, ANSI is detected first
70
+ mixed = "\x1b[1m<b>Bold</b>\x1b[0m"
71
+ assert detect_format_type(mixed) == "ansi"
72
+
73
+
74
+ class TestAutoFormat:
75
+ """Tests for auto_format function."""
76
+
77
+ def test_auto_format_html(self):
78
+ """Test auto-formatting HTML text."""
79
+ result = auto_format("<b>Bold</b>")
80
+ assert isinstance(result, HTML)
81
+ # HTML objects have the text accessible via their value attribute
82
+ assert result.value == "<b>Bold</b>"
83
+
84
+ def test_auto_format_ansi(self):
85
+ """Test auto-formatting ANSI text."""
86
+ result = auto_format("\x1b[1mBold\x1b[0m")
87
+ assert isinstance(result, ANSI)
88
+ # ANSI objects have the text accessible via their value attribute
89
+ assert result.value == "\x1b[1mBold\x1b[0m"
90
+
91
+ def test_auto_format_plain(self):
92
+ """Test auto-formatting plain text."""
93
+ result = auto_format("Plain text")
94
+ assert isinstance(result, str)
95
+ assert result == "Plain text"
96
+
97
+ def test_auto_format_empty(self):
98
+ """Test auto-formatting empty string."""
99
+ result = auto_format("")
100
+ assert isinstance(result, str)
101
+ assert result == ""
102
+
103
+
104
+ class TestPrintAutoFormatted:
105
+ """Tests for print_auto_formatted function."""
106
+
107
+ def test_print_html(self):
108
+ """Test printing HTML formatted text."""
109
+ # Just verify it doesn't raise an exception
110
+ # We can't easily test the actual output in pytest
111
+ try:
112
+ print_auto_formatted("<b>Bold</b>")
113
+ except Exception as e:
114
+ pytest.fail(f"print_auto_formatted raised {e}")
115
+
116
+ def test_print_plain(self):
117
+ """Test printing plain text."""
118
+ try:
119
+ print_auto_formatted("Plain text")
120
+ except Exception as e:
121
+ pytest.fail(f"print_auto_formatted raised {e}")
122
+
123
+ def test_print_with_kwargs(self):
124
+ """Test printing with additional kwargs."""
125
+ try:
126
+ print_auto_formatted("Test", end="", flush=True)
127
+ except Exception as e:
128
+ pytest.fail(f"print_auto_formatted raised {e}")
129
+
130
+
131
+ class TestCreateAutoPrinter:
132
+ """Tests for create_auto_printer function."""
133
+
134
+ def test_create_printer(self):
135
+ """Test creating an auto printer."""
136
+ printer = create_auto_printer()
137
+ assert callable(printer)
138
+
139
+ def test_printer_html(self):
140
+ """Test printer with HTML text."""
141
+ printer = create_auto_printer()
142
+ try:
143
+ printer("<b>Bold</b>")
144
+ except Exception as e:
145
+ pytest.fail(f"printer raised {e}")
146
+
147
+ def test_printer_plain(self):
148
+ """Test printer with plain text."""
149
+ printer = create_auto_printer()
150
+ try:
151
+ printer("Plain text")
152
+ except Exception as e:
153
+ pytest.fail(f"printer raised {e}")
154
+
155
+ def test_printer_with_kwargs(self):
156
+ """Test printer with kwargs."""
157
+ printer = create_auto_printer()
158
+ try:
159
+ printer("Test", end="", flush=True)
160
+ except Exception as e:
161
+ pytest.fail(f"printer raised {e}")
162
+
163
+ def test_printer_multiple_calls(self):
164
+ """Test printer with multiple calls."""
165
+ printer = create_auto_printer()
166
+ try:
167
+ printer("<b>Prefix:</b> ", end="", flush=True)
168
+ printer("Hello", end="", flush=True)
169
+ printer(" world\n")
170
+ except Exception as e:
171
+ pytest.fail(f"printer raised {e}")
172
+
173
+
174
+ class TestIntegration:
175
+ """Integration tests for formatting utilities."""
176
+
177
+ def test_callback_handler_simulation(self):
178
+ """Test simulating a callback handler usage."""
179
+ # Simulate ConfigurableCallbackHandler behavior
180
+ printer = create_auto_printer()
181
+ response_prefix = "<b><darkcyan>🤖 Assistant:</darkcyan></b> "
182
+
183
+ try:
184
+ # Print prefix
185
+ printer(response_prefix, end="", flush=True)
186
+
187
+ # Print message parts
188
+ printer("Hello", end="", flush=True)
189
+ printer(" world", end="", flush=True)
190
+ printer("\n")
191
+ except Exception as e:
192
+ pytest.fail(f"callback simulation raised {e}")
193
+
194
+ def test_different_format_types(self):
195
+ """Test handling different format types in sequence."""
196
+ printer = create_auto_printer()
197
+
198
+ try:
199
+ # HTML
200
+ printer("<b>HTML</b>")
201
+
202
+ # ANSI
203
+ printer("\x1b[1mANSI\x1b[0m")
204
+
205
+ # Plain
206
+ printer("Plain")
207
+ except Exception as e:
208
+ pytest.fail(f"different format types raised {e}")
209
+
210
+
211
+ class TestDocstrings:
212
+ """Test that docstrings are present and informative."""
213
+
214
+ def test_detect_format_type_docstring(self):
215
+ """Test detect_format_type has docstring."""
216
+ assert detect_format_type.__doc__ is not None
217
+ assert "Detect the format type" in detect_format_type.__doc__
218
+
219
+ def test_auto_format_docstring(self):
220
+ """Test auto_format has docstring."""
221
+ assert auto_format.__doc__ is not None
222
+ assert "Auto-detect format type" in auto_format.__doc__
223
+
224
+ def test_print_auto_formatted_docstring(self):
225
+ """Test print_auto_formatted has docstring."""
226
+ assert print_auto_formatted.__doc__ is not None
227
+ assert "Print text with auto-detected formatting" in print_auto_formatted.__doc__
228
+
229
+ def test_create_auto_printer_docstring(self):
230
+ """Test create_auto_printer has docstring."""
231
+ assert create_auto_printer.__doc__ is not None
232
+ assert "Create a printer function" in create_auto_printer.__doc__