summaryrefslogtreecommitdiff
path: root/tests/test_utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-12-09 16:24:54 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-12-09 16:24:54 -0500
commit6f16e671971a9fac4edfda9c0117ceaeb5e6487e (patch)
treeee3a78ca7427729ff9d93c2401f2e3c85bea59d6 /tests/test_utils.py
parentcda57dc1a1859408fb25d31178ad0f6e77ede902 (diff)
downloadcmd2-git-6f16e671971a9fac4edfda9c0117ceaeb5e6487e.tar.gz
Adding unit tests for text alignment functions
Diffstat (limited to 'tests/test_utils.py')
-rw-r--r--tests/test_utils.py168
1 files changed, 152 insertions, 16 deletions
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