rich/tests/test_columns.py

73 lines
6.7 KiB
Python
Raw Normal View History

2020-06-06 13:26:58 +00:00
# encoding=utf-8
2020-05-31 11:56:25 +00:00
import io
from rich.columns import Columns
from rich.console import Console
COLUMN_DATA = [
"Ursus americanus",
"American buffalo",
"Bison bison",
"American crow",
"Corvus brachyrhynchos",
"American marten",
"Martes americana",
"American racer",
"Coluber constrictor",
"American woodcock",
"Scolopax minor",
"Anaconda (unidentified)",
"Eunectes sp.",
"Andean goose",
"Chloephaga melanoptera",
"Ant",
"Anteater, australian spiny",
"Tachyglossus aculeatus",
"Anteater, giant",
"Myrmecophaga tridactyla",
]
def render():
console = Console(file=io.StringIO(), width=100)
2020-06-04 15:49:39 +00:00
2020-06-06 11:08:07 +00:00
console.rule("empty")
empty_columns = Columns([])
console.print(empty_columns)
2020-05-31 11:56:25 +00:00
columns = Columns(COLUMN_DATA)
2020-06-04 15:49:39 +00:00
console.rule("optimal")
2020-05-31 11:56:25 +00:00
console.print(columns)
2020-06-06 11:08:07 +00:00
console.rule("optimal, expand")
columns.expand = True
console.print(columns)
2020-06-04 15:49:39 +00:00
console.rule("columm first, optimal")
2020-05-31 11:56:25 +00:00
columns.column_first = True
2020-06-06 11:08:07 +00:00
columns.expand = False
2020-05-31 11:56:25 +00:00
console.print(columns)
2020-06-04 15:49:39 +00:00
console.rule("column first, right to left")
2020-05-31 11:56:25 +00:00
columns.right_to_left = True
console.print(columns)
2020-06-04 15:49:39 +00:00
console.rule("equal columns, expand")
2020-05-31 11:56:25 +00:00
columns.equal = True
columns.expand = True
console.print(columns)
2020-06-04 15:49:39 +00:00
console.rule("fixed width")
2020-06-01 15:08:06 +00:00
columns.width = 16
console.print(columns)
console.print()
2020-05-31 11:56:25 +00:00
render_result = console.file.getvalue()
return render_result
def test_render():
2020-06-06 13:26:58 +00:00
expected = "────────────────────────────────────────────── empty ───────────────────────────────────────────────\n───────────────────────────────────────────── optimal ──────────────────────────────────────────────\nUrsus americanus American buffalo Bison bison American crow \nCorvus brachyrhynchos American marten Martes americana American racer \nColuber constrictor American woodcock Scolopax minor Anaconda (unidentified)\nEunectes sp. Andean goose Chloephaga melanoptera Ant \nAnteater, australian spiny Tachyglossus aculeatus Anteater, giant Myrmecophaga tridactyla\n───────────────────────────────────────── optimal, expand ──────────────────────────────────────────\nUrsus americanus American buffalo Bison bison American crow \nCorvus brachyrhynchos American marten Martes americana American racer \nColuber constrictor American woodcock Scolopax minor Anaconda (unidentified) \nEunectes sp. Andean goose Chloephaga melanoptera Ant \nAnteater, australian spiny Tachyglossus aculeatus Anteater, giant Myrmecophaga tridactyla \n────────────────────────────────────── columm first, optimal ───────────────────────────────────────\nUrsus americanus American marten Scolopax minor Ant \nAmerican buffalo Martes americana Anaconda (unidentified) Anteater, australian spiny\nBison bison American racer Eunectes sp. Tachyglossus aculeatus \nAmerican crow Coluber constrictor Andean goose Anteater, giant \nCorvus brachyrhynchos American woodcock Chloephaga melanoptera Myrmecophaga tridactyla \n─────────────────────────────────── column first, right to left ────────────────────────────────────\nAnt Scolopax minor American marten Ursus americanus \nAnteater, australian spiny Anaconda (unidentified) Martes americana American buffalo \nTachyglossus aculeatus Eunectes sp. American racer Bison bison \nAnteater, giant Andean goose Coluber constrictor American crow \nMyrmecophaga tridactyla Chloephaga melanoptera American woodcock Corvus brachyrhynchos\n────────────────────────────────────── equal columns, expand ───────────────────────────────────────\nChloephaga melanoptera American racer Ursus americanus \nAnt Coluber constrictor American buffalo \nAnteater, australian spiny American woodcock Bison bison \nTachyglossus aculeatus Scolopax minor American crow \nAnteater, giant Anaconda (unidentified) Corvus brachyrhynchos \nMyrmecophaga tridactyla
2020-05-31 11:56:25 +00:00
assert render() == expected
if __name__ == "__main__":
result = render()
print(result)
print(repr(result))