Test get_completions with matches.
(self, mock_cmd_app: MockCmd, monkeypatch)
| 264 | |
| 265 | class TestCmd2Completer: |
| 266 | def test_get_completions(self, mock_cmd_app: MockCmd, monkeypatch) -> None: |
| 267 | """Test get_completions with matches.""" |
| 268 | mock_print = Mock() |
| 269 | monkeypatch.setattr(pt_utils, "print_formatted_text", mock_print) |
| 270 | |
| 271 | completer = pt_utils.Cmd2Completer(cast(Any, mock_cmd_app)) |
| 272 | |
| 273 | # Set up document |
| 274 | line = "" |
| 275 | document = Document(line, cursor_position=0) |
| 276 | |
| 277 | # Test plain and styled values for display and display_meta |
| 278 | foo_text = "foo" |
| 279 | foo_display = "Foo Display" |
| 280 | foo_meta = "Foo Meta" |
| 281 | |
| 282 | bar_text = "bar" |
| 283 | bar_display = stylize("Bar Display", Cmd2Style.SUCCESS) |
| 284 | bar_meta = stylize("Bar Meta", Cmd2Style.WARNING) |
| 285 | |
| 286 | # Set up matches |
| 287 | completion_items = [ |
| 288 | cmd2.CompletionItem(foo_text, display=foo_display, display_meta=foo_meta), |
| 289 | cmd2.CompletionItem(bar_text, display=bar_display, display_meta=bar_meta), |
| 290 | ] |
| 291 | |
| 292 | table = Table("Table Header") |
| 293 | table.add_row("Table Data") |
| 294 | cmd2_completions = cmd2.Completions(completion_items, table=table) |
| 295 | mock_cmd_app.complete.return_value = cmd2_completions |
| 296 | |
| 297 | # Call get_completions |
| 298 | completions = list(completer.get_completions(document, None)) |
| 299 | |
| 300 | assert len(completions) == len(cmd2_completions) |
| 301 | |
| 302 | assert completions[0].text == bar_text |
| 303 | assert to_formatted_text(completions[0].display) == to_formatted_text(ANSI(bar_display)) |
| 304 | assert to_formatted_text(completions[0].display_meta) == to_formatted_text(ANSI(bar_meta)) |
| 305 | |
| 306 | assert completions[1].text == foo_text |
| 307 | assert to_formatted_text(completions[1].display) == to_formatted_text(ANSI(foo_display)) |
| 308 | assert to_formatted_text(completions[1].display_meta) == to_formatted_text(ANSI(foo_meta)) |
| 309 | |
| 310 | # Verify that only the completion table printed |
| 311 | assert mock_print.call_count == 1 |
| 312 | args, _ = mock_print.call_args |
| 313 | assert "Table Header" in str(args[0]) |
| 314 | assert "Table Data" in str(args[0]) |
| 315 | |
| 316 | def test_get_completions_no_matches(self, mock_cmd_app: MockCmd, monkeypatch) -> None: |
| 317 | """Test get_completions with no matches.""" |
nothing calls this directly
no test coverage detected