Skip to content

Commit

Permalink
Merge pull request #50 from joej164/add-simple-table-#44
Browse files Browse the repository at this point in the history
FEA: adding simple table, tests, and some parameterized tests
  • Loading branch information
thclark committed Feb 13, 2024
2 parents 0195a40 + 6b0f3c0 commit 081ea16
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rstcloth/rstcloth.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ def table(self, header: typing.List, data: t_optional_2d_array, indent=0) -> Non
t = tabulate(tabular_data=data, headers=header, tablefmt="grid", disable_numparse=True)
self._add("\n" + _indent(t, indent) + "\n")

def simple_table(self, header: typing.List, data: t_optional_2d_array, indent=0) -> None:
"""
Constructs a simple grid table.
:param header: a list of header values (strings), to use for the table
:param data: a list of lists of row data (same length as the header
list each)
:param indent: number of spaces to indent this element
"""

t = tabulate(tabular_data=data, headers=header, tablefmt="rst", disable_numparse=True)
self._add("\n" + _indent(t, indent) + "\n")

def table_list(
self,
headers: typing.Iterable,
Expand Down
73 changes: 73 additions & 0 deletions tests/test_rstcloth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import unittest
import pytest

from rstcloth import RstCloth

Expand Down Expand Up @@ -649,5 +650,77 @@ def test_table_list_integer_width(self):
self.assertEqual(r.data, expected)


class SimpleTestTable(unittest.TestCase):
"""Testing operation of the Rst generator for simple tables"""

def test_header_1_body_0(self):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=["span"], data=None)
expected = "\n======\nspan\n======\n======\n\n"
given = r.data
self.assertEqual(expected, given)

def test_header_1_body_1(self):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=["span"], data=[[1]])
expected = "\n======\nspan\n======\n1\n======\n\n"
given = r.data
self.assertEqual(expected, given)

def test_header_2_body_0(self):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=["span", "ham"], data=None)
expected = "\n====== =====\nspan ham\n====== =====\n====== =====\n\n"
given = r.data
self.assertEqual(expected, given)

def test_header_2_body_1(self):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=["span", "ham"], data=[[1, 2]])
expected = "\n====== =====\nspan ham\n====== =====\n1 2\n====== =====\n\n"
given = r.data
self.assertEqual(expected, given)

def test_header_2_body_2(self):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=["span", "ham"], data=[[1, 2], [3, 4]])
expected = "\n====== =====\nspan ham\n====== =====\n1 2\n3 4\n====== =====\n\n"
given = r.data
self.assertEqual(expected, given)

def test_header_2_body_2_indent(self):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=["span", "ham"], data=[[1, 2], [3, 4]], indent=3)
expected = (
"\n ====== =====\n span ham\n ====== =====\n 1 2\n 3 4\n ====== =====\n\n"
)
given = r.data
self.assertEqual(expected, given)


@pytest.mark.parametrize(
"header,data,expected",
[
(["span"], None, "\n======\nspan\n======\n======\n\n"),
(["span"], [[1]], "\n======\nspan\n======\n1\n======\n\n"),
(["span", "ham"], None, "\n====== =====\nspan ham\n====== =====\n====== =====\n\n"),
(["span", "ham"], [[1, 2]], "\n====== =====\nspan ham\n====== =====\n1 2\n====== =====\n\n"),
(
["span", "ham"],
[[1, 2], [3, 4]],
"\n====== =====\nspan ham\n====== =====\n1 2\n3 4\n====== =====\n\n",
),
],
)
class TestParamterizedSimpleTestTable:
"""Testing operation of the Rst generator for simple tables"""

def test_header_1_body_0(self, header, data, expected):
r = RstCloth(stream=io.StringIO())
r.simple_table(header=header, data=data)
given = r.data
assert given == expected


if __name__ == "__main__":
unittest.main()

0 comments on commit 081ea16

Please sign in to comment.