Anna Schumaker
456cef1090
This class is designed to make it easy to print out data formatted as a table. It was inspired by the prettytable Python module but is tailored for how I intend to use it, including support for color schemes. I use two different character sets for drawing borders. If color is disabled, then I use basic ascii "+" and "-" characters. However, if colors are enabled then I use characters from the Unicode Box Drawing block to draw a table with rounded corners. Finally, each table cell is formatted by calling the "do_format_cell()" function. Table subclasses can override this function to implement their own coloring schemes. Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
168 lines
7.0 KiB
Python
168 lines
7.0 KiB
Python
# Copyright 2023 (c) Anna Schumaker.
|
|
"""Tests our table printing code."""
|
|
import io
|
|
import unittest
|
|
import unittest.mock
|
|
import xfstestsdb.table
|
|
|
|
|
|
class TestTableBorders(unittest.TestCase):
|
|
"""Test the Table Borders character set."""
|
|
|
|
def test_ascii_borders(self):
|
|
"""Test ascii borders."""
|
|
self.assertDictEqual(xfstestsdb.table.get_borders(True),
|
|
xfstestsdb.table.ASCII_BORDERS)
|
|
self.assertDictEqual(xfstestsdb.table.ASCII_BORDERS,
|
|
{"horizontal": "-",
|
|
"vertical": "|",
|
|
"top-left": "+-",
|
|
"top-center": "-+-",
|
|
"top-right": "-+",
|
|
"mid-left": "+-",
|
|
"mid-center": "-+-",
|
|
"mid-right": "-+",
|
|
"bot-left": "+-",
|
|
"bot-center": "-+-",
|
|
"bot-right": "-+"})
|
|
|
|
def test_unicode_borders(self):
|
|
"""Test unicode borders."""
|
|
self.assertDictEqual(xfstestsdb.table.get_borders(False),
|
|
xfstestsdb.table.UNICODE_BORDERS)
|
|
self.assertDictEqual(xfstestsdb.table.UNICODE_BORDERS,
|
|
{"horizontal": "─",
|
|
"vertical": "│",
|
|
"top-left": "╭─",
|
|
"top-center": "─┬─",
|
|
"top-right": "─╮",
|
|
"mid-left": "├─",
|
|
"mid-center": "─┼─",
|
|
"mid-right": "─┤",
|
|
"bot-left": "╰─",
|
|
"bot-center": "─┴─",
|
|
"bot-right": "─╯"})
|
|
|
|
|
|
class TestTable(unittest.TestCase):
|
|
"""Test the Table class."""
|
|
|
|
def test_align_funcs(self):
|
|
"""Test the alignment dictionary."""
|
|
self.assertDictEqual(xfstestsdb.table.ALIGN,
|
|
{"l": str.ljust, "c": str.center, "r": str.rjust})
|
|
|
|
def test_init(self):
|
|
"""Test initializing a table."""
|
|
tbl = xfstestsdb.table.Table(["Test", "Columns"])
|
|
self.assertIsInstance(tbl.colors, xfstestsdb.colors.ColorScheme)
|
|
self.assertDictEqual(tbl.borders, xfstestsdb.table.ASCII_BORDERS)
|
|
self.assertListEqual(tbl.columns, ["Test", "Columns"])
|
|
self.assertListEqual(tbl.align, ["l", "l"])
|
|
self.assertListEqual(tbl.maxlens, [4, 7])
|
|
self.assertListEqual(tbl.rows, [])
|
|
|
|
def test_init_align(self):
|
|
"""Test initializing a table with an alignment value."""
|
|
tbl = xfstestsdb.table.Table(["Test", "Some", "Columns"], ["c", "r"])
|
|
self.assertListEqual(tbl.align, ["c", "r", "l"])
|
|
|
|
tbl = xfstestsdb.table.Table(["Test", "Align"], ["c", "r", "r", "l"])
|
|
self.assertListEqual(tbl.align, ["c", "r"])
|
|
|
|
def test_add_column(self):
|
|
"""Test adding a single column to the table."""
|
|
tbl = xfstestsdb.table.Table(["Test"])
|
|
tbl.add_column("Add")
|
|
self.assertListEqual(tbl.columns, ["Test", "Add"])
|
|
self.assertListEqual(tbl.maxlens, [4, 3])
|
|
self.assertListEqual(tbl.align, ["l", "l"])
|
|
|
|
tbl.add_row(1, 2)
|
|
tbl.add_column("Column", align="c")
|
|
self.assertListEqual(tbl.columns, ["Test", "Add", "Column"])
|
|
self.assertListEqual(tbl.maxlens, [4, 3, 6])
|
|
self.assertListEqual(tbl.align, ["l", "l", "c"])
|
|
self.assertListEqual(tbl.rows, [["1", "2", ""]])
|
|
|
|
def test_add_row(self):
|
|
"""Test adding a row to a table."""
|
|
tbl = xfstestsdb.table.Table(["Test", "Columns"])
|
|
tbl.add_row("Row", 1)
|
|
self.assertListEqual(tbl.rows, [["Row", "1"]])
|
|
self.assertListEqual(tbl.maxlens, [4, 7])
|
|
|
|
tbl.add_row("Longer", 123456789)
|
|
self.assertListEqual(tbl.rows, [["Row", "1"],
|
|
["Longer", "123456789"]])
|
|
self.assertListEqual(tbl.maxlens, [6, 9])
|
|
|
|
tbl.add_row("Too", "Many", "Columns")
|
|
self.assertListEqual(tbl.rows, [["Row", "1"],
|
|
["Longer", "123456789"],
|
|
["Too", "Many"]])
|
|
self.assertListEqual(tbl.maxlens, [6, 9])
|
|
|
|
tbl.add_row("Too Short")
|
|
self.assertListEqual(tbl.rows, [["Row", "1"],
|
|
["Longer", "123456789"],
|
|
["Too", "Many"],
|
|
["Too Short", ""]])
|
|
|
|
tbl.add_row("None", None)
|
|
self.assertListEqual(tbl.rows, [["Row", "1"],
|
|
["Longer", "123456789"],
|
|
["Too", "Many"],
|
|
["Too Short", ""],
|
|
["None", ""]])
|
|
|
|
tbl.add_row()
|
|
self.assertListEqual(tbl.rows, [["Row", "1"],
|
|
["Longer", "123456789"],
|
|
["Too", "Many"],
|
|
["Too Short", ""],
|
|
["None", ""],
|
|
["", ""]])
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_print_empty(self, mock_stdout: io.StringIO):
|
|
"""Test printing an empty table."""
|
|
print(xfstestsdb.table.Table(["Test", "Columns"]))
|
|
self.assertEqual(mock_stdout.getvalue(), "\n")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_print_default_align_unicode(self, mock_stdout: io.StringIO):
|
|
"""Test printing a table with default alignment and unicode borders."""
|
|
tbl = xfstestsdb.table.Table(["Test", "Table", "Columns"],
|
|
color_scheme="dark")
|
|
tbl.add_row(1, 2, 3)
|
|
print()
|
|
print(tbl)
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
╭──────┬───────┬─────────╮
|
|
│ Test │ Table │ Columns │
|
|
├──────┼───────┼─────────┤
|
|
│ 1 │ 2 │ 3 │
|
|
╰──────┴───────┴─────────╯
|
|
""")
|
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
|
def test_print_align_ascii(self, mock_stdout: io.StringIO):
|
|
"""Test printing with custom alignment and ascii borders."""
|
|
tbl = xfstestsdb.table.Table(["Left", "Center", "Right"],
|
|
["l", "c", "r"], color_scheme="none")
|
|
tbl.add_row("abcdefg", "hijklmnopq", "rstuvwxyz")
|
|
tbl.add_row(123, 4567, 890)
|
|
print()
|
|
print(tbl)
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
"""
|
|
+---------+------------+-----------+
|
|
| Left | Center | Right |
|
|
+---------+------------+-----------+
|
|
| abcdefg | hijklmnopq | rstuvwxyz |
|
|
| 123 | 4567 | 890 |
|
|
+---------+------------+-----------+
|
|
""")
|