def testReuse(self): t = HtmlTable() t.addColumn(Column(label="c1")) html = t.render((1, 2)) self.assertEqualsWS("<table><thead><tr><th>c1</th></tr></thead><tfoot></tfoot><tbody><tr><td>1</td></tr><tr><td>2</td></tr></tbody></table>", html) html = t.render((3, 4)) self.assertEqualsWS("<table><thead><tr><th>c1</th></tr></thead><tfoot></tfoot><tbody><tr><td>3</td></tr><tr><td>4</td></tr></tbody></table>", html)
def testMyOwnRendering(self): class CustomColumn(Column): def cell_content(self, item, parent, **kwargs): yield str(item).swapcase() + parent t = HtmlTable() t.addColumn(CustomColumn(label='c1')) html = t.render(items=['aa<p'], parent='fiets') self.assertEqualsWS( '<table>' '<thead><tr><th>c1</th></tr></thead>' '<tfoot></tfoot>' '<tbody>' '<tr><td>AA<Pfiets</td></tr>' '</tbody></table>', html)
def testPrevCurNext(self): class PrevCurNextColumn(Column): def cell_content(self, item, prevItem, nextItem, **kwargs): yield '{} {} {}'.format(prevItem or '-', item, nextItem or '-') t = HtmlTable() t.addColumn(PrevCurNextColumn(label="label")) html = t.render([1,2,3]) self.assertEqualsWS('<table>' '<thead><tr><th>label</th></tr></thead>' '<tfoot></tfoot>' '<tbody>' '<tr><td>- 1 2</td></tr>' '<tr><td>1 2 3</td></tr>' '<tr><td>2 3 -</td></tr>' '</tbody>' '</table>', html)
def testColumnBasics(self): self.maxDiff = None st = HtmlTable() st.addColumn(Column(label='Label')) st.addColumn(Column(label='<label>')) html = st.render(items=(1,2,3)) self.assertEqualsWS( '<table>' '<thead>' '<tr><th>Label</th><th><label></th></tr>' '</thead>' '<tfoot></tfoot>' '<tbody>' '<tr><td>1</td><td>1</td></tr>' '<tr><td>2</td><td>2</td></tr>' '<tr><td>3</td><td>3</td></tr>' '</tbody>' '</table>', html)
def testColumnWithColspan(self): class OneHeadTwoCells(Column): def head_tag(self, **kwargs): return Column.head_tag(self).set('colspan', '2') def colspan(self): return 2 def cell_tag(self, **kwargs): return self.tag(None) def cell_content(self, item, **kwargs): with self.tag('td'): yield '1-{}'.format(item) with self.tag('td'): yield '2-{}'.format(item) t = HtmlTable() t.addColumn(OneHeadTwoCells(label="label")) html = t.render(("a", "b")) self.assertEqualsWS('<table>' '<thead><tr><th colspan="2">label</th></tr></thead>' '<tfoot></tfoot>' '<tbody>' '<tr><td>1-a</td><td>2-a</td></tr>' '<tr><td>1-b</td><td>2-b</td></tr>' '</tbody>' '</table>', html)