Skip to content

Commit

Permalink
fixes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Feb 14, 2017
1 parent 5c84608 commit 0f07a66
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 22 deletions.
21 changes: 7 additions & 14 deletions src/pyRestTable/rest_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def example_complicated():
t.addRow( ['one,\ntwo', "buckle my", "shoe.\n\n\nthree,\nfour", "..."] )
t.addRow( ['class', 'NX_FLOAT', '', None, ] )
t.addRow( range(0,4) )
t.addRow( [None, t, 1.234, range(3)] )
t.addRow( [None, {'a':1, 'b': 'dreamy'}, 1.234, range(3)] )
t.setLongtable()
t.setTabularColumns(True, 'l L c r'.split())
return t
Expand All @@ -93,6 +93,9 @@ def __init__(self):
self.alignment = []
self.longtable = False

def __str__(self):
return self.reST()

def addLabel(self, text):
'''
add label for one additional column
Expand Down Expand Up @@ -284,21 +287,11 @@ def find_widths(self):
if len(self.labels) > 0:
width = [max(map(len, str(_).split("\n"))) for _ in self.labels]
for row in self.rows:
row_width = [max(map(len, str(_).split("\n"))) for _ in row]

row_width = [max(map(len, str(_ or '').split("\n"))) for _ in row]

if len(width) == 0:
width = row_width
width = list(map( max, zip(width, row_width) ))
return width


def main():
'''test routine used to demo the code'''
print(_prepare_results_(example_basic()))
print('\n-----------\n')
print(_prepare_results_(example_complicated()))
print('\n-----------\n')
print(_prepare_results_(example_minimal()))


if __name__ == '__main__':
main()
53 changes: 45 additions & 8 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,31 @@
- 4,3'''


EXAMPLE_COMPLICATED_RESULT = '.. tabularcolumns:: |l|L|c|r|\n :longtable:\n\n========== ========================================================= ====== =================\nName Type Units Description \nand (and Occurrences)\nAttributes \n========== ========================================================= ====== =================\none, buckle my shoe. ... \ntwo \n \n three, \n four \nclass NX_FLOAT None \n0 1 2 3 \nNone <pyRestTable.rest_table.Table instance at 0x7f7401d2bcf8> 1.234 [0, 1, 2] \n========== ========================================================= ====== =================\n\n.. tabularcolumns:: |l|L|c|r|\n :longtable:\n\n+------------+-----------------------------------------------------------+--------+-------------------+\n| Name | Type | Units | Description |\n| and | | | (and Occurrences) |\n| Attributes | | | |\n+============+===========================================================+========+===================+\n| one, | buckle my | shoe. | ... |\n| two | | | |\n| | | | |\n| | | three, | |\n| | | four | |\n+------------+-----------------------------------------------------------+--------+-------------------+\n| class | NX_FLOAT | | None |\n+------------+-----------------------------------------------------------+--------+-------------------+\n| 0 | 1 | 2 | 3 |\n+------------+-----------------------------------------------------------+--------+-------------------+\n| None | <pyRestTable.rest_table.Table instance at 0x7f7401d2bcf8> | 1.234 | [0, 1, 2] |\n+------------+-----------------------------------------------------------+--------+-------------------+\n\n.. list-table:: \n :header-rows: 1\n :widths: 10 57 6 17\n\n * - Name\n and\n Attributes\n - Type\n - Units\n - Description\n (and Occurrences)\n * - one,\n two\n - buckle my\n - shoe.\n \n \n three,\n four\n - ...\n * - class\n - NX_FLOAT\n - \n - \n * - 0\n - 1\n - 2\n - 3\n * - None\n - <pyRestTable.rest_table.Table instance at 0x7f7401d2bcf8>\n - 1.234\n - [0, 1, 2]'


class TestUM(unittest.TestCase):
EXAMPLE_COMPLICATED_RESULT = '''
.. tabularcolumns:: |l|L|c|r|
:longtable:
+------------+-------------------------+--------+-------------------+
| Name | Type | Units | Description |
| and | | | (and Occurrences) |
| Attributes | | | |
+============+=========================+========+===================+
| one, | buckle my | shoe. | ... |
| two | | | |
| | | | |
| | | three, | |
| | | four | |
+------------+-------------------------+--------+-------------------+
| class | NX_FLOAT | | None |
+------------+-------------------------+--------+-------------------+
| 0 | 1 | 2 | 3 |
+------------+-------------------------+--------+-------------------+
| None | {'a': 1, 'b': 'dreamy'} | 1.234 | [0, 1, 2] |
+------------+-------------------------+--------+-------------------+
'''.strip()


class Test_pyRestTable(unittest.TestCase):

def setUp(self):
pass
Expand Down Expand Up @@ -165,8 +186,8 @@ def test_example_basic(self):

def test_example_complicated(self):
t = pyRestTable.rest_table.example_complicated()
s = pyRestTable.rest_table._prepare_results_(t)
self.assertNotEqual(s, EXAMPLE_COMPLICATED_RESULT)
s = t.reST(fmt='grid').strip()
self.assertEqual(s, EXAMPLE_COMPLICATED_RESULT)

def test_example_minimal(self):
t = pyRestTable.rest_table.example_minimal()
Expand Down Expand Up @@ -223,6 +244,22 @@ def test_num_col_labels_different_from_col_width_specifiers(self):
expected += ' - 2,2'
self.assertEqual(s, expected)

def test_default_str(self):
t = pyRestTable.rest_table.example_minimal()
s = str(t)
self.assertEqual(s, MINIMAL_SIMPLE_RESULT, 'string representation')


def suite(*args, **kw):
test_suite = unittest.TestSuite()
test_list = [
Test_pyRestTable,
]
for test_case in test_list:
test_suite.addTest(unittest.makeSuite(test_case))
return test_suite


if __name__ == '__main__':
unittest.main()
if __name__ == "__main__":
runner=unittest.TextTestRunner(verbosity=2)
runner.run(suite())

0 comments on commit 0f07a66

Please sign in to comment.