Skip to content

Commit

Permalink
fixes #8
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Feb 14, 2017
1 parent 0f07a66 commit 1a23ba6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/pyRestTable/rest_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

def _prepare_results_(t):
s = ''
s += t.reST(fmt='plain') + '\n'
s += t.reST(fmt='simple') + '\n'
s += t.reST(fmt='grid') + '\n'
s += t.reST(fmt='list-table')
Expand Down Expand Up @@ -144,11 +145,32 @@ def reST(self, indentation = '', fmt = 'simple'):
msg = "Number of column labels is different from column width specifiers"
raise IndexError(msg)
return {'simple': self.simple_table,
'plain': self.plain_table,
'complex': self.grid_table, # alias for `grid`, do not deprecate
'grid': self.grid_table,
'list-table': self.list_table,
}[fmt](indentation)

def plain_table(self, indentation = ''):
'''render the table in *plain* reST format'''
# maximum column widths, considering possible line breaks in each cell
width = self.find_widths()

# build the row format strings
fmt = " ".join(["%%-%ds" % w for w in width]) + '\n'

rest = ''
if self.use_tabular_columns:
rest += indentation
rest += '.. tabularcolumns:: |%s|' % '|'.join(self.alignment)
if self.longtable:
rest += '\n%s%s' % (' '*4, ':longtable:')
rest += '\n\n'
rest += self._row(self.labels, fmt, indentation) # labels
for row in self.rows:
rest += self._row(row, fmt, indentation) # each row
return rest

def simple_table(self, indentation = ''):
'''render the table in *simple* reST format'''
# maximum column widths, considering possible line breaks in each cell
Expand Down
58 changes: 55 additions & 3 deletions tests/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
= =
'''

MINIMAL_PLAIN_RESULT = '''\
x y
1 2
'''

MINIMAL_GRID_RESULT = '''\
+---+---+
| x | y |
Expand All @@ -76,14 +81,22 @@
'''


EXAMPLE_MINIMAL_RESULT = MINIMAL_SIMPLE_RESULT
EXAMPLE_MINIMAL_RESULT = MINIMAL_PLAIN_RESULT
EXAMPLE_MINIMAL_RESULT += '\n'
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
=== === =====
Expand Down Expand Up @@ -158,10 +171,49 @@ def setUp(self):
def apply_test(self, table, reference_text, style='simple'):
text = table.reST(fmt=style)
self.assertTrue(text == reference_text)

def population_table(self):
t = pyRestTable.Table()
t.addLabel('City name')
t.addLabel('Area')
t.addLabel('Population')
t.addLabel('Annual Rainfall')
t.addRow(['Adelaide', 1295, 1158259, 600.5])
t.addRow(['Brisbane', 5905, 1857594, 1146.4])
t.addRow(['Darwin', 112, 120900, 1714.7])
return t

def test_simple(self):
import pyRestTable.simple
self.apply_test(pyRestTable.simple.main(), SIMPLE_RESULT)
s = self.population_table().reST(fmt = 'simple')
expected = '========= ==== ========== ===============\n'
expected += 'City name Area Population Annual Rainfall\n'
expected += '========= ==== ========== ===============\n'
expected += 'Adelaide 1295 1158259 600.5 \n'
expected += 'Brisbane 5905 1857594 1146.4 \n'
expected += 'Darwin 112 120900 1714.7 \n'
expected += '========= ==== ========== ===============\n'
self.assertEqual(s, expected)

def test_plain(self):
s = self.population_table().reST(fmt = 'plain')
expected = 'City name Area Population Annual Rainfall\n'
expected += 'Adelaide 1295 1158259 600.5 \n'
expected += 'Brisbane 5905 1857594 1146.4 \n'
expected += 'Darwin 112 120900 1714.7 \n'
self.assertEqual(s, expected)

def test_grid(self):
s = self.population_table().reST(fmt = 'grid')
expected = '+-----------+------+------------+-----------------+\n'
expected += '| City name | Area | Population | Annual Rainfall |\n'
expected += '+===========+======+============+=================+\n'
expected += '| Adelaide | 1295 | 1158259 | 600.5 |\n'
expected += '+-----------+------+------------+-----------------+\n'
expected += '| Brisbane | 5905 | 1857594 | 1146.4 |\n'
expected += '+-----------+------+------------+-----------------+\n'
expected += '| Darwin | 112 | 120900 | 1714.7 |\n'
expected += '+-----------+------+------------+-----------------+\n'
self.assertEqual(s, expected)

def test_cansas(self):
import pyRestTable.cansas
Expand Down

0 comments on commit 1a23ba6

Please sign in to comment.