Skip to content

Commit 3ee5666

Browse files
committed
Added unit test for ppretty().
1 parent 0325b5e commit 3ee5666

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

tests/test_cmd2.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,6 +3490,40 @@ def test_ppaged_terminal_restoration_oserror(outsim_app, monkeypatch) -> None:
34903490
assert not termios_mock.tcsetattr.called
34913491

34923492

3493+
def test_ppretty(base_app: cmd2.Cmd) -> None:
3494+
# Mock the Pretty class and the print_to() method
3495+
with mock.patch('cmd2.cmd2.Pretty') as mock_pretty, mock.patch.object(cmd2.Cmd, 'print_to') as mock_print_to:
3496+
# Set up the mock return value for Pretty
3497+
mock_pretty_obj = mock.Mock()
3498+
mock_pretty.return_value = mock_pretty_obj
3499+
3500+
test_obj = {"key": "value"}
3501+
3502+
# Call ppretty() with some custom arguments
3503+
base_app.ppretty(
3504+
test_obj,
3505+
indent_size=2,
3506+
max_depth=5,
3507+
expand_all=True,
3508+
)
3509+
3510+
# Verify Pretty was instantiated with the correct arguments
3511+
mock_pretty.assert_called_once_with(
3512+
test_obj,
3513+
indent_size=2,
3514+
indent_guides=True,
3515+
max_length=None,
3516+
max_string=None,
3517+
max_depth=5,
3518+
expand_all=True,
3519+
overflow="ignore",
3520+
)
3521+
3522+
# Verify print_to() was called with the mock pretty object and soft_wrap=True
3523+
# It should default to self.stdout when no file is provided
3524+
mock_print_to.assert_called_once_with(base_app.stdout, mock_pretty_obj, soft_wrap=True)
3525+
3526+
34933527
# we override cmd.parseline() so we always get consistent
34943528
# command parsing by parent methods we don't override
34953529
# don't need to test all the parsing logic here, because

0 commit comments

Comments
 (0)