@@ -293,21 +293,157 @@ def test_context_flag_exit_err(context_flag):
293293 context_flag .__exit__ ()
294294
295295
296- def test_center_text_pad_equals ():
297- msg = 'foo'
298- fill_char = '='
299- centered = cu .center_text (msg , fill_char = fill_char )
300- assert msg in centered
301- assert centered .startswith (fill_char )
302- assert centered .endswith (fill_char )
303- letters_in_centered = set (centered )
304- letters_in_msg = set (msg )
305- assert len (letters_in_centered ) == len (letters_in_msg ) + 1
306-
307-
308- def test_center_text_pad_blank ():
309- msg = 'foo'
310- fill_char = ''
296+ def test_align_text_fill_char_is_tab ():
297+ text = 'foo'
298+ fill_char = '\t '
299+ width = 5
300+ aligned = cu .align_text (text , fill_char = fill_char , width = width , alignment = cu .TextAlignment .LEFT )
301+ assert aligned == text + ' '
302+
303+ def test_align_text_fill_char_is_too_long ():
304+ text = 'foo'
305+ fill_char = 'fill'
306+ width = 5
307+ with pytest .raises (TypeError ):
308+ cu .align_text (text , fill_char = fill_char , width = width , alignment = cu .TextAlignment .LEFT )
311309
310+ def test_align_text_fill_char_is_unprintable ():
311+ text = 'foo'
312+ fill_char = '\n '
313+ width = 5
312314 with pytest .raises (ValueError ):
313- cu .center_text (msg , fill_char = fill_char )
315+ cu .align_text (text , fill_char = fill_char , width = width , alignment = cu .TextAlignment .LEFT )
316+
317+ def test_align_text_has_tabs ():
318+ text = '\t \t foo'
319+ fill_char = '-'
320+ width = 10
321+ aligned = cu .align_text (text , fill_char = fill_char , width = width , alignment = cu .TextAlignment .LEFT , tab_width = 2 )
322+ assert aligned == ' ' + 'foo' + '---'
323+
324+ def test_align_text_blank ():
325+ text = ''
326+ fill_char = '-'
327+ width = 5
328+ aligned = cu .align_text (text , fill_char = fill_char , width = width , alignment = cu .TextAlignment .LEFT )
329+ assert aligned == text + fill_char * width
330+
331+ def test_align_text_wider_than_width ():
332+ text = 'long'
333+ fill_char = '-'
334+ width = 3
335+ aligned = cu .align_text (text , fill_char = fill_char , width = width , alignment = cu .TextAlignment .LEFT )
336+ assert aligned == text
337+
338+ def test_align_text_term_width ():
339+ import shutil
340+ from cmd2 import ansi
341+ text = 'foo'
342+ fill_char = ' '
343+
344+ term_width = shutil .get_terminal_size ().columns
345+ expected_fill = (term_width - ansi .ansi_safe_wcswidth (text )) * fill_char
346+
347+ aligned = cu .align_text (text , fill_char = fill_char , alignment = cu .TextAlignment .LEFT )
348+ assert aligned == text + expected_fill
349+
350+ def test_left_text ():
351+ text = 'foo'
352+ fill_char = '-'
353+ width = 5
354+ aligned = cu .ljustify_text (text , fill_char = fill_char , width = width )
355+ assert aligned == text + fill_char + fill_char
356+
357+ def test_left_text_multiline ():
358+ text = "foo\n shoes"
359+ fill_char = '-'
360+ width = 7
361+ aligned = cu .ljustify_text (text , fill_char = fill_char , width = width )
362+ assert aligned == ('foo----\n '
363+ 'shoes--' )
364+
365+ def test_left_text_asian_fill ():
366+ """Test fill_char with display width greater than 1"""
367+ text = 'foo'
368+ fill_char = '苹'
369+ width = 5
370+ aligned = cu .ljustify_text (text , fill_char = fill_char , width = width )
371+ assert aligned == text + fill_char
372+
373+ def test_left_text_asian_fill_needs_padding ():
374+ """Test when fill_char's display width does not divide evenly into gap"""
375+ text = 'foo'
376+ fill_char = '苹'
377+ width = 6
378+ aligned = cu .ljustify_text (text , fill_char = fill_char , width = width )
379+ assert aligned == text + fill_char + ' '
380+
381+ def test_center_text ():
382+ text = 'foo'
383+ fill_char = '-'
384+ width = 5
385+ aligned = cu .center_text (text ,fill_char = fill_char , width = width )
386+ assert aligned == fill_char + text + fill_char
387+
388+ def test_center_text_multiline ():
389+ text = "foo\n shoes"
390+ fill_char = '-'
391+ width = 7
392+ aligned = cu .center_text (text , fill_char = fill_char , width = width )
393+ assert aligned == ('--foo--\n '
394+ '-shoes-' )
395+
396+ def test_center_text_asian_fill ():
397+ """Test fill_char with display width greater than 1"""
398+ text = 'foo'
399+ fill_char = '苹'
400+ width = 7
401+ aligned = cu .center_text (text , fill_char = fill_char , width = width )
402+ assert aligned == fill_char + text + fill_char
403+
404+ def test_center_text_asian_fill_needs_right_padding ():
405+ """Test when fill_char's display width does not divide evenly into right gap"""
406+ text = 'foo'
407+ fill_char = '苹'
408+ width = 8
409+ aligned = cu .center_text (text , fill_char = fill_char , width = width )
410+ assert aligned == fill_char + text + fill_char + ' '
411+
412+ def test_center_text_asian_fill_needs_left_and_right_padding ():
413+ """Test when fill_char's display width does not divide evenly into either gap"""
414+ text = 'foo'
415+ fill_char = '苹'
416+ width = 9
417+ aligned = cu .center_text (text , fill_char = fill_char , width = width )
418+ assert aligned == fill_char + ' ' + text + fill_char + ' '
419+
420+ def test_right_text ():
421+ text = 'foo'
422+ fill_char = '-'
423+ width = 5
424+ aligned = cu .rjustify_text (text , fill_char = fill_char , width = width )
425+ assert aligned == fill_char + fill_char + text
426+
427+ def test_right_text_multiline ():
428+ text = "foo\n shoes"
429+ fill_char = '-'
430+ width = 7
431+ aligned = cu .rjustify_text (text , fill_char = fill_char , width = width )
432+ assert aligned == ('----foo\n '
433+ '--shoes' )
434+
435+ def test_right_text_asian_fill ():
436+ """Test fill_char with display width greater than 1"""
437+ text = 'foo'
438+ fill_char = '苹'
439+ width = 5
440+ aligned = cu .rjustify_text (text , fill_char = fill_char , width = width )
441+ assert aligned == fill_char + text
442+
443+ def test_right_text_asian_fill_needs_padding ():
444+ """Test when fill_char's display width does not divide evenly into gap"""
445+ text = 'foo'
446+ fill_char = '苹'
447+ width = 6
448+ aligned = cu .rjustify_text (text , fill_char = fill_char , width = width )
449+ assert aligned == fill_char + ' ' + text
0 commit comments