2020-06-06 14:34:59 +00:00
# encoding=utf-8
import io
2020-06-28 10:10:59 +00:00
import pytest
2020-06-06 14:34:59 +00:00
2022-01-07 11:49:21 +00:00
from rich import box , errors
from rich . align import VerticalAlignMethod
2020-06-06 14:34:59 +00:00
from rich . console import Console
2020-06-28 10:10:59 +00:00
from rich . measure import Measurement
2021-06-17 21:31:08 +00:00
from rich . style import Style
2022-01-07 11:49:21 +00:00
from rich . table import Column , Table
2020-06-23 10:40:31 +00:00
from rich . text import Text
2020-06-06 14:34:59 +00:00
def render_tables ( ) :
2020-06-23 10:40:31 +00:00
console = Console (
width = 60 ,
force_terminal = True ,
file = io . StringIO ( ) ,
legacy_windows = False ,
2020-06-28 10:10:59 +00:00
color_system = None ,
2021-03-01 19:42:00 +00:00
_environ = { } ,
2020-06-23 10:40:31 +00:00
)
2020-06-06 14:34:59 +00:00
2021-02-20 11:05:21 +00:00
table = Table ( title = " test table " , caption = " table caption " , expand = False )
2020-06-23 10:40:31 +00:00
table . add_column ( " foo " , footer = Text ( " total " ) , no_wrap = True , overflow = " ellipsis " )
2020-06-06 14:34:59 +00:00
table . add_column ( " bar " , justify = " center " )
table . add_column ( " baz " , justify = " right " )
table . add_row ( " Averlongwordgoeshere " , " banana pancakes " , None )
2020-06-28 10:10:59 +00:00
2021-03-25 21:08:36 +00:00
assert Measurement . get ( console , console . options , table ) == Measurement ( 41 , 48 )
2021-02-20 11:05:21 +00:00
table . expand = True
2021-03-25 21:08:36 +00:00
assert Measurement . get ( console , console . options , table ) == Measurement ( 41 , 48 )
2020-06-28 10:10:59 +00:00
2020-06-06 14:34:59 +00:00
for width in range ( 10 , 60 , 5 ) :
console . print ( table , width = width )
2020-06-23 10:40:31 +00:00
table . expand = False
console . print ( table , justify = " left " )
console . print ( table , justify = " center " )
console . print ( table , justify = " right " )
assert table . row_count == 1
table . row_styles = [ " red " , " yellow " ]
2020-06-28 10:10:59 +00:00
table . add_row ( " Coffee " )
table . add_row ( " Coffee " , " Chocolate " , None , " cinnamon " )
2020-06-23 10:40:31 +00:00
2020-06-28 10:10:59 +00:00
assert table . row_count == 3
2020-06-23 10:40:31 +00:00
console . print ( table )
table . show_lines = True
console . print ( table )
table . show_footer = True
console . print ( table )
table . show_edge = False
console . print ( table )
table . padding = 1
console . print ( table )
table . width = 20
2021-03-25 21:08:36 +00:00
assert Measurement . get ( console , console . options , table ) == Measurement ( 20 , 20 )
2021-02-20 11:05:21 +00:00
table . expand = False
2021-03-25 21:08:36 +00:00
assert Measurement . get ( console , console . options , table ) == Measurement ( 20 , 20 )
2021-02-20 11:05:21 +00:00
table . expand = True
2020-06-23 10:40:31 +00:00
console . print ( table )
2020-07-12 15:17:36 +00:00
table . columns [ 0 ] . no_wrap = True
table . columns [ 1 ] . no_wrap = True
table . columns [ 2 ] . no_wrap = True
console . print ( table )
table . padding = 0
table . width = 60
table . leading = 1
console . print ( table )
2020-06-06 14:34:59 +00:00
return console . file . getvalue ( )
def test_render_table ( ) :
2021-03-01 19:42:00 +00:00
expected = " test table \n ┏━━━━━━┳┳┓ \n ┃ foo ┃┃┃ \n ┡━━━━━━╇╇┩ \n │ Ave… │││ \n └──────┴┴┘ \n table \n caption \n test table \n ┏━━━━━━━━━━━┳┳┓ \n ┃ foo ┃┃┃ \n ┡━━━━━━━━━━━╇╇┩ \n │ Averlong… │││ \n └───────────┴┴┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━┳┳┓ \n ┃ foo ┃┃┃ \n ┡━━━━━━━━━━━━━━━━╇╇┩ \n │ Averlongwordg… │││ \n └────────────────┴┴┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━┳┳┓ \n ┃ foo ┃┃┃ \n ┡━━━━━━━━━━━━━━━━━━━━━╇╇┩ \n │ Averlongwordgoeshe… │││ \n └─────────────────────┴┴┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━┳━━┳━━┓ \n ┃ foo ┃ ┃ ┃ \n ┡━━━━━━━━━━━━━━━━━━━━━━╇━━╇━━┩ \n │ Averlongwordgoeshere │ │ │ \n └──────────────────────┴──┴──┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━┳━━━━┓ \n ┃ foo ┃ bar ┃ b… ┃ \n ┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━╇━━━━┩ \n │ Averlongwordgoeshere │ ba… │ │ \n │ │ pa… │ │ \n └──────────────────────┴─────┴────┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━┓ \n ┃ foo ┃ bar ┃ baz ┃ \n ┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━┩ \n │ Averlongwordgoeshere │ banana │ │ \n │ │ pancak… │ │ \n └──────────────────────┴─────────┴─────┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━┓ \n ┃ foo ┃ bar ┃ baz ┃ \n ┡━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━┩ \n │ Averlongwordgoeshere │ banana │ │ \n │ │ pancakes │ │ \n └──────────────────────┴──────────────┴─────┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━┓ \n ┃ foo ┃ bar ┃ baz ┃ \n ┡━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━┩ \n │ Averlongwordgoeshere │ banana pancakes │ │ \n └───────────────────────┴──────────────────┴─────┘ \n table caption \n test table \n ┏━━━━━━━━━━━━━━━━━━━━<EFBFBD>
result = render_tables ( )
print ( repr ( result ) )
assert result == expected
2020-06-06 14:34:59 +00:00
2020-06-28 10:10:59 +00:00
def test_not_renderable ( ) :
class Foo :
pass
table = Table ( )
with pytest . raises ( errors . NotRenderableError ) :
table . add_row ( Foo ( ) )
2020-10-08 08:31:31 +00:00
def test_init_append_column ( ) :
2020-10-08 07:33:32 +00:00
header_names = [ " header1 " , " header2 " , " header3 " ]
test_columns = [
Column ( _index = index , header = header ) for index , header in enumerate ( header_names )
]
# Test appending of strings for header names
2020-10-08 07:44:16 +00:00
assert Table ( * header_names ) . columns == test_columns
2020-10-08 07:33:32 +00:00
# Test directly passing a Table Column objects
assert Table ( * test_columns ) . columns == test_columns
2020-10-08 07:44:16 +00:00
def test_rich_measure ( ) :
2021-03-25 21:08:36 +00:00
console = Console ( )
assert Table ( " test_header " , width = - 1 ) . __rich_measure__ (
console , console . options
2020-10-08 08:31:31 +00:00
) == Measurement ( 0 , 0 )
# Check __rich_measure__() for a positive width passed as an argument
assert Table ( " test_header " , width = None ) . __rich_measure__ (
2021-03-25 21:08:36 +00:00
console , console . options . update_width ( 10 )
2020-10-08 08:31:31 +00:00
) == Measurement ( 10 , 10 )
2020-10-08 07:44:16 +00:00
2020-10-17 16:10:37 +00:00
def test_min_width ( ) :
table = Table ( " foo " , min_width = 30 )
table . add_row ( " bar " )
2021-03-25 21:08:36 +00:00
console = Console ( )
assert table . __rich_measure__ (
console , console . options . update_width ( 100 )
) == Measurement ( 30 , 30 )
2020-10-17 16:10:37 +00:00
console = Console ( color_system = None )
console . begin_capture ( )
console . print ( table )
output = console . end_capture ( )
print ( output )
assert all ( len ( line ) == 30 for line in output . splitlines ( ) )
2021-02-24 21:28:34 +00:00
def test_no_columns ( ) :
console = Console ( color_system = None )
console . begin_capture ( )
console . print ( Table ( ) )
output = console . end_capture ( )
print ( repr ( output ) )
assert output == " \n "
2021-06-17 21:31:08 +00:00
def test_get_row_style ( ) :
console = Console ( )
table = Table ( )
table . add_row ( " foo " )
table . add_row ( " bar " , style = " on red " )
assert table . get_row_style ( console , 0 ) == Style . parse ( " " )
assert table . get_row_style ( console , 1 ) == Style . parse ( " on red " )
2022-01-07 11:49:21 +00:00
def test_vertical_align_top ( ) :
console = Console ( _environ = { } )
def make_table ( vertical_align ) :
table = Table ( show_header = False , box = box . SQUARE )
2022-01-07 20:38:23 +00:00
table . add_column ( vertical = vertical_align )
2022-01-07 11:49:21 +00:00
table . add_row ( " foo " , " \n " . join ( [ " bar " ] * 5 ) )
return table
with console . capture ( ) as capture :
console . print ( make_table ( " top " ) )
console . print ( )
console . print ( make_table ( " middle " ) )
console . print ( )
console . print ( make_table ( " bottom " ) )
console . print ( )
result = capture . get ( )
print ( repr ( result ) )
expected = " ┌─────┬─────┐ \n │ foo │ bar │ \n │ │ bar │ \n │ │ bar │ \n │ │ bar │ \n │ │ bar │ \n └─────┴─────┘ \n \n ┌─────┬─────┐ \n │ │ bar │ \n │ │ bar │ \n │ foo │ bar │ \n │ │ bar │ \n │ │ bar │ \n └─────┴─────┘ \n \n ┌─────┬─────┐ \n │ │ bar │ \n │ │ bar │ \n │ │ bar │ \n │ │ bar │ \n │ foo │ bar │ \n └─────┴─────┘ \n \n "
assert result == expected
2020-06-06 14:34:59 +00:00
if __name__ == " __main__ " :
render = render_tables ( )
print ( render )
print ( repr ( render ) )