From 0dfe1c9e2780469e3a4696e8fb3e6f717a7ebeb7 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 16 Sep 2022 11:09:35 +0200 Subject: Adding upstream version 3.1.0. Signed-off-by: Daniel Baumann --- tests/test_width_and_alignment/test_table_width.py | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/test_width_and_alignment/test_table_width.py (limited to 'tests/test_width_and_alignment/test_table_width.py') diff --git a/tests/test_width_and_alignment/test_table_width.py b/tests/test_width_and_alignment/test_table_width.py new file mode 100644 index 0000000..5818789 --- /dev/null +++ b/tests/test_width_and_alignment/test_table_width.py @@ -0,0 +1,70 @@ +"""Test function in module.""" + +from terminaltables.width_and_alignment import max_dimensions, table_width + + +def test_empty(): + """Test with zero-length cells.""" + assert table_width(max_dimensions([['']])[2], 0, 0) == 0 + assert table_width(max_dimensions([['', '', '']])[2], 0, 0) == 0 + assert table_width(max_dimensions([['', '', ''], ['', '', '']])[2], 0, 0) == 0 + + assert table_width(max_dimensions([['']], 1, 1)[2], 2, 1) == 4 + assert table_width(max_dimensions([['', '', '']], 1, 1)[2], 2, 1) == 10 + assert table_width(max_dimensions([['', '', ''], ['', '', '']], 1, 1)[2], 2, 1) == 10 + + +def test_single_line(): + """Test with single-line cells.""" + table_data = [ + ['Name', 'Color', 'Type'], + ['Avocado', 'green', 'nut'], + ['Tomato', 'red', 'fruit'], + ['Lettuce', 'green', 'vegetable'], + ] + + # '| Lettuce | green | vegetable |' + outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1, 1)[2] + assert table_width(outer_widths, outer, inner) == 31 + + # ' Lettuce | green | vegetable ' + outer = 0 + assert table_width(outer_widths, outer, inner) == 29 + + # '| Lettuce green vegetable |' + outer, inner = 2, 0 + assert table_width(outer_widths, outer, inner) == 29 + + # ' Lettuce green vegetable ' + outer = 0 + assert table_width(outer_widths, outer, inner) == 27 + + # '|Lettuce |green |vegetable |' + outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1)[2] + assert table_width(outer_widths, outer, inner) == 28 + + # '|Lettuce |green |vegetable |' + outer_widths = max_dimensions(table_data, 3, 2)[2] + assert table_width(outer_widths, outer, inner) == 40 + + table_data = [ + ['Name', 'Color', 'Type'], + ['Avocado', 'green', 'nut'], + ['Tomato', 'red', 'fruit'], + ['Lettuce', 'green', 'vegetable'], + ['Watermelon', 'green', 'fruit'], + ] + outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1, 1)[2] + assert table_width(outer_widths, outer, inner) == 34 + + +def test_multi_line(): + """Test with multi-line cells.""" + table_data = [ + ['Show', 'Characters'], + ['Rugrats', ('Tommy Pickles, Chuckie Finster, Phillip DeVille, Lillian DeVille, Angelica Pickles,\n' + 'Susie Carmichael, Dil Pickles, Kimi Finster, Spike')], + ['South Park', 'Stan Marsh, Kyle Broflovski, Eric Cartman, Kenny McCormick'] + ] + outer, inner, outer_widths = 2, 1, max_dimensions(table_data, 1, 1)[2] + assert table_width(outer_widths, outer, inner) == 100 -- cgit v1.2.3