From cda57dc1a1859408fb25d31178ad0f6e77ede902 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 9 Dec 2019 15:23:58 -0500 Subject: Updated center_text to support ansi escape sequences and characters with display widths greater than 1. Also added left and right justification functions. --- tests/test_utils.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index e4b9169c..2c43371f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -293,24 +293,21 @@ def test_context_flag_exit_err(context_flag): context_flag.__exit__() -def test_center_text_pad_none(): +def test_center_text_pad_equals(): msg = 'foo' - centered = cu.center_text(msg, pad=None) - expected_center = ' ' + msg + ' ' - assert expected_center in centered + fill_char = '=' + centered = cu.center_text(msg, fill_char=fill_char) + assert msg in centered + assert centered.startswith(fill_char) + assert centered.endswith(fill_char) letters_in_centered = set(centered) letters_in_msg = set(msg) assert len(letters_in_centered) == len(letters_in_msg) + 1 -def test_center_text_pad_equals(): + +def test_center_text_pad_blank(): msg = 'foo' - pad = '=' - centered = cu.center_text(msg, pad=pad) - expected_center = ' ' + msg + ' ' - assert expected_center in centered - assert centered.startswith(pad) - assert centered.endswith(pad) - letters_in_centered = set(centered) - letters_in_msg = set(msg) - assert len(letters_in_centered) == len(letters_in_msg) + 2 + fill_char = '' + with pytest.raises(ValueError): + cu.center_text(msg, fill_char=fill_char) -- cgit v1.2.1 From 6f16e671971a9fac4edfda9c0117ceaeb5e6487e Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 9 Dec 2019 16:24:54 -0500 Subject: Adding unit tests for text alignment functions --- tests/test_utils.py | 168 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 152 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 2c43371f..1327064f 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -293,21 +293,157 @@ def test_context_flag_exit_err(context_flag): context_flag.__exit__() -def test_center_text_pad_equals(): - msg = 'foo' - fill_char = '=' - centered = cu.center_text(msg, fill_char=fill_char) - assert msg in centered - assert centered.startswith(fill_char) - assert centered.endswith(fill_char) - letters_in_centered = set(centered) - letters_in_msg = set(msg) - assert len(letters_in_centered) == len(letters_in_msg) + 1 - - -def test_center_text_pad_blank(): - msg = 'foo' - fill_char = '' +def test_align_text_fill_char_is_tab(): + text = 'foo' + fill_char = '\t' + width = 5 + aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) + assert aligned == text + ' ' + +def test_align_text_fill_char_is_too_long(): + text = 'foo' + fill_char = 'fill' + width = 5 + with pytest.raises(TypeError): + cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) +def test_align_text_fill_char_is_unprintable(): + text = 'foo' + fill_char = '\n' + width = 5 with pytest.raises(ValueError): - cu.center_text(msg, fill_char=fill_char) + cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) + +def test_align_text_has_tabs(): + text = '\t\tfoo' + fill_char = '-' + width = 10 + aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT, tab_width=2) + assert aligned == ' ' + 'foo' + '---' + +def test_align_text_blank(): + text = '' + fill_char = '-' + width = 5 + aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) + assert aligned == text + fill_char * width + +def test_align_text_wider_than_width(): + text = 'long' + fill_char = '-' + width = 3 + aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) + assert aligned == text + +def test_align_text_term_width(): + import shutil + from cmd2 import ansi + text = 'foo' + fill_char = ' ' + + term_width = shutil.get_terminal_size().columns + expected_fill = (term_width - ansi.ansi_safe_wcswidth(text)) * fill_char + + aligned = cu.align_text(text, fill_char=fill_char, alignment=cu.TextAlignment.LEFT) + assert aligned == text + expected_fill + +def test_left_text(): + text = 'foo' + fill_char = '-' + width = 5 + aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + assert aligned == text + fill_char + fill_char + +def test_left_text_multiline(): + text = "foo\nshoes" + fill_char = '-' + width = 7 + aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + assert aligned == ('foo----\n' + 'shoes--') + +def test_left_text_asian_fill(): + """Test fill_char with display width greater than 1""" + text = 'foo' + fill_char = '苹' + width = 5 + aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + assert aligned == text + fill_char + +def test_left_text_asian_fill_needs_padding(): + """Test when fill_char's display width does not divide evenly into gap""" + text = 'foo' + fill_char = '苹' + width = 6 + aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + assert aligned == text + fill_char + ' ' + +def test_center_text(): + text = 'foo' + fill_char = '-' + width = 5 + aligned = cu.center_text(text,fill_char=fill_char, width=width) + assert aligned == fill_char + text + fill_char + +def test_center_text_multiline(): + text = "foo\nshoes" + fill_char = '-' + width = 7 + aligned = cu.center_text(text, fill_char=fill_char, width=width) + assert aligned == ('--foo--\n' + '-shoes-') + +def test_center_text_asian_fill(): + """Test fill_char with display width greater than 1""" + text = 'foo' + fill_char = '苹' + width = 7 + aligned = cu.center_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + text + fill_char + +def test_center_text_asian_fill_needs_right_padding(): + """Test when fill_char's display width does not divide evenly into right gap""" + text = 'foo' + fill_char = '苹' + width = 8 + aligned = cu.center_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + text + fill_char + ' ' + +def test_center_text_asian_fill_needs_left_and_right_padding(): + """Test when fill_char's display width does not divide evenly into either gap""" + text = 'foo' + fill_char = '苹' + width = 9 + aligned = cu.center_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + ' ' + text + fill_char + ' ' + +def test_right_text(): + text = 'foo' + fill_char = '-' + width = 5 + aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + fill_char + text + +def test_right_text_multiline(): + text = "foo\nshoes" + fill_char = '-' + width = 7 + aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + assert aligned == ('----foo\n' + '--shoes') + +def test_right_text_asian_fill(): + """Test fill_char with display width greater than 1""" + text = 'foo' + fill_char = '苹' + width = 5 + aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + text + +def test_right_text_asian_fill_needs_padding(): + """Test when fill_char's display width does not divide evenly into gap""" + text = 'foo' + fill_char = '苹' + width = 6 + aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + ' ' + text -- cgit v1.2.1 From 5ffed1387ddc4e5725f9c6b5b632a611eca1e3ca Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 9 Dec 2019 16:34:07 -0500 Subject: Added more text alignment unit tests --- tests/test_utils.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 1327064f..745e5ff4 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -326,7 +326,7 @@ def test_align_text_blank(): fill_char = '-' width = 5 aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) - assert aligned == text + fill_char * width + assert aligned == fill_char * width def test_align_text_wider_than_width(): text = 'long' @@ -335,6 +335,13 @@ def test_align_text_wider_than_width(): aligned = cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) assert aligned == text +def test_align_text_has_unprintable(): + text = 'foo\x02' + fill_char = '-' + width = 5 + with pytest.raises(ValueError): + cu.align_text(text, fill_char=fill_char, width=width, alignment=cu.TextAlignment.LEFT) + def test_align_text_term_width(): import shutil from cmd2 import ansi -- cgit v1.2.1 From 4c3cd737d780819f02664f4d49edbebab08ee8a8 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 9 Dec 2019 17:35:44 -0500 Subject: Added more unit tests for text alignment --- tests/test_utils.py | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 745e5ff4..233b3b70 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -369,15 +369,21 @@ def test_left_text_multiline(): assert aligned == ('foo----\n' 'shoes--') -def test_left_text_asian_fill(): - """Test fill_char with display width greater than 1""" +def test_left_wide_display_text(): + text = '苹' + fill_char = '-' + width = 4 + aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + assert aligned == text + fill_char + fill_char + +def test_left_text_wide_display_fill(): text = 'foo' fill_char = '苹' width = 5 aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) assert aligned == text + fill_char -def test_left_text_asian_fill_needs_padding(): +def test_left_text_wide_display_fill_needs_padding(): """Test when fill_char's display width does not divide evenly into gap""" text = 'foo' fill_char = '苹' @@ -400,15 +406,21 @@ def test_center_text_multiline(): assert aligned == ('--foo--\n' '-shoes-') -def test_center_text_asian_fill(): - """Test fill_char with display width greater than 1""" +def test_center_wide_display_text(): + text = '苹' + fill_char = '-' + width = 4 + aligned = cu.center_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + text + fill_char + +def test_center_text_wide_display_fill(): text = 'foo' fill_char = '苹' width = 7 aligned = cu.center_text(text, fill_char=fill_char, width=width) assert aligned == fill_char + text + fill_char -def test_center_text_asian_fill_needs_right_padding(): +def test_center_text_wide_display_fill_needs_right_padding(): """Test when fill_char's display width does not divide evenly into right gap""" text = 'foo' fill_char = '苹' @@ -416,7 +428,7 @@ def test_center_text_asian_fill_needs_right_padding(): aligned = cu.center_text(text, fill_char=fill_char, width=width) assert aligned == fill_char + text + fill_char + ' ' -def test_center_text_asian_fill_needs_left_and_right_padding(): +def test_center_text_wide_display_fill_needs_left_and_right_padding(): """Test when fill_char's display width does not divide evenly into either gap""" text = 'foo' fill_char = '苹' @@ -439,15 +451,21 @@ def test_right_text_multiline(): assert aligned == ('----foo\n' '--shoes') -def test_right_text_asian_fill(): - """Test fill_char with display width greater than 1""" +def test_right_wide_display_text(): + text = '苹' + fill_char = '-' + width = 4 + aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + assert aligned == fill_char + fill_char + text + +def test_right_text_wide_display_fill(): text = 'foo' fill_char = '苹' width = 5 aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) assert aligned == fill_char + text -def test_right_text_asian_fill_needs_padding(): +def test_right_text_wide_display_fill_needs_padding(): """Test when fill_char's display width does not divide evenly into gap""" text = 'foo' fill_char = '苹' -- cgit v1.2.1 From 9e747677a803210ad5ef5c1d5fdf01ea9c1a5ee9 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 9 Dec 2019 20:53:37 -0500 Subject: Renamed functions based on code review comments. Fixed Python warnings. --- tests/test_utils.py | 64 ++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index 233b3b70..27b0e3bb 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -354,121 +354,121 @@ def test_align_text_term_width(): aligned = cu.align_text(text, fill_char=fill_char, alignment=cu.TextAlignment.LEFT) assert aligned == text + expected_fill -def test_left_text(): +def test_align_left(): text = 'foo' fill_char = '-' width = 5 - aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_left(text, fill_char=fill_char, width=width) assert aligned == text + fill_char + fill_char -def test_left_text_multiline(): +def test_align_left_multiline(): text = "foo\nshoes" fill_char = '-' width = 7 - aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_left(text, fill_char=fill_char, width=width) assert aligned == ('foo----\n' 'shoes--') -def test_left_wide_display_text(): +def test_align_left_wide_text(): text = '苹' fill_char = '-' width = 4 - aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_left(text, fill_char=fill_char, width=width) assert aligned == text + fill_char + fill_char -def test_left_text_wide_display_fill(): +def test_align_left_wide_fill(): text = 'foo' fill_char = '苹' width = 5 - aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_left(text, fill_char=fill_char, width=width) assert aligned == text + fill_char -def test_left_text_wide_display_fill_needs_padding(): +def test_align_left_wide_fill_needs_padding(): """Test when fill_char's display width does not divide evenly into gap""" text = 'foo' fill_char = '苹' width = 6 - aligned = cu.ljustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_left(text, fill_char=fill_char, width=width) assert aligned == text + fill_char + ' ' -def test_center_text(): +def test_align_center(): text = 'foo' fill_char = '-' width = 5 - aligned = cu.center_text(text,fill_char=fill_char, width=width) + aligned = cu.align_center(text, fill_char=fill_char, width=width) assert aligned == fill_char + text + fill_char -def test_center_text_multiline(): +def test_align_center_multiline(): text = "foo\nshoes" fill_char = '-' width = 7 - aligned = cu.center_text(text, fill_char=fill_char, width=width) + aligned = cu.align_center(text, fill_char=fill_char, width=width) assert aligned == ('--foo--\n' '-shoes-') -def test_center_wide_display_text(): +def test_align_center_wide_text(): text = '苹' fill_char = '-' width = 4 - aligned = cu.center_text(text, fill_char=fill_char, width=width) + aligned = cu.align_center(text, fill_char=fill_char, width=width) assert aligned == fill_char + text + fill_char -def test_center_text_wide_display_fill(): +def test_align_center_wide_fill(): text = 'foo' fill_char = '苹' width = 7 - aligned = cu.center_text(text, fill_char=fill_char, width=width) + aligned = cu.align_center(text, fill_char=fill_char, width=width) assert aligned == fill_char + text + fill_char -def test_center_text_wide_display_fill_needs_right_padding(): +def test_align_center_wide_fill_needs_right_padding(): """Test when fill_char's display width does not divide evenly into right gap""" text = 'foo' fill_char = '苹' width = 8 - aligned = cu.center_text(text, fill_char=fill_char, width=width) + aligned = cu.align_center(text, fill_char=fill_char, width=width) assert aligned == fill_char + text + fill_char + ' ' -def test_center_text_wide_display_fill_needs_left_and_right_padding(): +def test_align_center_wide_fill_needs_left_and_right_padding(): """Test when fill_char's display width does not divide evenly into either gap""" text = 'foo' fill_char = '苹' width = 9 - aligned = cu.center_text(text, fill_char=fill_char, width=width) + aligned = cu.align_center(text, fill_char=fill_char, width=width) assert aligned == fill_char + ' ' + text + fill_char + ' ' -def test_right_text(): +def test_align_right(): text = 'foo' fill_char = '-' width = 5 - aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_right(text, fill_char=fill_char, width=width) assert aligned == fill_char + fill_char + text -def test_right_text_multiline(): +def test_align_right_multiline(): text = "foo\nshoes" fill_char = '-' width = 7 - aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_right(text, fill_char=fill_char, width=width) assert aligned == ('----foo\n' '--shoes') -def test_right_wide_display_text(): +def test_align_right_wide_text(): text = '苹' fill_char = '-' width = 4 - aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_right(text, fill_char=fill_char, width=width) assert aligned == fill_char + fill_char + text -def test_right_text_wide_display_fill(): +def test_align_right_wide_fill(): text = 'foo' fill_char = '苹' width = 5 - aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_right(text, fill_char=fill_char, width=width) assert aligned == fill_char + text -def test_right_text_wide_display_fill_needs_padding(): +def test_align_right_wide_fill_needs_padding(): """Test when fill_char's display width does not divide evenly into gap""" text = 'foo' fill_char = '苹' width = 6 - aligned = cu.rjustify_text(text, fill_char=fill_char, width=width) + aligned = cu.align_right(text, fill_char=fill_char, width=width) assert aligned == fill_char + ' ' + text -- cgit v1.2.1