def test_heading_footing(inner_column_border, outer_border, style): """Test heading and footing borders. :param bool inner_column_border: Passed to table class. :param bool outer_border: Passed to table class. :param str style: Passed to method. """ table = BaseTable(SINGLE_LINE) table.inner_column_border = inner_column_border table.outer_border = outer_border outer_widths = max_dimensions(table.table_data, table.padding_left, table.padding_right)[2] # Determine expected. if style == 'heading' and outer_border: expected = '+---------+-------+-----------+' if inner_column_border else '+---------------------------+' elif style == 'heading': expected = '---------+-------+-----------' if inner_column_border else '---------------------------' elif style == 'footing' and outer_border: expected = '+---------+-------+-----------+' if inner_column_border else '+---------------------------+' else: expected = '---------+-------+-----------' if inner_column_border else '---------------------------' # Test. actual = ''.join(table.horizontal_border(style, outer_widths)) assert actual == expected
def test_outer_borders(outer_border): """Test left/right/top/bottom table borders. :param bool outer_border: Passed to table. """ table_data = [ ['Name', 'Color', 'Type'], ['Avocado', 'green', 'nut'], ['Tomato', 'red', 'fruit'], ['Lettuce', 'green', 'vegetable'], ] table = BaseTable(table_data, 'Example Table') table.outer_border = outer_border inner_widths, inner_heights, outer_widths = max_dimensions( table_data, table.padding_left, table.padding_right)[:3] actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths)) # Determine expected. if outer_border: expected = ('+Example Table----+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') else: expected = (' Name | Color | Type \n' '---------+-------+-----------\n' ' Avocado | green | nut \n' ' Tomato | red | fruit \n' ' Lettuce | green | vegetable ') assert actual == expected
def test_row(inner_column_border, outer_border): """Test inner borders. :param bool inner_column_border: Passed to table class. :param bool outer_border: Passed to table class. """ table = BaseTable(SINGLE_LINE) table.inner_column_border = inner_column_border table.outer_border = outer_border outer_widths = max_dimensions(table.table_data, table.padding_left, table.padding_right)[2] # Determine expected. if inner_column_border and outer_border: expected = '+---------+-------+-----------+' elif inner_column_border: expected = '---------+-------+-----------' elif outer_border: expected = '+---------------------------+' else: expected = '---------------------------' # Test. actual = ''.join(table.horizontal_border('row', outer_widths)) assert actual == expected
def test_uneven(): """Test with row missing cells.""" row = ['Row One Column One'] table = BaseTable([row]) actual = list(table.gen_cell_lines(row, [18, 3, 5], 1)) expected = [ ('|', ' Row One Column One ', '|', ' ', '|', ' ', '|'), ] assert actual == expected
def test_empty_table(): """Test empty table.""" row = [] table = BaseTable([row]) actual = list(table.gen_cell_lines(row, [], 0)) expected = [ ('|', '|'), ] assert actual == expected
def test_single_line(): """Test with single-line row.""" row = ['Row One Column One', 'Two', 'Three'] table = BaseTable([row]) actual = list(table.gen_cell_lines(row, [18, 3, 5], 1)) expected = [ ('|', ' Row One Column One ', '|', ' Two ', '|', ' Three ', '|'), ] assert actual == expected
def test_multi_line(): """Test with multi-line row.""" row = ['Row One\nColumn One', 'Two', 'Three'] table = BaseTable([row]) actual = list(table.gen_cell_lines(row, [10, 3, 5], 2)) expected = [ ('|', ' Row One ', '|', ' Two ', '|', ' Three ', '|'), ('|', ' Column One ', '|', ' ', '|', ' ', '|'), ] assert actual == expected
def test_single_line(style): """Test with single-line row. :param str style: Passed to method. """ row = ['Row One Column One', 'Two', 'Three'] table = BaseTable([row]) actual = [tuple(i) for i in table.gen_row_lines(row, style, [18, 3, 5], 1)] expected = [ ('|', ' Row One Column One ', '|', ' Two ', '|', ' Three ', '|'), ] assert actual == expected
def test_empty_table(style): """Test empty table. :param str style: Passed to method. """ row = [] table = BaseTable([row]) actual = [tuple(i) for i in table.gen_row_lines(row, style, [], 0)] expected = [ ('|', '|'), ] assert actual == expected
def test_uneven(style): """Test with row missing cells. :param str style: Passed to method. """ row = ['Row One Column One'] table = BaseTable([row]) actual = [tuple(i) for i in table.gen_row_lines(row, style, [18, 3, 5], 1)] expected = [ ('|', ' Row One Column One ', '|', ' ', '|', ' ', '|'), ] assert actual == expected
def test_multi_line(style): """Test with multi-line row. :param str style: Passed to method. """ row = ['Row One\nColumn One', 'Two', 'Three'] table = BaseTable([row]) actual = [tuple(i) for i in table.gen_row_lines(row, style, [10, 3, 5], 2)] expected = [ ('|', ' Row One ', '|', ' Two ', '|', ' Three ', '|'), ('|', ' Column One ', '|', ' ', '|', ' ', '|'), ] assert actual == expected
def test_rtl(): """Test with RTL characters.""" table_data = [ [u'RTL'], [u'שלום'], [u'معرب'], ] table = BaseTable(table_data) actual = table.unicode_table(encoding='utf-8') expected = ('+------+\n' '| RTL |\n' '+------+\n' '| שלום |\n' '| معرب |\n' '+------+') assert actual == expected
def test_cjk(): """Test with CJK characters.""" table_data = [ [u'CJK'], [u'蓝色'], [u'世界你好'], ] table = BaseTable(table_data) actual = table.unicode_table(encoding='utf-8') expected = ('+----------+\n' '| CJK |\n' '+----------+\n' '| 蓝色 |\n' '| 世界你好 |\n' '+----------+') assert actual == expected
def test_rtl_large(): """Test large table of RTL characters.""" table_data = [ [u'اكتب', u'اللون', u'اسم'], [u'البندق', u'أخضر', u'أفوكادو'], [u'ثمرة', u'أحمر', u'بندورة'], [u'الخضروات', u'أخضر', u'الخس'], ] table = BaseTable(table_data, u'جوجل المترجم') actual = table.unicode_table(encoding='utf-8') expected = ('+جوجل المترجم------+---------+\n' '| اكتب | اللون | اسم |\n' '+----------+-------+---------+\n' '| البندق | أخضر | أفوكادو |\n' '| ثمرة | أحمر | بندورة |\n' '| الخضروات | أخضر | الخس |\n' '+----------+-------+---------+') assert actual == expected
def test_int(): """Test with integers instead of strings.""" table_data = [ [100, 10, 1], [0, 3, 6], [1, 4, 7], [2, 5, 8], ] table = BaseTable(table_data, 1234567890) actual = table.unicode_table() expected = ('+1234567890+---+\n' '| 100 | 10 | 1 |\n' '+-----+----+---+\n' '| 0 | 3 | 6 |\n' '| 1 | 4 | 7 |\n' '| 2 | 5 | 8 |\n' '+-----+----+---+') assert actual == expected
def test_float(): """Test with floats instead of strings.""" table_data = [ [1.0, 22.0, 333.0], [0.1, 3.1, 6.1], [1.1, 4.1, 7.1], [2.1, 5.1, 8.1], ] table = BaseTable(table_data, 0.12345678) actual = table.unicode_table() expected = ('+0.12345678--+-------+\n' '| 1.0 | 22.0 | 333.0 |\n' '+-----+------+-------+\n' '| 0.1 | 3.1 | 6.1 |\n' '| 1.1 | 4.1 | 7.1 |\n' '| 2.1 | 5.1 | 8.1 |\n' '+-----+------+-------+') assert actual == expected
def test_bool_none(): """Test with NoneType/boolean instead of strings.""" table_data = [ [True, False, None], [True, False, None], [False, None, True], [None, True, False], ] table = BaseTable(table_data, True) actual = table.unicode_table() expected = ('+True---+-------+-------+\n' '| True | False | None |\n' '+-------+-------+-------+\n' '| True | False | None |\n' '| False | None | True |\n' '| None | True | False |\n' '+-------+-------+-------+') assert actual == expected
def test_ascii(): """Test with ASCII characters.""" table_data = [ ['Name', 'Color', 'Type'], ['Avocado', 'green', 'nut'], ['Tomato', 'red', 'fruit'], ['Lettuce', 'green', 'vegetable'], ] table = BaseTable(table_data) actual = table.unicode_table() expected = ('+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') assert actual == expected
def test_color(): """Test with color characters.""" table_data = [ ['ansi', '\033[31mRed\033[39m', '\033[32mGreen\033[39m', '\033[34mBlue\033[39m'], ['colorclass', Color('{red}Red{/red}'), Color('{green}Green{/green}'), Color('{blue}Blue{/blue}')], ['colorama', Fore.RED + 'Red' + Fore.RESET, Fore.GREEN + 'Green' + Fore.RESET, Fore.BLUE + 'Blue' + Fore.RESET], ['termcolor', colored('Red', 'red'), colored('Green', 'green'), colored('Blue', 'blue')], ] table = BaseTable(table_data) table.inner_heading_row_border = False actual = table.table expected = ( u'+------------+-----+-------+------+\n' u'| ansi | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n' u'| colorclass | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n' u'| colorama | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n' u'| termcolor | \033[31mRed\033[0m | \033[32mGreen\033[0m | \033[34mBlue\033[0m |\n' u'+------------+-----+-------+------+' ) assert actual == expected
def test_top_bottom(inner_column_border, style): """Test top and bottom borders. :param bool inner_column_border: Passed to table class. :param str style: Passed to method. """ table = BaseTable(SINGLE_LINE, 'Example') table.inner_column_border = inner_column_border outer_widths = max_dimensions(table.table_data, table.padding_left, table.padding_right)[2] # Determine expected. if style == 'top' and inner_column_border: expected = '+Example--+-------+-----------+' elif style == 'top': expected = '+Example--------------------+' elif style == 'bottom' and inner_column_border: expected = '+---------+-------+-----------+' else: expected = '+---------------------------+' # Test. actual = ''.join(table.horizontal_border(style, outer_widths)) assert actual == expected
def test_outer_borders(outer_border): """Test left/right/top/bottom table borders. :param bool outer_border: Passed to table. """ table_data = [ ['Name', 'Color', 'Type'], ['Avocado', 'green', 'nut'], ['Tomato', 'red', 'fruit'], ['Lettuce', 'green', 'vegetable'], ] table = BaseTable(table_data, 'Example Table') table.outer_border = outer_border inner_widths, inner_heights, outer_widths = max_dimensions(table_data, table.padding_left, table.padding_right)[:3] actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths)) # Determine expected. if outer_border: expected = ( '+Example Table----+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+' ) else: expected = ( ' Name | Color | Type \n' '---------+-------+-----------\n' ' Avocado | green | nut \n' ' Tomato | red | fruit \n' ' Lettuce | green | vegetable ' ) assert actual == expected
def test_color(): """Test with color characters.""" table_data = [ [ 'ansi', '\033[31mRed\033[39m', '\033[32mGreen\033[39m', '\033[34mBlue\033[39m' ], [ 'colorclass', Color('{red}Red{/red}'), Color('{green}Green{/green}'), Color('{blue}Blue{/blue}') ], [ 'colorama', Fore.RED + 'Red' + Fore.RESET, Fore.GREEN + 'Green' + Fore.RESET, Fore.BLUE + 'Blue' + Fore.RESET ], [ 'termcolor', colored('Red', 'red'), colored('Green', 'green'), colored('Blue', 'blue') ], ] table = BaseTable(table_data) table.inner_heading_row_border = False actual = table.table expected = ( u'+------------+-----+-------+------+\n' u'| ansi | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n' u'| colorclass | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n' u'| colorama | \033[31mRed\033[39m | \033[32mGreen\033[39m | \033[34mBlue\033[39m |\n' u'| termcolor | \033[31mRed\033[0m | \033[32mGreen\033[0m | \033[34mBlue\033[0m |\n' u'+------------+-----+-------+------+') assert actual == expected
def test_rtl(): """Test with RTL characters.""" table_data = [ ['RTL'], ['שלום'], ['معرب'], ] table = BaseTable(table_data) actual = table.table expected = ('+------+\n' '| RTL |\n' '+------+\n' '| שלום |\n' '| معرب |\n' '+------+') assert actual == expected
def test_cjk(): """Test with CJK characters.""" table_data = [ ['CJK'], ['蓝色'], ['世界你好'], ] table = BaseTable(table_data) actual = table.table expected = ('+----------+\n' '| CJK |\n' '+----------+\n' '| 蓝色 |\n' '| 世界你好 |\n' '+----------+') assert actual == expected
def test_no_padding_no_borders(style): """Test without padding or borders. :param str style: Passed to method. """ row = ['Row One\nColumn One', 'Two', 'Three'] table = BaseTable([row]) table.inner_column_border = False table.outer_border = False table.padding_left = 0 table.padding_right = 0 actual = [tuple(i) for i in table.gen_row_lines(row, style, [10, 3, 5], 2)] expected = [ ('Row One ', 'Two', 'Three'), ('Column One', ' ', ' '), ] assert actual == expected
def test_no_padding_no_borders(): """Test without padding or borders.""" row = ['Row One\nColumn One', 'Two', 'Three'] table = BaseTable([row]) table.inner_column_border = False table.outer_border = False table.padding_left = 0 table.padding_right = 0 actual = list(table.gen_cell_lines(row, [10, 3, 5], 2)) expected = [ ('Row One ', 'Two', 'Three'), ('Column One', ' ', ' '), ] assert actual == expected
def test_inner_row_borders(inner_heading_row_border, inner_footing_row_border, inner_row_border): """Test heading/footing/row borders. :param bool inner_heading_row_border: Passed to table. :param bool inner_footing_row_border: Passed to table. :param bool inner_row_border: Passed to table. """ table_data = [ ['Name', 'Color', 'Type'], ['Avocado', 'green', 'nut'], ['Tomato', 'red', 'fruit'], ['Lettuce', 'green', 'vegetable'], ] table = BaseTable(table_data) table.inner_heading_row_border = inner_heading_row_border table.inner_footing_row_border = inner_footing_row_border table.inner_row_border = inner_row_border inner_widths, inner_heights, outer_widths = max_dimensions(table_data, table.padding_left, table.padding_right)[:3] actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths)) # Determine expected. if inner_row_border: expected = ( '+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '+---------+-------+-----------+\n' '| Tomato | red | fruit |\n' '+---------+-------+-----------+\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+' ) elif inner_heading_row_border and inner_footing_row_border: expected = ( '+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '+---------+-------+-----------+\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+' ) elif inner_heading_row_border: expected = ( '+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+' ) elif inner_footing_row_border: expected = ( '+---------+-------+-----------+\n' '| Name | Color | Type |\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '+---------+-------+-----------+\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+' ) else: expected = ( '+---------+-------+-----------+\n' '| Name | Color | Type |\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+' ) assert actual == expected
def test_inner_row_borders(inner_heading_row_border, inner_footing_row_border, inner_row_border): """Test heading/footing/row borders. :param bool inner_heading_row_border: Passed to table. :param bool inner_footing_row_border: Passed to table. :param bool inner_row_border: Passed to table. """ table_data = [ ['Name', 'Color', 'Type'], ['Avocado', 'green', 'nut'], ['Tomato', 'red', 'fruit'], ['Lettuce', 'green', 'vegetable'], ] table = BaseTable(table_data) table.inner_heading_row_border = inner_heading_row_border table.inner_footing_row_border = inner_footing_row_border table.inner_row_border = inner_row_border inner_widths, inner_heights, outer_widths = max_dimensions( table_data, table.padding_left, table.padding_right)[:3] actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths)) # Determine expected. if inner_row_border: expected = ('+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '+---------+-------+-----------+\n' '| Tomato | red | fruit |\n' '+---------+-------+-----------+\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') elif inner_heading_row_border and inner_footing_row_border: expected = ('+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '+---------+-------+-----------+\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') elif inner_heading_row_border: expected = ('+---------+-------+-----------+\n' '| Name | Color | Type |\n' '+---------+-------+-----------+\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') elif inner_footing_row_border: expected = ('+---------+-------+-----------+\n' '| Name | Color | Type |\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '+---------+-------+-----------+\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') else: expected = ('+---------+-------+-----------+\n' '| Name | Color | Type |\n' '| Avocado | green | nut |\n' '| Tomato | red | fruit |\n' '| Lettuce | green | vegetable |\n' '+---------+-------+-----------+') assert actual == expected
def test_one_no_rows(mode, bare): """Test with one or no rows. :param str mode: Type of table contents to test. :param bool bare: Disable padding/borders. """ if mode == 'row': table_data = [ ['Avocado', 'green', 'nut'], ] elif mode == 'one': table_data = [ ['Avocado'], ] elif mode == 'blank': table_data = [ [''], ] elif mode == 'empty': table_data = [ [], ] else: table_data = [] table = BaseTable(table_data) if bare: table.inner_column_border = False table.inner_footing_row_border = False table.inner_heading_row_border = False table.inner_row_border = False table.outer_border = False table.padding_left = 0 table.padding_right = 0 inner_widths, inner_heights, outer_widths = max_dimensions( table_data, table.padding_left, table.padding_right)[:3] actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths)) # Determine expected. if mode == 'row': if bare: expected = ('Avocadogreennut') else: expected = ('+---------+-------+-----+\n' '| Avocado | green | nut |\n' '+---------+-------+-----+') elif mode == 'one': if bare: expected = ('Avocado') else: expected = ('+---------+\n' '| Avocado |\n' '+---------+') elif mode == 'blank': # Remember there's still padding. if bare: expected = ('') else: expected = ('+--+\n' '| |\n' '+--+') elif mode == 'empty': if bare: expected = ('') else: expected = ('++\n' '||\n' '++') else: if bare: expected = ('') else: expected = ('++\n' '++') assert actual == expected
def test_one_no_rows(mode, bare): """Test with one or no rows. :param str mode: Type of table contents to test. :param bool bare: Disable padding/borders. """ if mode == 'row': table_data = [ ['Avocado', 'green', 'nut'], ] elif mode == 'one': table_data = [ ['Avocado'], ] elif mode == 'blank': table_data = [ [''], ] elif mode == 'empty': table_data = [ [], ] else: table_data = [ ] table = BaseTable(table_data) if bare: table.inner_column_border = False table.inner_footing_row_border = False table.inner_heading_row_border = False table.inner_row_border = False table.outer_border = False table.padding_left = 0 table.padding_right = 0 inner_widths, inner_heights, outer_widths = max_dimensions(table_data, table.padding_left, table.padding_right)[:3] actual = flatten(table.gen_table(inner_widths, inner_heights, outer_widths)) # Determine expected. if mode == 'row': if bare: expected = ( 'Avocadogreennut' ) else: expected = ( '+---------+-------+-----+\n' '| Avocado | green | nut |\n' '+---------+-------+-----+' ) elif mode == 'one': if bare: expected = ( 'Avocado' ) else: expected = ( '+---------+\n' '| Avocado |\n' '+---------+' ) elif mode == 'blank': # Remember there's still padding. if bare: expected = ( '' ) else: expected = ( '+--+\n' '| |\n' '+--+' ) elif mode == 'empty': if bare: expected = ( '' ) else: expected = ( '++\n' '||\n' '++' ) else: if bare: expected = ( '' ) else: expected = ( '++\n' '++' ) assert actual == expected