Debian Codegraph @

src/tests/test_print_table.py

raw
1 """Basic unit tests of print_table()."""
2 # pylint: disable=invalid-name,import-error
3
4 import io
5 import unittest
6 import unittest.mock
7
8 from foomuuri import print_table
9
10
11 @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
12 class TestPrintTable(unittest.TestCase):
13 """Test print_table()."""
14
15 def setUp(self):
16 """Fill in common fixture values."""
17 self.data_multiple_rows = [
18 (
19 'row 1 column 1',
20 'row 1 colum 2',
21 'row 1 colu 3',
22 ),
23 (
24 'row 2 colu 1',
25 'row 2 colum 2',
26 'row 2 column 3',
27 ),
28 ]
29
30 self.data_no_rows = []
31
32 self.header = (
33 'Header',
34 'Long Elaborate Header',
35 'Medium Header',
36 )
37
38 def test_invalid_align_values(self, _):
39 """Test invalid align values."""
40 invalid_align = [
41 '', # no alignment
42 'x', # invalid align value
43 '<<', # too few values for 3 columns
44 '<<<<', # too much values for 3 columns
45 ]
46
47 for align in invalid_align:
48 with self.subTest(align):
49 self.assertRaises(
50 ValueError,
51 print_table,
52 self.data_multiple_rows,
53 align=align,
54 )
55
56 def test_print_table_all_args(self, stdout: io.StringIO):
57 """Test table with all supported arguments."""
58 print_table(
59 data=[self.header] + self.data_multiple_rows,
60 header=True,
61 align='<>^',
62 sep=' ',
63 )
64
65 self.assertEqual(
66 stdout.getvalue(),
67 """Header Long Elaborate Header Medium Header
68 -------------- --------------------- --------------
69 row 1 column 1 row 1 colum 2 row 1 colu 3
70 row 2 colu 1 row 2 colum 2 row 2 column 3
71 """,
72 )
73
74 def test_print_table_with_rows_no_headers(self, stdout: io.StringIO):
75 """Test table without headers, rows only."""
76 print_table(data=self.data_multiple_rows)
77
78 self.assertEqual(
79 stdout.getvalue(),
80 """row 1 column 1 row 1 colum 2 row 1 colu 3
81 row 2 colu 1 row 2 colum 2 row 2 column 3
82 """,
83 )
84
85 def test_print_table_no_rows(self, stdout: io.StringIO):
86 """Test table without headers and rows."""
87 print_table(data=self.data_no_rows)
88
89 self.assertEqual(
90 stdout.getvalue(),
91 '',
92 )
93
94 def test_print_table_with_headers_no_rows(self, stdout: io.StringIO):
95 """Test table with headers, without rows."""
96 print_table(data=[self.header] + self.data_no_rows, header=True)
97 self.assertEqual(
98 stdout.getvalue(),
99 '',
100 )
101
102 def test_print_table_default_align(self, stdout: io.StringIO):
103 """Test table with headers, and default (left) align."""
104 print_table(data=[self.header] + self.data_multiple_rows, header=True)
105
106 self.assertEqual(
107 stdout.getvalue(),
108 """Header Long Elaborate Header Medium Header
109 -------------- --------------------- --------------
110 row 1 column 1 row 1 colum 2 row 1 colu 3
111 row 2 colu 1 row 2 colum 2 row 2 column 3
112 """,
113 )