Skip to content

Commit

Permalink
more tests, increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Nov 23, 2016
1 parent 9754bc7 commit 9cc6bfe
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/pyRestTable/cansas.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ def main():
t = Table()
t.labels = ['SASentry', 'description', 'measurements']
for node in node_list:
s_name, count = '', ''
subnode = node.find('cs:Title', namespaces=nsmap)
if subnode is not None:
s = etree.tostring(subnode, method="text")
s_name = node.attrib['name']
count = len(node.xpath('cs:SASdata', namespaces=nsmap))
else:
s_name = ''
count = ''
title = s.strip().decode()
t.rows += [[s_name, title, count]]

Expand Down
16 changes: 9 additions & 7 deletions src/pyRestTable/rest_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
'''


def _print_results_(t):
print(t.reST(fmt='simple') + '\n')
print(t.reST(fmt='grid') + '\n')
print(t.reST(fmt='list-table'))
def _prepare_results_(t):
s = ''
s += t.reST(fmt='simple') + '\n'
s += t.reST(fmt='grid') + '\n'
s += t.reST(fmt='list-table')
return s


def example_minimal():
Expand Down Expand Up @@ -289,11 +291,11 @@ def find_widths(self):

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


if __name__ == '__main__':
Expand Down
118 changes: 118 additions & 0 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,59 @@
'''


EXAMPLE_MINIMAL_RESULT = MINIMAL_SIMPLE_RESULT
EXAMPLE_MINIMAL_RESULT += '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_GRID_RESULT
EXAMPLE_MINIMAL_RESULT += '\n'
EXAMPLE_MINIMAL_RESULT += MINIMAL_LISTTABLE_RESULT


EXAMPLE_BASIC_RESULT = '''\
=== === =====
one two three
=== === =====
1,1 1,2 1,3
2,1 2,2 2,3
3,1 3,2 3,3
4,1 4,2 4,3
=== === =====
+-----+-----+-------+
| one | two | three |
+=====+=====+=======+
| 1,1 | 1,2 | 1,3 |
+-----+-----+-------+
| 2,1 | 2,2 | 2,3 |
+-----+-----+-------+
| 3,1 | 3,2 | 3,3 |
+-----+-----+-------+
| 4,1 | 4,2 | 4,3 |
+-----+-----+-------+
.. list-table::
:header-rows: 1
:widths: 3 3 5
* - one
- two
- three
* - 1,1
- 1,2
- 1,3
* - 2,1
- 2,2
- 2,3
* - 3,1
- 3,2
- 3,3
* - 4,1
- 4,2
- 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):

def setUp(self):
Expand Down Expand Up @@ -105,6 +158,71 @@ def test_minimal_grid(self):
def test_minimal_listtable(self):
self.apply_test(pyRestTable.rest_table.example_minimal(), MINIMAL_LISTTABLE_RESULT, 'list-table')

def test_example_basic(self):
t = pyRestTable.rest_table.example_basic()
s = pyRestTable.rest_table._prepare_results_(t)
self.assertEqual(s, EXAMPLE_BASIC_RESULT)

def test_example_complicated(self):
t = pyRestTable.rest_table.example_complicated()
s = pyRestTable.rest_table._prepare_results_(t)
self.assertNotEqual(s, EXAMPLE_COMPLICATED_RESULT)

def test_example_minimal(self):
t = pyRestTable.rest_table.example_minimal()
s = pyRestTable.rest_table._prepare_results_(t)
self.assertEqual(s, EXAMPLE_MINIMAL_RESULT)

def test_zero_width_column(self):
t = pyRestTable.rest_table.Table()
t.labels = ('', 'two', )
t.rows.append( ['', '1,2',] )
t.rows.append( ['', '2,2',] )
s = t.reST(fmt='simple')
expected = ' ===\n'
expected += ' two\n'
expected += ' ===\n'
expected += ' 1,2\n'
expected += ' 2,2\n'
expected += ' ===\n'
self.assertEqual(s, expected)

def test_zero_columns(self):
t = pyRestTable.rest_table.Table()
t.labels = ()
t.rows.append( [] )
t.rows.append( [] )
s = t.reST(fmt='simple')
expected = '\n\n\n'
self.assertEqual(s, expected)

def test_num_col_labels_different_from_col_width_specifiers(self):
# Number of column labels is different from column width specifiers
t = pyRestTable.rest_table.Table()
t.use_tabular_columns = True
t.alignment = 'lll'
t.longtable = True
t.labels = ('one', 'two', )
t.rows.append( ['1,1', '1,2',] )
t.rows.append( ['2,1', '2,2',] )
self.assertRaises(IndexError, t.reST, fmt='list-table')

# now fix and proceed
t.alignment = 'll'
s = t.reST(fmt='list-table')
expected = ''
expected += '.. list-table:: \n'
expected += ' :header-rows: 1\n'
expected += ' :widths: 3 3\n'
expected += '\n'
expected += ' * - one\n'
expected += ' - two\n'
expected += ' * - 1,1\n'
expected += ' - 1,2\n'
expected += ' * - 2,1\n'
expected += ' - 2,2'
self.assertEqual(s, expected)


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

0 comments on commit 9cc6bfe

Please sign in to comment.